0

I am trying to create an EC2 instance from a custom vpc. I have vpc as Output. But i want to assign to string. Would appreciate help in this context.

Option 1:

const vpcId = args.network.vpc.id.apply(id => id); //type Output<string>; network extends awsx:Vpc
const mySubnet = aws.ec2.gtSubnet({
  availabilityZone: 'ap-southeast-2a',
  filters: [
    { name: "tag:Name", values: ["*private-0"]}
  ],
  vpcId: vpcId; //Error: Type 'Output<string>' is not assignable to type 'string'
});
this.vm = new aws.ec2.Instance("ec2Instance", {
  ami: amiId,
  instanceType: instanceClass,
  networkInterfaces: [{
    networkInterfaceId: networkInterface.id,
    deviceIndex: 0,
  }]
});

Option 2:

const sg = new awsx.ec2.SecurityGroup("webserver-sg", { vpc }); //Here again I need vpcid string in the SecurityGroupArgs
Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99

1 Answers1

0

Output<T> is a promise, also called a future. It's a value that's not yet available. It will be available when the Pulumi engine starts executing. You can pass a callback which will be run when the value is available (and you'll immediately get a new promise for the new, not-yet-available return value of the callback) or you can pass it to methods which will accept a promise (these usually take Input<T>).

Unfortunately, getSubnet (currently) does not accept Input or Promise, so option #2 is out.

What you can do is:

const mySubnetId = args.network.vpc.id.apply(id => {
  const getSubnetResult = aws.ec2.getSubnet({
     vpcId: id;
  })
  return getSubnetResult.id
})

This will give you an Output<string> which you can pass to new Instance({subnetId: ...}) because it accepts an Input<string>.

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99