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.

Cannot find right method to import existing vpc with ec2.Vpc.fromLookup or ec2.Vpc.fromVpcAttributes

See original GitHub issue

Issue: I am trying to import an existing vpc to deploy few lambda, ecs and albs in private subnets. Running into issues when trying to import the vpc using ec2.Vpc.fromLookup or ec2.Vpc.fromVpcAttributes.

The existing vpcid, azs, privatesubnets are exported as dev-vpcId. - value: ‘vpc-xxxxx’ dev-azs - value: ‘us-east-1a,us-east-1b’ dev-privateSubnets - ‘subnet-xxxx,subnet-yyyy’

    const vpc = ec2.Vpc.fromVpcAttributes(this,'vpc',{
      vpcId: Fn.importValue(envName+'-vpcId'),
      availabilityZones: Fn.split(',',Fn.importValue(envName+'-azs')),
      privateSubnetIds: Fn.split(',',Fn.importValue(envName+'-privateSubnets')),
    })

Error

...../node_modules/@aws-cdk/core/lib/private/resolve.ts:94
      throw new Error('Found an encoded list token string in a scalar string context. Use \'Fn.select(0, list)\' (not \'list[0]\') to extract elements from token lists.');
            ^
Error: Found an encoded list token string in a scalar string context. Use 'Fn.select(0, list)' (not 'list[0]') to extract elements from token lists.

with ec2.Vpc.fromLookup() we cannot use import values

    const vpc = ec2.Vpc.fromLookup(this, 'vpc',{
      vpcId: Fn.importValue(envName+'-vpcId')
    })

Error

..../node_modules/@aws-cdk/aws-ec2/lib/vpc.ts:1040
      routerId: natGatewayId,
            ^
Error: All arguments to Vpc.fromLookup() must be concrete (no Tokens)

Please advise on the right method to import an existing vpc with details from the export values.


This is a 📕 documentation issue

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:27 (15 by maintainers)

github_iconTop GitHub Comments

5reactions
redbaroncommented, Dec 21, 2020

Fn.importValue executed during CloudFormation run , but ec2.Vpc.fromLookup runs before CF stack is submitted.

I’d try to give up Fn.importValue and hardcode map { dev: 'vpc-xxx', prod: 'vpc-yyyy' } and use these directly in ec2.Vpc.fromLookup

2reactions
pandrel-awscommented, Sep 7, 2021

@pandrel Have you found the fix for this problem. I am currently facing the same issue.

I am using the ec2.Vpc.fromLookup() with tags option to import an existing vpc.

example:

    const envName = 'dev' // passed from construct stack props but using fixed value for example
    const vpc = ec2.Vpc.fromLookup(this, 'vpc', {
      tags: { 'envName': envName}
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to import existing VPC in aws cdk? - Stack Overflow
I am trying to get existing non-default vpc. I tried below options. vpc = ec2.Vpc.from_lookup(self, id = "VPC", vpc_id='vpcid', vpc_name=' ...
Read more >
ec2.Vpc.fromLookup() - AWS Documentation - Amazon.com
No information is available for this page.
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 imported value. Errors can...
Read more >
awslabs/aws-cdk - Gitter
I looked back a ways but didn't see the answer: is there a roadmap or any projections for ... Import an exported VPC...
Read more >
aws-cdk.aws-ec2 - PyPI
Importing an existing VPC. If your VPC is created outside your CDK app, you can use Vpc.fromLookup() . The CDK CLI will search...
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