https://pulumi.com logo
#aws
Title
s

stale-secretary-86178

09/18/2023, 2:37 PM
Hi, I am using crosswalk components to create infrastructure on AWS. Is it possible to import these components (mainly awsx.ec2.VPC) to another project?
m

millions-furniture-75402

09/18/2023, 3:11 PM
If you export the values you'll need to recreate it, absolutely. Example:
Copy code
import * as awsxClassic from "@pulumi/awsx/classic";
import { ComponentResourceOptions, Input, all } from "@pulumi/pulumi";

export interface BaseArgs {
  tags?: aws.Tags;
}

export class BaseComponentResource extends ComponentResource {
  private readonly name: string;

  public getName(): string {
    return this.name;
  }

  constructor(type: string, name: string, args: BaseArgs, opts?: ComponentResourceOptions) {
    super(type, name, args, opts);

    this.name = name;
  }
}

export interface ExistingVpcArgs extends BaseArgs {
  privateSubnetIds: Input<Input<string>[]>;
  publicSubnetIds: Input<Input<string>[]>;
  vpcId: Input<string>;
}

export class ExistingVpc extends BaseComponentResource {
  vpc: any;

  constructor(name: string, args: ExistingVpcArgs, opts?: ComponentResourceOptions) {
    super("shared-vpc", name, args, opts);

    this.vpc = all([args.publicSubnetIds, args.privateSubnetIds, args.vpcId]).apply(
      ([publicSubnetIds, privateSubnetIds, vpcId]) => {
        return awsxClassic.ec2.Vpc.fromExistingIds(this.getName(), {
          vpcId,
          publicSubnetIds,
          privateSubnetIds,
        });
      },
    );
  }
}
usage:
Copy code
const vpc = new sansVpc.ExistingVpc("default-vpc", {
  privateSubnetIds,
  publicSubnetIds,
  vpcId
});
So if I create the VPC in my
shared-infrastructure
stack, I can
export
my
privateSubnetIds
,
publicSubnetIds
and
vpcId
, and use StackReferences to get the values in the "child" stack, and the custom component to simplify the getting of the resource.
s

stale-secretary-86178

09/18/2023, 6:08 PM
So I would have to use the classic package even if I used the 1.0 to create the resources? How would modifying the VPC after look like? (if I wanted to add a public subnet for example)
m

millions-furniture-75402

09/18/2023, 6:19 PM
No, you should use the latest if you can. I was just showing you old code to illustrate the point
s

stale-secretary-86178

09/18/2023, 6:35 PM
But stack references don't import the resources right? They might as well be stack inputs (the vpcId for example). I have something like this in the original codebase:
Copy code
new MegaComponent()
which does a bunch of stuff - creates VPC, ecs cluster, database, etc. Now I would like to untangle this, so that VPC stuff would go into a separate pulumi project/stack/whatever you call it. In pulumi aws package there is the
import
option, but here it is missing. In 1.0 there isn't even the
.fromExistingIds
. I tried exporting and importing the stack, but the urn's are messed up. Is what I am attempting even possible? Should I recreate the state by hand?
m

millions-furniture-75402

09/18/2023, 7:23 PM
Here is a much more detailed overview of how to utilize Stack References and organize your projects with a "shared resources" pattern https://www.pulumi.com/blog/iac-recommended-practices-using-stack-references/
s

stale-secretary-86178

09/18/2023, 10:27 PM
I don't have an issue with multiple stacks. 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?
m

millions-furniture-75402

09/19/2023, 12:58 PM
Have you already tried
import
in the
opts
?
s

stale-secretary-86178

09/19/2023, 12:59 PM
this option does not exist for awsx version of VPC
m

millions-furniture-75402

09/19/2023, 12:59 PM
Maybe manually editing the state? 😕
s

stale-secretary-86178

09/19/2023, 1:00 PM
Yeah, that is what I'm currently trying to do... untangle the component out of the current state by hand... this feels super hacky, but wcyd...
m

millions-furniture-75402

09/19/2023, 1:01 PM
how about if you use this?
Copy code
const defaultVpc = new aws.ec2.Vpc("defaultVpc", {}, {
    import: "<vpc-id>",
});
s

stale-secretary-86178

09/19/2023, 1:01 PM
I would have to also add all subnets, route tables, associations, etc by hand
m

millions-furniture-75402

09/19/2023, 1:02 PM
Yeah, I feel you.
awsx
and VPCs have been a major pain point for me in my projects as well 😞
That's why we've insulated ourselves a little with that custom component, but the experience as a user is still very frustrating.
Okay, another thing worth mentioning that I use to coerce LoadBalancers... All (or most) awsx components take the aws resource as a property. So if you declare your underlying defaultVpc with the import, then pass that VPC as a property to the awsx.ec2.Vpc declaration, maybe it will work?
That might be advice for the classic package 😞
s

stale-secretary-86178

09/20/2023, 6:52 AM
Yeah, awsx is super cool to set up things, but it is a pain to setup existing components with it... I'll create an issue on github and see what can be done about that. Thanks for all the help!
m

millions-furniture-75402

09/20/2023, 1:16 PM
There might be a path using classic to get it into your other stack using what I suggested, then once it's in that new stack, upgrade AWSX.
s

stale-secretary-86178

09/20/2023, 1:24 PM
But then the URNs will be wrong, which would render it basically impossible to upgrade.
This is the one thing I dislike about pulumi - it doesn't only care about the resources on aws, but it cares about the hierarchy of components. If you change the hierarchy, you basically have to recreate everything
m

millions-furniture-75402

09/20/2023, 2:07 PM
Maybe you can overcome that with aliases?
s

stale-secretary-86178

09/20/2023, 2:07 PM
aliases?
m

millions-furniture-75402

09/20/2023, 2:11 PM
s

stale-secretary-86178

09/20/2023, 2:13 PM
But I can't alias the individual resources if the VPC creates a whole bunch of them.
it will only alias the parent vpc, or am I missing something?
m

millions-furniture-75402

09/20/2023, 2:14 PM
Ahhh, I see what you mean now. Maybe? I'm not sure. Someone else might know the answer.
s

stale-secretary-86178

09/20/2023, 2:15 PM
tnx anyway, got another tool in the toolbox now 😄
m

millions-furniture-75402

09/20/2023, 2:15 PM
This pain point is what made me divest from Custom Resource Components early on..
s

stale-secretary-86178

09/20/2023, 2:20 PM
Yeah, I'm refactoring our internal custom components to just awsx, but even this seems to be a pain to refactor if needed. I have opened github issue and I'll see what comes off that...
m

millions-furniture-75402

09/20/2023, 2:21 PM
We've moved to using the AWS provider for the majority of cases, except a few.
We're still stuck on Classic fwiw.