Hi, I am using crosswalk components to create infr...
# aws
s
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
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
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
No, you should use the latest if you can. I was just showing you old code to illustrate the point
s
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
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
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
Have you already tried
import
in the
opts
?
s
this option does not exist for awsx version of VPC
m
Maybe manually editing the state? 😕
s
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
how about if you use this?
Copy code
const defaultVpc = new aws.ec2.Vpc("defaultVpc", {}, {
    import: "<vpc-id>",
});
s
I would have to also add all subnets, route tables, associations, etc by hand
m
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
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
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
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
Maybe you can overcome that with aliases?
s
aliases?
m
s
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
Ahhh, I see what you mean now. Maybe? I'm not sure. Someone else might know the answer.
s
tnx anyway, got another tool in the toolbox now 😄
m
This pain point is what made me divest from Custom Resource Components early on..
s
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
We've moved to using the AWS provider for the majority of cases, except a few.
We're still stuck on Classic fwiw.