How to import existing VPC in aws cdk?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When you use AWS CDK with infrastructure that already exists, you usually do not recreate foundational network resources such as the VPC. Instead, you import the existing VPC into your stack so other constructs can attach to it.
In CDK, the two main approaches are Vpc.fromLookup and Vpc.fromVpcAttributes. fromLookup is the easiest when the VPC already exists in the target account and region, while fromVpcAttributes is better when you already know the IDs you want to supply explicitly.
Use Vpc.fromLookup for Existing Environments
fromLookup queries the AWS environment during synthesis and returns an IVpc that other constructs can use.
You can also look up by tags, name, or default VPC settings depending on what is stable in your environment.
Understand the Lookup Behavior
fromLookup happens at synthesis time, not deployment time. CDK stores the discovered values in context so later synth runs do not have to query AWS every time.
That means:
- your AWS account and region must be configured correctly when you run
cdk synthorcdk deploy - the lookup result may be cached in
cdk.context.json - changing the target VPC may require clearing or refreshing context
If the lookup seems stale, run:
Then synthesize again so CDK fetches fresh values.
This behavior surprises many people the first time they switch AWS profiles or regions, because the stack code did not change even though the environment did.
Use fromVpcAttributes When IDs Are Known
If you already know the VPC ID, subnet IDs, and availability zones, you can import them directly without a lookup:
This is useful in CI systems or multi-account setups where you want deterministic input values rather than live discovery.
What Importing a VPC Really Means
Importing does not put the VPC under CDK ownership. CDK can reference the VPC so other resources can be placed into it, but it will not start managing the VPC lifecycle itself.
For example, this is valid:
The EC2 instance is managed by CDK. The pre-existing VPC is not recreated or updated as part of that stack.
That separation is exactly what makes imports useful in brownfield AWS environments where networking is shared across many teams and stacks.
Common Pitfalls
- Assuming import means CDK now owns and manages the existing VPC. It only references it.
- Forgetting that
fromLookupdepends on the correct account and region during synthesis. - Being confused by stale context after a VPC change. Clear
cdk.context.jsonor refresh context. - Using
fromVpcAttributeswithout enough subnet information for downstream constructs. - Importing a VPC successfully but then selecting subnet types that do not actually exist in that environment.
Summary
- Use
Vpc.fromLookupwhen CDK can discover the existing VPC in the target environment. - Use
Vpc.fromVpcAttributeswhen you want to provide IDs explicitly. - Imported VPCs are referenced by CDK, not owned by CDK.
- Context caching is normal for lookups and may need to be refreshed.
- Once imported, the VPC can be used by security groups, instances, load balancers, and other constructs.
Related reading
- How to improve random number generation in kubernetes cluster containers?
- How to improve slow uploading to Amazon S3
- how to include and copy files that are in current directory to s3 and not recursively
- How to increase aws dynamodb index limit from 5
- How to increase AWS EBS NVME size
- How to increase the maximum size of the AWS lambda deployment package RequestEntityTooLargeException?
- How to insert to DynamoDb just if the key does not exist
- How to insert to DynamoDb just if the key does not exist

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.