Skip to content

Instantly share code, notes, and snippets.

@serverhorror
Forked from cnunciato/main.go
Last active January 12, 2023 20:58
Show Gist options
  • Save serverhorror/710a8bc35e40fa4dc084b2003e3ea8dd to your computer and use it in GitHub Desktop.
Save serverhorror/710a8bc35e40fa4dc084b2003e3ea8dd to your computer and use it in GitHub Desktop.
Converting a pulumi.IDOutput to a string and using it in a lookup function
package main
import (
"fmt"
"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ec2"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
vpc, err := ec2.NewVpc(ctx, "vpc", &ec2.VpcArgs{
CidrBlock: pulumi.String("10.0.0.0/16"),
EnableDnsHostnames: pulumi.Bool(true),
EnableDnsSupport: pulumi.Bool(true),
})
if err != nil {
return err
}
// Convert the ID to an output of string.
vpcID := vpc.ID().ToStringOutput()
// Use the string output to look up the associated route table.
// NOTE:
// We will not be able to create a variable outside, assign inside
// and use it later.
// This is the case because `Apply` does **not** block!
// We will always end up with an empty or nil value!
_ = vpcID.ApplyT(func(id string) string {
routeTableResult, err := ec2.LookupRouteTable(ctx, &ec2.LookupRouteTableArgs{
VpcId: &id,
Filters: []ec2.GetRouteTableFilter{
{
Name: "vpc-id",
Values: []string{id},
},
{
Name: "association.main",
Values: []string{"true"},
},
},
})
if err != nil {
return ""
}
// Log the looked-up route table ID.
fmt.Printf("%s", routeTableResult.RouteTableId)
return id
}).(pulumi.StringOutput)
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment