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

[cli] Allow dynamic stack name generation during cdk synth #9261

Closed
mohdarshad opened this issue Jul 25, 2020 · 3 comments
Closed

[cli] Allow dynamic stack name generation during cdk synth #9261

mohdarshad opened this issue Jul 25, 2020 · 3 comments
Assignees
Labels
guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. package/tools Related to AWS CDK Tools or CLI

Comments

@mohdarshad
Copy link

Is there anyway I can keep stack name dynamic and not hardcoded in the code? Is there any way I can pass stackName during cdk synth? I have defined a single stack in my code but want to run the same stack for multiple project, hence I want different stackname to be created in cloudformation for different project. Language = Java
I am unable to find any documentation supporting this.

@mohdarshad mohdarshad added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Jul 25, 2020
@mohdarshad
Copy link
Author

Apparently I can pass the stack name in context, and use the context value to set the stack name in StackProps.
Kindly confirm.

@SomayaB SomayaB changed the title Allow dynamic stack name generation during cdk synth [cli] Allow dynamic stack name generation during cdk synth Jul 27, 2020
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Jul 27, 2020
@mohdarshad
Copy link
Author

Apparently I can pass the stack name in context, and use the context value to set the stack name in StackProps.

@stevestorey
Copy link

This was the top result when I was searching for a solution to the same issue, and I did find a way to do it which I'll leave here - you can pass context parameters when you run the cdk script, so you can do something like cdk deploy -c myParam=some-value and pick up the context value as you're creating the stack - something like this (Typescript, but I would assume the same applies to the other runtimes as well):

const app = new cdk.App();

const myParam = app.node.tryGetContext("myParam");
if (myParam === undefined || !(typeof(myParam) === 'string') || myParam.trim() === '') {
  throw new Error("Must pass a '-c myParam=<MyParam>' context parameter");
}

new XyzStack(app, `xyz-${myParam}`, {
  env: { account: '1234567890', region: 'eu-west-1' },
});

which would produce a stack called xyz-some-value. It will also apply to cdk synth -c myParam=some-value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

3 participants