Skip to content

Instantly share code, notes, and snippets.

@cnunciato
Last active January 12, 2023 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cnunciato/cd2b7dc8b8312de0e43cf6d8fd2c8e9d to your computer and use it in GitHub Desktop.
Save cnunciato/cd2b7dc8b8312de0e43cf6d8fd2c8e9d 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().ApplyT(func(id interface{}) string {
return fmt.Sprintf("%s", id)
}).(pulumi.StringOutput)
// Use the string output to look up the associated route table.
_ = 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