Vpc.fromVpcAttributes throws Validation exception from ec2 construct
See original GitHub issueWhen I import an existing VPC using this
const vpc = ec2.Vpc.fromVpcAttributes(this, 'Vpc', {
vpcId: cdk.Fn.importValue('VPC'),
availabilityZones: cdk.Fn.getAzs(),
privateSubnetIds: [1, 2, 3].map(i => cdk.Fn.importValue(`PrivateSubnet0${i}`)),
publicSubnetIds: [1, 2, 3].map(i => cdk.Fn.importValue(`PublicSubnet0${i}`))
});
I get the following error while synthesis:
There is already a Construct with name 'undefinedSubnet2' in ImportedVpc [Vpc]
/node_modules/@aws-cdk/core/lib/construct.ts:48
throw new Error(`Validation failed with the following errors:\n ${errorList}`);
^
What is the correct way to resolve this problem?
Reproduction Steps
Error Log
Environment
- **CLI Version :1.22.0
- **Framework Version:1.23.0
- OS :
- **Language :TS
Other
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
aws-cdk-lib.aws_ec2 module
Constructs using the VPC will "launch instances" (or more accurately, create Elastic Network Interfaces) into one or more of the subnets. They all...
Read more >awslabs/aws-cdk - Gitter
From what I have checked so far, the code below should work, but gives error on cdk synth. The code: let importVpc =...
Read more >How to import existing VPC in aws cdk? - Stack Overflow
If I set isDefault to false, it gives me the correct one, and if I set it to true, it throws and error...
Read more >@aws-cdk/assert | Yarn - Package Manager
Build Status Gitpod Ready-to-Code NPM version PyPI version NuGet version Maven ... The AWS Cloud Development Kit (AWS CDK) is an open-source software ......
Read more >Importing VPC IDs into a stack with CDK - DEV Community
When we want to import a VPC ID from another stack using CDK, not all methods will accept the... Tagged with aws, cdk,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi,
I tried the above implementation but I am not getting another error related to the same:
The current implementation I have is
Fairly sure that this has nothing to do with your VPC, given that the error message says “context provider hosted-zone”. You are probably doing
HostedZone.fromLookup(Fn.importValue(...))
or similar.That won’t work because
fromLookup()
is evaluated by CDK andFn.importValue()
is evaluated by CFN (which happens later in time, sofromLookup()
can never use the actual value CFN would have imported).See here for an explanation: https://docs.aws.amazon.com/cdk/latest/guide/apps.html#lifecycle
You need concrete parameters, not
Fn.importValues()
.