Home > Blockchain >  CDK Deployment of 2 Identical Apps To The Same Account For Different Environments
CDK Deployment of 2 Identical Apps To The Same Account For Different Environments

Time:10-28

Given a situation where the same AWS account will host the Dev and UAT cloud deployments simultaneously, what is the best way to instantiate and segregate those 2 pseudo-environments?

I say "pseudo" because the cdk Environment only takes in an account id & region. There is no other separation mechanism.

I could hand down the prefix manually into resource names by passing variables to the Construct constructor call which is passed in by the Stack constructor

main(args) {

  app App = ...

  Stack(app, "stackid", bucketPrefix = args[0]) 

But I'm asking if there is a cdk/aws native way that enables this instead.

Thank you in advance for your consideration and response.

CodePudding user response:

While the CDK best practice is separate environments for Dev and UAT, it's certainly possible to deploy multiple app copies in the same account/region pair*.

  1. Each app's stacks need a different construct id - e.g. DevMyStack and UatMyStack. Child construct identifiers inherit their parent's scope, so passing the prefixes down the construct hierarchy isn't required. The docs have an example of duplicating stacks within an app, which has the same constraints as your case.
  2. This assumes you are following the best practice of letting CDK/CloudFormation set resource names. The Dev and UAT builds are ephemeral anyway, so this shouldn't be a problem. If you prefer pretty names, consider setting explicit resource names only for production:
bucketName: props.isProduction ? "my-explicit-bucket-name" : undefined
  1. Add tags for better operational and cost control. It's easy to tag to all stack resources in one go:
cdk.Tags.of(DevMyStack).add('build', 'dev');

* Your apps should not contain "singleton" resources that are created once per account/region.

  • Related