Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import existing VPC in aws cdk?

Tags:

Hi I am working on aws cdk. 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='vpc-dev') 

This results in below error

[Error at /LocationCdkStack-cdkstack] Request has expired. [Warning at /LocationCdkStack-cdkstack/TaskDef/mw-service] Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'. Found errors 

Other method I tried is

vpc = ec2.Vpc.from_vpc_attributes(self, 'VPC', vpc_id='vpc-839227e7', availability_zones=['ap-southeast-2a','ap-southeast-2b','ap-southeast-2c']) 

This results in

[Error at /LocationCdkStack-cdkstack] Request has expired. [Warning at /LocationCdkStack-cdkstack/TaskDef/mw-service] Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'. Found errors 

Other method I tried is

vpc = ec2.Vpc.from_lookup(self, id = "VPC", is_default=True) // This will get default vpc and this will work

Can someone help me to get non-default vpc in aws cdk? Any help would be appreciated. Thanks

like image 263
Niranjan Avatar asked Dec 12 '19 09:12

Niranjan


People also ask

How do I import an existing VPC to CDK?

In order to import an existing VPC in CDK, we have to use the fromLookup static method on the Vpc construct. We have to explicitly set the stack environment (account, region), otherwise CDK doesn't know where to perform the lookup.

How do I find my AWS VPC?

You can view your default VPC and subnets using the Amazon VPC console or the command line. Open the Amazon VPC console at https://console.aws.amazon.com/vpc/ . In the navigation pane, choose Your VPCs. In the Default VPC column, look for a value of Yes.

What is CDK context JSON?

The project file cdk. context. json is where the AWS CDK caches context values retrieved from your AWS account. This practice avoids unexpected changes to your deployments when, for example, a new Availability Zone is introduced.


1 Answers

Take a look at aws_cdk.aws_ec2 documentation and at CDK Runtime Context.

If your VPC is created outside your CDK app, you can use Vpc.fromLookup(). The CDK CLI will search for the specified VPC in the the stack’s region and account, and import the subnet configuration. Looking up can be done by VPC ID, but more flexibly by searching for a specific tag on the VPC.

Usage:

# Example automatically generated. See https://github.com/aws/jsii/issues/826 from aws_cdk.core import App, Stack, Environment from aws_cdk import aws_ec2 as ec2  # Information from environment is used to get context information # so it has to be defined for the stack stack = MyStack(     app, "MyStack", env=Environment(account="account_id", region="region") )  # Retrieve VPC information vpc = ec2.Vpc.from_lookup(stack, "VPC",     # This imports the default VPC but you can also     # specify a 'vpcName' or 'tags'.     is_default=True ) 

Update with a relevant example:

vpc = ec2.Vpc.from_lookup(stack, "VPC",     vpc_id = VPC_ID ) 

Update with typescript example:

import ec2 = require('@aws-cdk/aws-ec2'); const getExistingVpc = ec2.Vpc.fromLookup(this, 'ImportVPC',{isDefault: false,vpcId: vpcId }); 

More info here.

like image 64
Amit Baranes Avatar answered Sep 19 '22 11:09

Amit Baranes