My issue is on how to split the existing stack into multiple and keep the same resources allocated on AWS. The VPC.fromExistingIds does not import the resource (from my understanding). Is there a way to import the awsx.ec2.VPC component or is this impossible ATM?
TypeScriptSure, the
VPC.fromExistingIds
method fromawsx
gets metadata for existing infrastructure, but it doesn't "import" it in the sense of managing changes to it.For the purpose of "splitting a stack while maintaining existing AWS resources", we can work with the
pulumi.Import
function to manage existing AWS resources within Pulumi. For VPC, we will use theaws.ec2.Vpc
type from@pulumi/aws
.The program below will accomplish your goal, please fill in your specific
vpc-id
:import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Import an existing VPC const defaultVpc = new aws.ec2.Vpc("defaultVpc", {}, { import: "<vpc-id>", }); // Export the VPC's ID and ARN export const vpcId = defaultVpc.id; export const vpcArn = defaultVpc.arn;
- It first imports your existing resource.
- Exports the Resource ID and ARN so you can consume it in other parts of your pulumi program or other pulumi programs.
Remember, when you import a resource, Pulumi takes under management an existing resource that someone else might have created (manually or by another program). So be careful, any further changes to this program will now be applied to the managed resource.
You can read more about the
aws.ec2.Vpc
resource here.Move a VPC declaration from one stack to another by Importng a VPC using AWSX classic in the new stack, and redeclaring the aws.ec2.Vpc
TypeScriptBased on your goal, we'll import the VPC using
awsx.ec2.Vpc
and redeclare it withaws.ec2.Vpc
.Here is your updated Pulumi Program:
import * as aws from "@pulumi/aws"; import * as awsx from "@pulumi/awsx"; // Import an existing VPC using awsx.ec2.Vpc let defaultVpcAwsx = awsx.ec2.Vpc.fromExistingId("defaultVpc", "<vpc-id>"); // Declare aws.ec2.Vpc using imported awsx.ec2.Vpc const defaultVpc = new aws.ec2.Vpc("defaultVpc", { cidrBlock: defaultVpcAwsx.vpc.cidrBlock, enableDnsHostnames: defaultVpcAwsx.vpc.enableDnsHostnames, enableDnsSupport: defaultVpcAwsx.vpc.enableDnsSupport, tags: defaultVpcAwsx.vpc.tags, }); // Export the VPC's ID and ARN export const vpcId = defaultVpc.id; export const vpcArn = defaultVpc.arn;
In this program, the awsx.ec2.Vpc.fromExistingId function is used to import a VPC from an existing AWS resource by its ID. The Pulumi aws.ec2.Vpc resource is then redeclared with the same ID and properties using aws.ec2.Vpc.
You can replace
"<vpc-id>"
with the actual VPC ID.Please note that the properties used in
aws.ec2.Vpc
are only an example, it might vary based on the real imported awsx resources. You would need to inspect properties ofdefaultVpcAwsx
(for e.g.defaultVpcAwsx.vpc.cidrBlock
) to see what properties are available to be used inaws.ec2.Vpc
, and set those accordingly.For additional details on the use of
awsx.ec2.Vpc
andaws.ec2.Vpc
, refer to their Pulumi Registry documentation at awsx/ec2.Vpc and aws/ec2.Vpc.Let me know if you need changes or have any questions.