When I create a NewVpc, how do I then get the ec2....
# general
l
When I create a NewVpc, how do I then get the ec2.RouteTable for the default Vpc route table?
m
You can also look up the default route table via https://www.pulumi.com/registry/packages/aws/api-docs/ec2/defaultroutetable/#look-up but have a look at the description at the top of the page first to see if this is what you need.
l
This is so frustrating, I have an ec2.Vpc and I want to simply read the default routing table, but I cannot get a pulumi.IDInput from the VPC Main/Default Route Table Ids
m
getRouteTable
takes the "regular" AWS RouteTable ID. You should even be able to pass a string that you copy from the AWS console.
l
ec2.GetRouteTable(ctx, “default-vpc-rt”, vpc.DefaultRouteTableId)
Why is that not possible? Am I fundamentally misunderstanding how pulumi works at runtime?
Is vpc.DefaultRouteTableId not the value you say I need from the AWS console?
m
Assuming you're writing Go,
ec2.GetRouteTable
is not the same as
ec2.LookupRouteTable
l
There is also
ec2.GetDefaultRouteTable
m
Just to confirm, you're writing Go, right?
l
Yes, in Go
m
OK. That's important here because the naming seems to be a bit special.
There are basically two kinds of "get" methods you have available in the AWS package: Every resource "AWSResource" has an associated "GetAWSResource" that allows you to get a read-only version of "AWSResource".
l
That makes sense
m
Then, some resources have a function which in most languages is called "getAWSResource" (small "g") but in your case, for the route table, is called LookupRouteTable.
This uses the AWS SDK under the hood to look up information. Thus, it does not need the provider ID but usually requires just enough information to find the resource you're asking for.
In your case,
Copy code
selected, err := ec2.LookupRouteTable(ctx, &ec2.LookupRouteTableArgs{
			RouteTableId: pulumi.String("the-id-of-your-route-table"),
		}, nil)
should return the route table you're asking about.
l
Okay that makes sense, I appreciate the explanation
The problem I have, is I have the vpc.ID() and vpc.DefaultRouteTableId
m
You should be able to replace the hard-coded string with the default route table ID output of your VPC resource.
l
But neither are *string and as such I can’t pass it into the lookup method
m
Copy code
selected, err := ec2.LookupRouteTable(ctx, &ec2.LookupRouteTableArgs{
			RouteTableId: pulumi.String(vpc.DefaultRouteTableId),
		}, nil)
l
image.png
m
If you look at the example at https://www.pulumi.com/registry/packages/aws/api-docs/ec2/getroutetable/#example-usage you see that the ec2.NewRoute gets
pulumi.String(selected.Id)
which is also an Output.
l
I think that example works because the type of subnetId can be converted to *string
But somehow I cannot get the string value of vpc.DefaultRouteTableId
This feels like a super basic use case, I must be doing something fundamentally wrong 🤔
m
I haven't written Pulumi in Go yet, but let me find an example. Using the outputs of one resource as inputs on another is a key feature of Pulumi.
l
I’m thinking it might be because those values aren’t resolved yet
As I understand it StringOutput is an async value that’s resolved at some point
Hence converting it to a string isn’t trivial because there’s not guarantee the value exists, maybe?
m
No, that's what I mean by "key feature" 🙂
l
Ah okay
m
Have a look at this example: https://github.com/pulumi/examples/blob/59abe05bce06aeea0edeb43c014d0a552ba19a63/aws-go-webserver/main.go#L27-L50 This looks up the AMI and then uses its ID as an input later.
Pulumi AI says to try this:
Copy code
defaultRouteTable, err := ec2.LookupRouteTable(ctx, &ec2.LookupRouteTableArgs{
			RouteTableId: vpc.DefaultRouteTableId,
		}, nil)
		if err != nil {
			return err
		}
l
If I do this, I get the correct resource id
Copy code
vpc.DefaultRouteTableId.ApplyT(func(id string) error {
        fmt.Printf("id is %s", id)

		return nil
	})
