Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get existing VPC for use within a Pulumi stack

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.

like image 869
Michael Robinson Avatar asked Oct 31 '25 05:10

Michael Robinson


2 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 });
like image 84
Mikhail Shilkov Avatar answered Nov 02 '25 18:11

Mikhail Shilkov


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
})
like image 45
Michael Robinson Avatar answered Nov 02 '25 17:11

Michael Robinson