So I'm just getting started with CDK using Java & I'd like to find how to extract context info from the cdk.context.json. Essentially looking to hold parameters externally from the Stack relative to the environment (dev, test etc). So will be looking at incorporating into a pipeline (probably Gitlab) so the cdk.context.json will be version controlled. For instance, a cut of my context is as follows;
{
"vpc-provider:account=999999999:filter.isDefault=true:filter.vpc-id=vpc-w4w4w4w4w4:region=eu-west-2:returnAsymmetricSubnets=true": {
"vpcId": "vpc-w4w4w4w4w4",
.......
"environments": {
"dev": {
"documentDb": [
{
"port": "27017"
}
],
"subnetGroups": [
{
"name": "Private",
"type": "Private",
"subnets": [
{
"cidr": "10.0.1.0/24/24",
"availabilityZone": "eu-west-2a"
},
{
"cidr": "10.0.2.0/24",
"availabilityZone": "eu-west-2b"
}
]
}
]
},
"prod": {
"documentDb": [
{
"port": "27018"
}
],
"subnetGroups": [
{
"name": "Private",
"type": "Private",
"subnets": [
{
"cidr": "20.0.1.0/24/24",
"availabilityZone": "eu-west-2a"
},
{
"cidr": "20.0.2.0/24",
"availabilityZone": "eu-west-2b"
}
]
}
]
}
}
}
I'd like to extract the values for dev --> documentDb --> port for instance in the most elegant CDK way. If in my Stack I use;
this.getNode().tryGetContext("environments")
I am returned the whole JSON block;
{dev={documentDb=[{port=27017], subnetGroups=[{name=Private, type=Private, subnets=[{cidr=10.0.1.0/24/24, availabilityZone=eu-west-2a}, {cidr=10.0.2.0/24, availabilityZone=eu-west-2b}]}]}, prod={documentDb=[{port=27018], subnetGroups=..............
& not sure how to progress up the tree. If I synch passing in the config;
cdk synth -c config=environments > target/DocumentDbAppStack.yaml
& in my Stack;
this.getNode().tryGetContext("config")
I get "environments".
I can parse the LinkedHashMap using a JSON parser but that's obviously the wrong approach. I have looked at loads of examples / AWS documentation etc but can't seem to find the answer. There seems to be a wealth of info using Typescript (think that was the first language used for CDK), but I've never used it.
Thanks in advance.
Detailed above in description.