How to import AWS VPC with Pulumi CLI?
TypeScriptYou can import an existing VPC into a Pulumi program using the
get
function, provided by Pulumi AWS Provider. This will allow Pulumi to retrieve the state of the existing VPC without needing to manage it.Here's an example on how you might do this:
import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Create an AWS provider connection const provider = new aws.Provider("aws-provider", { region: "us-west-2", }); // Use the VPC ID to get the VPC details const vpcId = "your-vpc-id"; // replace with your VPC ID const vpc = aws.ec2.getVpc({ id: vpcId }, { provider }); // Export the details of the VPC export const vpcCidrBlock = vpc.then(v => v.cidrBlock);
This program uses
aws.ec2.getVpc
to get the VPC based on its ID. You'll need to replace"your-vpc-id"
with your actual VPC ID. Thevpc
constant will hold a promise which resolves to the VPC details. Note that the VPC details are not immediately available because they're fetched asynchronously. Thus, if you want to access these details (like thecidrBlock
), you need to usethen
function.More on aws.ec2.vpc in the Pulumi documentation
Once you run this program, you can retrieve the exported VPC CIDR block using the
pulumi stack output vpcCidrBlock
command in the CLI.