Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using json instead of a string as context in cdk.json

See original GitHub issue

❓ General Issue

The Question

Hello, I’m trying to setup one cdk,json which will provide parameters for all DEV, QA and PROD environments. My cdk.json looks like this:

"context": {
   "appName": "app1",
   "ENV:TEST": {
          "vpcId": "vpc-111111111111"
   "ENV:QA": {
          "vpcId": "vpc-222222222222"
   "ENV:PROD": {
          "vpcId": "vpc-333333333333"

I have it working, but every time i run “cdk synth”, all “ENV:XXX” entries get removed from cdk.json. After running cdk synth my cdk.json looks like this:

"context": {
   "appName": "app1"

All values do persist in the cdk context though. Is this by design?

The problem im trying to solve is to have one repository, which can be deployed to a any environment by say doing - cdk deploy -c "ENV:QA".



  • CDK CLI Version: 1.3.0 (build bba9914)
  • Module Version:
  • OS: Windows 10
  • Language: TypeScript

Other information

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

jgondroncommented, Oct 30, 2019

I think this should work, although this will break some of the cli options for managing context since cdk currently doesn’t really support this. In your case, I think you would need to do something like cdk deploy -c env="QA", then in code use that to look up the correct context object, ex:

const targetEnv = app.node.tryGetContext('env');
const env = app.node.tryGetContext(`ENV:${targetEnv}`);
const vpcId = env.vpcId;

IMO, it would be nice if there were an option similar to -c that would allow passing a json file to specify context for that specific deploy event. That way you could just have three separate files with similar structure, and not really have to do the extra gymnastics to process the target env inside of your code.

Ie, instead of the code above, you would be able to do something like cdk deploy --context-file env/qa.json where env/qa.json is something like:

  "appName": "app1",
  "vpcId": "vpc-222222222222"

then the code above is simplified to just:

const vpcId = app.node.tryGetContext('vpcId');

Doing this would also better support the use case of multiple devs working on similar parts of an application within the same account. A dev could have a personal file used to deploy their own dev env during iteration and exclude it from the repo.

drissamricommented, Aug 1, 2020

Is there a cleaner way to do this in meanwhile? It seems like a basic requirement for anything over POC’s/demos.

In our case we are deploying from Jenkins/GitLab and we have an AWS account (with different params) per environment so it should be easy to pass these in.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Runtime context - AWS Cloud Development Kit (AWS CDK) v2
Automatically from the current AWS account. · Through the --context option to the cdk command. (These values are always strings.) · In the...
Read more >
How to use Context in AWS CDK | bobbyhadz
Let's take a quick look at how CDK uses context to cache certain values that belong to our deployment environment. If we open...
Read more >
4 Methods to configure multiple environments in the AWS CDK
We will first look at using the context variables in the cdk.json file, then move those same variables out to YAML files.
Read more >
Pass CDK context values per deployment environment
json that may become very cluttered with larger stacks and multiple environments. If you go this route then you can have your Prod...
Read more >
How To Statically Type CDK Configurations For Multiple ...
In this example, we'll use a combination of the cdk.json file and the —context or -c cli flag to pass in our runtime...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found