DEV Community

Discussion on: AWS CDK: Per-Environment Configuration Patterns

Collapse
 
daveharig profile image
Dave Harig

Thank you for taking the time to put this together.

I have been struggling with this for weeks, and even AWS Support does not know how to do it.

I have two environments, dev and prod. I have two cli profiles configured, dev and prod.

I did not set a default config, so I force myself to always declare the profile like this $cdk deploy stackName --profile dev

What I am trying to do is set property values based on the profile being used to deploy.

if ---> $cdk deploy stackName --profile prod
then ---> someProperty : productionDomain.com

if --->$cdk deploy stackName --profile dev
then ---> someProperty : developmentDomain.com

I read (and re-read) your post but it's still over my head. Please let me know if you would be willing to dumb it down to my example above.

Collapse
 
varvay profile image
Varid Vaya Yusuf • Edited

It might be to late to answer this, I also new in using CDK but I implement such functionality by providing additional context through cdk command option --context and then retrieve the rest of the context defined in cdk.json file based on it, as the sample shown below,
cdk.json file:

{
  ...
  "context": {
    ...
    ],
    "dev": {
      "someProperty": "developmentDomain.com"
    },
    "prod": {
      "someProperty": "productionDomain.com"
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

bin/app.ts file:

const app = new App();
const env = app.node.tryGetContext('env');
const conf = app.node.tryGetContext(env);
Enter fullscreen mode Exit fullscreen mode

I synth the cdk app by issuing command cdk synth --profile prod --context env=dev and the conf constant result would be like,

{
   "someProperty": "developmentDomain.com"
}
Enter fullscreen mode Exit fullscreen mode

Also I want to keep the env context provided receives dev or prod value only, hence I implement schema validation using zod while parsing the env context