The question however is how do I get that id out of the apply callback
m
You don't. The callback currently returns "nil" and at most returns the same kind of Output that you had originally.
The question is how do you pass a StringOutput as an input to a Pulumi resource in Go.
l
I maybe worked this out
ApplyT can return either T or (T, error)
m
I'm pretty sure you don't need an apply in there. The result of an apply is also an output.
Another example:
Copy code
current, err := aws.GetRegion(ctx, &aws.GetRegionArgs{}, nil)
		if err != nil {
			return err
		}
		test, err := ec2.NewVpcIpam(ctx, "test", &ec2.VpcIpamArgs{
			OperatingRegions: ec2.VpcIpamOperatingRegionArray{
				&ec2.VpcIpamOperatingRegionArgs{
					RegionName: pulumi.String(current.Name),
				},
			},
		})
		if err != nil {
			return err
		}
		testVpcIpamPool, err := ec2.NewVpcIpamPool(ctx, "test", &ec2.VpcIpamPoolArgs{
			AddressFamily: pulumi.String("ipv4"),
			IpamScopeId:   test.PrivateDefaultScopeId,
			Locale:        pulumi.String(current.Name),
		})
		if err != nil {
			return err
		}
l
I think I can adjust the output type though by performing the action in the Apply with the ID as a string
m
That is correct, but a StringOutput should be compatible with a String input.
The example above is straight from https://www.pulumi.com/registry/packages/aws/api-docs/ec2/vpc/ and contains several examples of what you're trying to achieve.
l
Am I using an outdated version of something/
Hm, no I’m using the same deps as the examples
image.png
I think this is the fundamental issue, these two types are not convertible
m
Yes, and that should not be the case.
l
I just updated these to the latest version
No change 🤔
m
l
No errors
m
OK, then this is in your code somewhere. Can you verify that the types you see are the same?
Unfortunately, I can't do more than provide the hints I gave you and the examples. Hopefully someone familiar with Pulumi and Go has encountered this before and can point you in the right direction.
l
Yeah you’re right
I have this broken out into modules
If I move this into the func main
m
This might be super subtle, like missing a context or an & or something.
l
I don’t get the type conversion error
image.png
That works
So bizarre though because there’s no compiler errors anywhere
and it runs
m
Awesome! This looks like I'd expect it to look and work.
l
In any case, really appreciate the help apologies that it ended up being me 😅
I’ll have to step though from the main func and work out where things get confused
m
No worries, always happy to help, and I learned something as well 🙂
l
Hm, @modern-zebra-45309 it turns out it wasn’t me. I’m just looking at this again now, this code gives the same errors.
Copy code
package main

import (
	"<http://github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2|github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2>"
	"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		vpc, err := ec2.NewVpc(ctx, "my-project-vpc", &ec2.VpcArgs{
			CidrBlock: pulumi.String("10.0.0.0/16"),

			EnableDnsHostnames: pulumi.Bool(true),
			EnableDnsSupport:   pulumi.Bool(true),

			Tags: pulumi.StringMap{
				"Name": pulumi.String("my-project-vpc"),
			},
		})

		if err != nil {
			return err
		}

		drt, err := ec2.LookupRouteTable(ctx, &ec2.LookupRouteTableArgs{
			VpcId: pulumi.String(vpc.DefaultRouteTableId),
			RouteTableId: pulumi.String(vpc.DefaultRouteTableId),
		})

		if err != nil {
			return err
		}

		return nil
	})
}
image.png
I’m going slowly insane over this
This is also using the absolute most recent versions of pulumi and pulumi-aws?
Copy code
require (
	<http://github.com/pulumi/pulumi-aws/sdk/v6|github.com/pulumi/pulumi-aws/sdk/v6> v6.68.0
	<http://github.com/pulumi/pulumi/sdk/v3|github.com/pulumi/pulumi/sdk/v3> v3.150.0
)