Search code examples
amazon-web-servicesinfrastructure-as-codepulumi

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.


Solution

  • 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
    })