https://pulumi.com logo
#typescript
Title
# typescript
f

fierce-cpu-94517

07/16/2019, 1:42 PM
Hi, I have the following aws vpc configuration:
Copy code
export const vpc = new awsx.ec2.Vpc("vpc", {
   numberOfAvailabilityZones: 3,
});
I'm struggling to find a way to grab the private subnet so that I can use it to create an EC2 instance. This is what I came to thus far:
Copy code
const privateSubnets = pulumi.output(vpc.privateSubnets).apply(
    subnets => subnets.filter(x => x.subnet.availabilityZone == pulumi.output("us-east-1a"))
);

export const server = new aws.ec2.Instance("server", {
    instanceType: aws.ec2.C5InstanceLarge,
    vpcSecurityGroupIds: [ vpc.sgInternal.id ],
    subnetId: privateSubnets.apply(x => x[0].id),
    ami: "ami-;02507631a9f7bc956",
});
I'm getting the following error:
Copy code
$ pulumi up
Previewing update (xxx):

     Type                 Name                      Plan     Info
     pulumi:pulumi:Stack  xxx           1 error; 5 messages
 
Diagnostics:
  pulumi:pulumi:Stack (xxx):
    TypeError: Cannot read property 'id' of undefined
        at exports.server.aws.ec2.Instance.subnetId.privateSubnets.apply.x (ec2.ts:24:46)
        at OutputImpl.<anonymous> (node_modules/@pulumi/pulumi/output.js:104:47)
        at Generator.next (<anonymous>)
        at fulfilled (node_modules/@pulumi/pulumi/output.js:17:58)
g

gentle-diamond-70147

07/16/2019, 3:32 PM
This should work for you:
Copy code
const server = new aws.ec2.Instance("web-server", {
    // ...
    subnetId: vpc.privateSubnetsIds[0], // reference the subnet created by awsx
});
f

fierce-cpu-94517

07/16/2019, 3:48 PM
Will index 0 always be
us-east-1a
?
g

gentle-diamond-70147

07/16/2019, 3:51 PM
I believe it will always be the first available zone found, but different accounts have different zones.
Not all accounts have an a to be more specific.
f

fierce-cpu-94517

07/16/2019, 3:58 PM
my issue here is that I have an EBS volume in AZ
us-east-1a
that I need to attach to the instance. that's why I used a filter on the subnet collection.
I tried to simplify my example, but what I actually have is:
Copy code
subnets => subnets.filter(x => x.subnet.availabilityZone == ebs.availabilityZone)
OK, I worked around this by selecting
vpc.privateSubnets[0].subnet.availabilityZone
when configuring the EBS volume AZ
👍 1
3 Views