cdk.context.json: vpc lookup which depends on an ssm lookup fails
See original GitHub issue-
I’m submitting a …
- 🪲 bug report
-
What is the current behavior? If the current behavior is a 🪲bug🪲: Please provide the steps to reproduce In order to use the pre-existing VPC, as I understand it, I need to use
aws_ec2.Vpc.from_lookup
to find the VPC by an attribute (eg. its ID). For that to work, I need the VPC ID at synth time.
To do this, within my stack constructor method, I have:
self.vpc_id_lookup = aws_ssm.StringParameter.value_from_lookup(self, '/path/to/vpc_id')
self.vpc = aws_ec2.Vpc.from_lookup(
self,
'FoundVpc',
vpc_id=self.vpc_id_lookup
)
self.rds_cluster = aws_rds.DatabaseCluster(
self,
'AuroraCluster',
cluster_identifier='test-aurora-cluster',
engine=aws_rds.DatabaseClusterEngine.AURORA_POSTGRESQL,
master_user=aws_rds.Login(username='master'),
instance_props=aws_rds.InstanceProps(
vpc=self.vpc,
instance_type=aws_ec2.InstanceType(instance_type_identifier='m5.large'),
vpc_subnets=aws_ec2.SubnetSelection(subnet_type=ec2.SubnetType.ISOLATED),
)
)
The problem is that when cdk synth
is attempted on the above, it fails at the second action (vpc lookup) fails because the vpc ID doesn’t already exist in cdk.context.json… the only way to get it written there is to comment out the second and third actions, run cdk synth, which adds the SSM parameter store value to the context. After that, it will succeed on step two, but fail on step 3, because the VPC information is also missing from the context. If I comment out the 3rd action, run cdk synth, then put the 3rd action back, it all works.
- What is the expected behavior (or behavior of feature suggested)?
Something seems wrong here… it seems like the two lookup methods are not behaving idempotently. They should populate context if context is missing, but also always return the looked up value(s), so that this will work whether or not context is already populated. I do have the account and region specified in the stack ‘env’.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
We need that behavior anyway to properly handle context lookups for multiple accounts in the same app.
@eladb I am having the same issue but I have the vpc id hardcoded and not doing any ssm lookups.
The issue only exists when there are other constructs that try to use the vpc from lookup and there is no cdk.context.json file. To make it work we have to comment out everything apart from the vpc lookup, run cdk synth which then generates the cdk.context.json file. Then uncomment everything else, it’s only then the other constructs can use the vpc from lookup.
using cdk 1.4.0