Skip to content

Instantly share code, notes, and snippets.

@ranguard
Last active December 6, 2020 19:26
Show Gist options
  • Save ranguard/caa16624734dfc157a10014f7c78bdcd to your computer and use it in GitHub Desktop.
Save ranguard/caa16624734dfc157a10014f7c78bdcd to your computer and use it in GitHub Desktop.
Pattern for deploying stacks to multiple environments
const envs:any = {
    dev: { region: 'eu-west-1', account: 'xxx' },
    production: { region: 'eu-west-1', account: 'yyy' },
};

class MyApp extends cdk.Construct {
    constructor(scope:any, id:any, props:any ) {
        super(scope, id);

        new OurStack1(this, 'ourstack1', { env: props.env, stackName: `${id}-ourstack1` });
        new OurStack2(this, 'ourstack2', { env: props.env, stackName: `${id}-ourstack1` });
        // ..
    }
}

const app = new cdk.App();
Object.entries(envs).forEach(([envName, env]) => {
    new MyApp(app, envName, { env });
});
> cdk list
dev-ourstack1
dev-ourstack2
production-ourstack1
production-ourstack2

See also: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html

@eladb
Copy link

eladb commented Jun 25, 2019

Use new OurStack1(this) instead of app. If you want to control the stack name, set it as stackName prop.

@ranguard
Copy link
Author

Use new OurStack1(this) instead of app. If you want to control the stack name, set it as stackName prop.

Thanks - have updated accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment