I have inherited a small CDK project that features a cdk.context.json
file:
{
"vpc-provider:account=9015xxxxxxxx:filter.tag:Name=preprod-eu-west-1:region=eu-west-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-0d891xxxxxxxxxxxx",
"vpcCidrBlock": "172.35.0.0/16",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Private",
"type": "Private",
"subnets": [
{
"subnetId": "subnet-0ad04xxxxxxxxxxxx",
"cidr": "172.35.a.0/22",
"availabilityZone": "eu-west-1b",
"routeTableId": "rtb-0fee4xxxxxxxxxxxx"
},
{
"subnetId": "subnet-08598xxxxxxxxxxxx",
"cidr": "172.35.z.0/22",
"availabilityZone": "eu-west-1c",
"routeTableId": "rtb-0f477xxxxxxxxxxxx"
}
]
},
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-0fba3xxxxxxxxxxxx",
"cidr": "172.35.y.0/22",
"availabilityZone": "eu-west-1b",
"routeTableId": "rtb-02dfbxxxxxxxxxxxx"
},
{
"subnetId": "subnet-0a3b8xxxxxxxxxxxx",
"cidr": "172.35.x.0/22",
"availabilityZone": "eu-west-1c",
"routeTableId": "rtb-02dfbxxxxxxxxxxxx"
}
]
}
]
}
}
This works fine for the preprod environment it connects to, but I now need to deploy to production. I could generate the same file by hand, but this feels like a waste of time – I suspect this is a generated file.
I have tried cdk context --build
in the hope that it would produce this file, but it seems not – it appears just to echo the preprod one that already exists. What can I do to avoid having to hand-build this (and to avoid all the guessing which VPCs and subnets it wants)?
2
Answers
This file is auto generated per deployment. You should exclude this file from your code repo.
The file is generated for whatever environment your CDK stacks are being deployed to.
If you want to populate it with values for both your staging environment and production, you should simply run
cdk synth
after activating your production AWS credentials (using--profile
, for example). This will populate the context with new values (in addition to the ones that are already there), and you should commit these changes to VCS.It is advised to keep the file in your VCS specifically to avoid having it re-populated on each deployment. This makes your CDK app deterministic.
From the best practices:
A better way to organize this would be to make your stacks environment-specific – that is, to instantiate your top-level stack or stage once for each environment you want to deploy to, specifying the
env
prop. Deploying then becomescdk deploy MyStaginStack
andcdk deploy MyProductionStack
. Although this is off-topic for the question.