CDK environment config in Python or a YAML file?

November 16, 2026 4 answers
Asked by r.205~cobaltclip

I'm setting up a Python CDK repo with a few settings that differ between dev, staging, and production. Typed Python classes already catch mistakes early, but a YAML or JSON file seems simpler to edit.

If you use files, do you keep one per environment or put them all together? Do you validate the data before passing it to CDK, and what tradeoffs matter most when choosing between files and code?

Answers

p.816+modclip

I'd stick with the typed Python classes unless there's a real need for a separate data file. You already have autocomplete and early checks. Moving values to YAML doesn't remove the need for validation, so you may end up maintaining a schema as well as the CDK code.

Pass the selected configuration into your stacks as props rather than having constructs look up the environment everywhere. That keeps the selection logic at the top of the app and makes the stacks easier to follow.

Reply to this answer
p.628!cobblemixer

A JSON or YAML file is reasonable if you want the settings to be editable as data or shared with tools outside Python. Load it once, validate it against a schema, then turn it into a typed config object before constructing stacks. A raw dictionary can let a typo travel a long way before it causes a useful error.

One file with environments nested inside it makes shared settings and comparisons easy. Separate files can be easier to manage as the number or size of environments grows. Either way, make the selected environment explicit and fail if it's missing.

r.205~cobaltclip:

Would you keep common values alongside the environment-specific ones?

_u71+papercrank.3:

That can work. A shared section with environment-specific overrides avoids repeating values, but keep the override rule clear so it's obvious which value wins.

Reply to this answer
_b28~signalladle.7

CDK context is another option for a small set of values. You can use a context value to choose the environment and keep global settings in cdk.json, with stack-specific values near the stack code.

It can get unwieldy if lots of constructs independently read context, though. Pick the config at the app boundary and pass the relevant values down.

Reply to this answer
_x64!drainclip.3

I'd separate deployment configuration from values an application needs at runtime. CI can supply deployment settings, while runtime values and secrets can live in AWS configuration and secret services. That can allow some runtime config changes without redeploying, depending on how the application retrieves it.

Environment variables can still be useful for passing a lookup name, without turning them into a long list of configuration values.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions