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".
Thanks!
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.
@jgondron , @markosha - were you able to get anywhere with this issue? Trying to do something similar and would like to know if you guys were able to figure out something. Thanks
@markosha was this resulting in your keys being moved from cdk.json to cdk.context.json ?
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
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.
@drissamri did @jgondron's PR fix your issue?
It looks like you can set environment params in your context json and then pass the environment you want on the command line, that would do it for me yes. I'll have to try to give it a go.
Closing this issue for now. Please tag me if the issue isn't fixed and you'd like me to reopen the issue.
Most helpful comment
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:IMO, it would be nice if there were an option similar to
-cthat 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.jsonwhereenv/qa.jsonis something like:then the code above is simplified to just:
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.