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.

Vpc.fromVpcAttributes throws Validation exception from ec2 construct

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
ayush987goyalcommented, Feb 18, 2020

Hi,

I tried the above implementation but I am not getting another error related to the same:

Cannot determine scope for context provider hosted-zone.
This usually happens when one or more of the provider props have unresolved tokens

The current implementation I have is

const vpc = ec2.Vpc.fromVpcAttributes(this, 'Vpc', {
      vpcId: cdk.Fn.importValue('VPC'),
      availabilityZones: [0, 1, 2].map(i => cdk.Fn.select(i, cdk.Fn.getAzs('us-east-1'))),
      privateSubnetIds: [1, 2, 3].map(i => cdk.Fn.importValue(`PrivateSubnet0${i}`)),
      publicSubnetIds: [1, 2, 3].map(i => cdk.Fn.importValue(`PublicSubnet0${i}`))
});
0reactions
rix0rrrcommented, Feb 18, 2020

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 and Fn.importValue() is evaluated by CFN (which happens later in time, so fromLookup() 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().

Read more comments on GitHub >

github_iconTop 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 >

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