Skip to content

Instantly share code, notes, and snippets.

@statik
Created May 13, 2021 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statik/e41071a2da40c795bb73bc5ffff86a8e to your computer and use it in GitHub Desktop.
Save statik/e41071a2da40c795bb73bc5ffff86a8e to your computer and use it in GitHub Desktop.
setting up CDK context values to make stack synth more deterministic rather than relying on runtime lookups
// Given a pre-existing VPC network layout with stable ID, subnets, and routing tables
// this CDK setup code will populate the context necessary for lookups without going
// through synth and maintaining the context cache outside of version control
private setupFooVpcContext(): void {
// this is the CDK context info for the Demo VPC in the example account
const account = "1234"
const region = "us-west-2"
const tag = "MyDemoVPC"
const contextKey =
`vpc-provider:account=${account}:filter.tag:Name=${tag}:region=${region}:returnAsymmetricSubnets=true';
const contextData = {
vpcId: 'vpc-TODO',
availabilityZones: [],
subnetGroups: [
{
name: 'Public',
type: 'Public',
subnets: [
{
subnetId: 'subnet-TODO',
availabilityZone: 'us-west-2a',
routeTableId: 'rtb-TODO',
},
{
subnetId: 'subnet-TODO',
availabilityZone: 'us-west-2b',
routeTableId: 'rtb-TODO',
},
],
},
],
};
this.node.setContext(contextKey, contextData);
}
// then you can use code like
const vpc = Vpc.fromLookup(this, 'AVPC', {
vpcName: 'MyDemoVPC',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment