How to load aws cdk --parameters from a file?

Viewed 2035

My CDK stack contains too many parameters (subnet ids, api urls) to specify in the command line. So I'd like to keep them in separate files like dev.properties or prod.json. Context values from cdk.json could be the way but i don't see how to keep multiple parallel versions. Is there a way to apply parameters from a file, e.g. cdk deploy --parameters file:///dev.json?

1 Answers

you can use the cdk.json file for this. For example, my cdk.json looks like

{
  "app": "python3 app.py",
  "profile": "my-aws-profile",
  "context": {
    "@aws-cdk/core:enableStackNameDuplicates": "true",
    "aws-cdk:enableDiffNoFail": "true",
    "ENVIRONMENTS": {
      "prod": {
        "bucket_name": "my-prod-bucket-name",
      }
    }
}

Then in my stack code

from aws_cdk import (core, aws_s3)


class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, env) -> None:
        super().__init__(scope, id, env=env)

        environments = self.node.try_get_context("ENVIRONMENTS")
        environment = environments.get("prod")
        bucket_name = environment.get("bucket_name")

        my_bucket = aws_s3.Bucket(
            self,
            bucket_name,
            bucket_name=bucket_name
        )
Related