When I try to lookup a vpc by name in my cdk codepipeline I keep getting vpc-12345 which doesnt exist:
var vpc = Vpc.FromLookup(this, canamVpc, new VpcLookupOptions()
{
VpcName = "MyVPC"
});
any idea how to find a vpc based on a name or tag? I don't want to hard code the vpc id in an from attributes search, id's change , names don't.
TL;DR Synth your app locally one time to populate the context cache. Commit the result.
FromLookup
is a CDK context method. Context methods make one cloud-side call and cache the results in cdk.context.json
. Subsequent calls read from the cache.
The CDK initializes the cache with dummy data like vpc-12345
. You are seeing the placeholder data because FromLookup
hasn't yet made the initial cloud-side call to get your real VPC info into the cache. This is because your CDK pipeline doesn't have permissions required for the API call.
There's a whole section about Context Lookups in the CDK pipeline docs. The recommended solution is simple:
Our recommended way of using lookups is by running cdk synth on the developer workstation and checking in the
cdk.context.json
file, which contains the results of the context lookups. This will make sure your synthesized infrastructure is consistent and repeatable.
"Deterministic deploys" that avoid the possible side-effects of "live", uncontrolled API calls is a CDK best practice.