Get existing VPC for use within a Pulumi stack

Viewed 1273

I'm trying to use Pulumi within a somewhat restricted AWS environment.

This sandbox requires that I use a specific VPC, and there is no default VPC.

I have tried the examples showing how to reference an existing VPC, but they all fail with some variant of "invoking aws:ec2/getVpc:getVpc: no matching VPC found"

@pulumi/awsx, using code referenced from: https://github.com/pulumi/pulumi-awsx/issues/522:

const vpc = awsx.ec2.Vpc.fromExistingIds('name', {
  vpcId: 'id',
  publicSubnetIds: ['a', 'b'],
  privateSubnetIds: ['a', 'b']
})

@pulumi/aws, using code referenced from https://www.pulumi.com/docs/reference/pkg/aws/ec2/getvpc/:

const vpc = aws.ec2.Vpc.get('vpc-1', 'vpc-1')

Question: what is the correct and complete syntax for referencing an existing VPC within a Pulumi stack?

Note that I would rather not "adopt" this resource as it is shared and the user running the pulumi up command does not have permission to delete VPC resources.

3 Answers

There is a subtle difference between getVpc() that you linked to and Vpc.get() that you tried using. You should use the former:

const vpc = aws.ec2.getVpc({ id: yourVpcId });

This is what worked in the end:

const vpc = aws.ec2.Vpc.get('vpc-123', 'vpc-123')

I don't think I had saved my file correctly before pulumi up after making the above change.

Note that I also had to add subnets manually to my ALB to get this working, as below:

const vpc = aws.ec2.Vpc.get('vpc-123', 'vpc-123')

const clusterName = nameResource('graphQlServiceCluster')
const ecsCluster = new awsx.ecs.Cluster(clusterName, {
  name: clusterName,
  vpc
})

const PublicSubnet1a = 'subnet-123'
const PublicSubnet1b = 'subnet-123'

const alb = new awsx.lb.ApplicationLoadBalancer(nameResource('graphQlServiceElb'), {
  name: nameResource('graphQlServiceElb'),
  external: true,
  vpc,
  subnets: [
    PublicSubnet1a,
    PublicSubnet1b

  ]
})
const listener = alb.createListener(nameResource('graphqlServiceListener'), {
  name: nameResource('graphqlServiceListener'),
  port: 80,
  external: true,
  vpc
})

Pulumi has multiple Vpc types. You probably want to use the awsx VPC as it's higher level (and required to use other awsx infrastructure).

There's two ways to do this:

Creating a new VPC

const vpc = new awsx.ec2.Vpc(config.vpcName, {
  cidrBlock: "10.0.0.0/16",
  subnets: [
    {
      name: "public",
      type: "public",
      location: {
        cidrBlock: "10.0.0.0/24",
        availabilityZone: "us-east-2a",
      },
    },
    {
      name: "private-a",
      type: "private",
      location: {
        cidrBlock: "10.0.1.0/24",
        availabilityZone: "us-east-2a",
      },
    },
    {
      name: "private-b",
      type: "private",
      location: {
        cidrBlock: "10.0.2.0/24",
        availabilityZone: "us-east-2b",
      },
    },
  ],
});

Using an existing VPC

Borrowing from this GitHub thread with the Pulumi CTO produced a correct result:

  const vpc = awsx.ec2.Vpc.fromExistingIds("mycompany", {
    vpcId: "vpc-myvpcid",
  });

  // Create an ECS Fargate cluster.
  const ecsCluster = new awsx.ecs.Cluster("mycompany-pulumi-cluster", {
    vpc,
  });
Related