question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rix0rrrcommented, Aug 23, 2019

We need that behavior anyway to properly handle context lookups for multiple accounts in the same app.

1reaction
ghostcommented, Aug 20, 2019

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting common AWS CDK issues
This topic describes how to troubleshoot the following issues with the AWS CDK.
Read more >
How to resolve “All arguments to Vpc.fromLookup() must be ...
This article walks you through a solution using SSM for this issue. ... valueFromLookup` and stored in `cdk.context.json` at `cdk synth`.
Read more >
@aws-cdk/aws-ec2 - npm
If your VPC is created outside your CDK app, you can use Vpc.fromLookup() . The CDK CLI will search for the specified VPC...
Read more >
Cdk use existing vpc - Seba Online
0 licence at our NPM packages aggregator and search engine. ... needs to create high level CDK constructs; and can use method of...
Read more >
Can the cdk.context.json file be auto-generated for a specific ...
The only changes in the new template are the changes you made in your code. When you use a construct's .fromLookup() method, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found