banner

KuchBhiLearning - A free website to learn and code

This is a good learning site. This contains details of cloud computing, AWS, AWS-CDK, AWS-SDK codes and examples including S3, Redis, lambda, api-gateway, cloudfront, cloudformation.

 Import existing VPC in AWS-CDK


CDK provides lot of methods which can be used create new resources and then use that reference for creating/updating another resource.

But there are wide use cases where some of the resources are already created and we just need to import it for another resource.

One such example is VPC, we do not need to create multiple VPC's. Having a single VPC can also does the job pretty well. In this case we do not want to create VPC through CDK.

Another such example is we already have infrastructure, now we are just migrating to CDK, and we know that VPC will never be changed. So we decided not to create it through CDK and just import the details and use it in our other resource creation.

CDK does provide an option to do this.

import * as ec2 from 'aws-cdk-lib/aws-ec2';

  const vpc = ec2.Vpc.fromLookup(stack, 'Example VPC', {
    vpcName: `VpcName`,
  });

With just simple code we can import the VPC details and use it for other resources.

No comments:

Post a Comment

If you have any doubts, Please let me know

Copyright 2022, KuchBhiLearning - A free website to learn and code. All rights Reserved.
| Designed by Yaseen Shariff