DEV Community

Discussion on: AWS CDK: Per-Environment Configuration Patterns

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