Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Riff-Raff YAML generator doesn't always handle multiple instances of the same GuStack #2298

Open
akash1810 opened this issue May 2, 2024 · 0 comments

Comments

@akash1810
Copy link
Member

akash1810 commented May 2, 2024

cc @richpryce

In https://github.com/guardian/datasync-notifier, we're instantiating the same GuStack class twice, to create two CloudFormation stacks. They differ only by the app property.

The Riff-Raff generator fails to correctly produce a file in this scenario:

  it("should create the correct riff-raff.yaml with multiple uses of the same class", () => {
    const app = new App({ outdir: "/tmp/cdk.out" });

    interface StackWithLambdaProps extends GuStackProps {
      app: string;
    }

    class StackWithLambda extends GuStack {
      // eslint-disable-next-line custom-rules/valid-constructors -- this is a test
      constructor(app: App, id: string, props: StackWithLambdaProps) {
        super(app, id, props);
        new GuLambdaFunction(this, "test", {
          app: props.app,
          runtime: Runtime.NODEJS_20_X,
          fileName: `${props.app}.zip`,
          handler: "handler.main",
          timeout: Duration.minutes(1),
        });
      }
    }

    new StackWithLambda(app, "test-stack-SYD", {
      stack: "test",
      stage: "TEST",
      app: "my-lambda-SYD",
      env: { region: "eu-west-1" },
    });

    new StackWithLambda(app, "test-stack-NYC", {
      stack: "test",
      stage: "TEST",
      app: "my-lambda-NYC",
      env: { region: "eu-west-1" },
    });

    const actual = new RiffRaffYamlFile(app).toYAML();
    expect(actual).toMatchSnapshot();
  });

This is because of how GuStacks are grouped; as the class name is the same, the generator thinks there is only one StackWithLambda.

A work-around is to sub-class:

class StackWithLambda extends GuStack {
  // eslint-disable-next-line custom-rules/valid-constructors -- this is a test
  constructor(app: App, id: string, props: StackWithLambdaProps) {
    super(app, id, props);
    new GuLambdaFunction(this, "test", {
      app: props.app,
      runtime: Runtime.NODEJS_20_X,
      fileName: `${props.app}.zip`,
      handler: "handler.main",
      timeout: Duration.minutes(1),
    });
  }
}

class StackOne extends StackWithLambda {}
class StackTwo extends StackWithLambda {}

Should there be better first-class support for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant