Hey Guys, I have an issue with Pulumi on Golang. I...
# golang
p
Hey Guys, I have an issue with Pulumi on Golang. I am trying to create a new EKS Cluster, and pass the
clusterArgs
the subnet ID’s of the VPC I have created in a different stack. I am getting the output from the stack reference like this:
Copy code
stack, err := pulumi.NewStackReference(ctx, Name, nil)
if err != nil {
   log.Fatalf("Got error while trying to get a new stack reference! Error: %s", err)
}
VpcPublicSubnetIdsOutput := stack.GetStringOutput(pulumi.String("vpcPublicSubnetIds"))
vpcPrivateSubnetIdsOutput := stack.GetStringOutput(pulumi.String("vpcPrivateSubnetIds"))

VpcPublicSubnetIds := pulumi.ToStringArrayOutput([]pulumi.StringOutput{VpcPublicSubnetIdsOutput})
vpcPrivateSubnetIds := pulumi.ToStringArrayOutput([]pulumi.StringOutput{vpcPrivateSubnetIdsOutput})
So VpcPublicSubnetIds & vpcPrivateSubnetIds are both of type
pulumi.StringArrayOutput
But I can’t figure out how to pass the subnet ID’s to the clusterArgs, by indexing the elements from VpcPublicSubnetIds & vpcPrivateSubnetIds ! I tried doing it like this:
Copy code
eksCluster, err := eks.NewCluster(ctx, values.Name, &eks.ClusterArgs{
   Name:  pulumi.StringPtr(values.Name),
   VpcId: vpcId,
   PublicSubnetIds: pulumi.StringArray{
      pulumi.StringInput(VpcPublicSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(0))),
      pulumi.StringInput(VpcPublicSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(1))),
      pulumi.StringInput(VpcPublicSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(2))),
      pulumi.StringInput(VpcPublicSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(3))),
   },
   PrivateSubnetIds: pulumi.StringArray{
      pulumi.StringInput(vpcPrivateSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(0))),
      pulumi.StringInput(vpcPrivateSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(1))),
      pulumi.StringInput(vpcPrivateSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(2))),
      pulumi.StringInput(vpcPrivateSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(3))),
   },
what am I doing wrong?
b
you can’t index
StringArrayOutput
until you’ve resolved it inside an
ApplyT
in your case though, you should be able to just pass the
VpcPublicSubnetIds
directly to
PublicSubnetIds
p
Thanks for the reply @billowy-army-68599 When I tried passing it directly to
PublicSubnetIds
I get the following error:
Copy code
error: program failed: waiting for RPCs: marshaling properties: awaiting input property privateSubnetIds: failed to convert []interface {} to string
    exit status 1
b
can you show me what you’re trying to do?
p
yep, that’s my code:
Copy code
stack, err := pulumi.NewStackReference(ctx, name, nil)
	if err != nil {
		log.Fatalf("Got error while trying to get a new stack reference! Error: %s", err)
	}
	vpcId := stack.GetStringOutput(pulumi.String("vpcId"))

	VpcPublicSubnetIdsOutput := stack.GetStringOutput(pulumi.String("vpcPublicSubnetIds"))
	vpcPrivateSubnetIdsOutput := stack.GetStringOutput(pulumi.String("vpcPrivateSubnetIds"))

	VpcPublicSubnetIds := pulumi.ToStringArrayOutput([]pulumi.StringOutput{VpcPublicSubnetIdsOutput})
	vpcPrivateSubnetIds := pulumi.ToStringArrayOutput([]pulumi.StringOutput{vpcPrivateSubnetIdsOutput})
	
	eksCluster, err := eks.NewCluster(ctx, values.Name, &eks.ClusterArgs{
	Name:             pulumi.StringPtr(values.Name),
	VpcId:            vpcId,
	PublicSubnetIds:  VpcPublicSubnetIds,
	PrivateSubnetIds: vpcPrivateSubnetIds,
	})
@billowy-army-68599
I have tried using applyT as well, like so:
Copy code
func applyStringArray(stack *pulumi.StackReference, output string) pulumi.StringArrayOutput {
   subnet := stack.GetOutput(pulumi.String(output)).ApplyT(func(x interface{}) []string {
      y := x.([]interface{})
      r := make([]string, 0)
      for _, item := range y {
         r = append(r, item.(string))
      }
      return r
   }).(pulumi.StringArrayOutput)
   return subnet
}

VpcPublicSubnetIds := applyStringArray(stackReference, "vpcPublicSubnetIds")
vpcPrivateSubnetIds := applyStringArray(stackReference, "vpcPrivateSubnetIds")
and pass those two variables to the EKSArgs , but no luck as well
b
I’m sort of surprised, I’d expect that to work https://github.com/jaxxstorm/iac-in-go/blob/master/eks/main.go#L142
p
@billowy-army-68599 Maybe it’s because I am using a different package I am using
<http://github.com/pulumi/pulumi-eks/sdk/go/eks|github.com/pulumi/pulumi-eks/sdk/go/eks>
Instead of
<http://github.com/pulumi/pulumi-aws/sdk/v2/go/aws/eks|github.com/pulumi/pulumi-aws/sdk/v2/go/aws/eks>
that you showed (I picked that package since I want to use the
NodeGroupOptions
option for a managed node grouyp, that the other package is missing
Hard coding the values works btw:
Copy code
PublicSubnetIds: pulumi.StringArray{
   pulumi.StringInput(pulumi.String("subnet-123")),
   pulumi.StringInput(pulumi.String("subnet-345")),
   pulumi.StringInput(pulumi.String("subnet-456")),
   pulumi.StringInput(pulumi.String("subnet-0789")),
},
I just need to figure out how to assign it from a stack reference output
b
@prehistoric-sandwich-7272 can you check one thing quickly, when you grab the stack reference value (ie:
VpcPublicSubnetIdsOutput
) it’s of type
pulumi.AnyOutput
?
I’m also struggling with this while trying to repro, so going to chase it down
p
Ok I swear that I have tried the exact thing a couple of hours ago, but suddenly doing:
Copy code
PublicSubnetIds:  pulumi.StringArrayOutput(stackReference.GetOutput(pulumi.String("vpcPublicSubnet"))),
PrivateSubnetIds: pulumi.StringArrayOutput(stackReference.GetOutput(pulumi.String("vpcPrivateSubnet"))),
worked!! I have no idea why, I even went and exported all the subnets from the vpc stack one by one - ie:
ctx.Export("vpcPublicSubnet1", vpc.PublicSubnetIds.Index(<http://pulumi.Int|pulumi.Int>(0)))
and so on And updated the VPC stack came back to the EKS and the above method worked all of a sudden.... maybe there was a bug in the export? maybe it needed a refresh? I don’t know - after two days of debugging I feels ridiculous.. but i’ll take it 😅
b
great news!
p
Ok never mind the said method doesn’t work....
Copy code
2022/07/10 19:31:30 map[org:****]
    error: program failed: waiting for RPCs: rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:59633->127.0.0.1:59632: use of closed network connection
    exit status 1
going back to exporting them one by one
I think it’s good enough for now, thanks for the help anyway @billowy-army-68599!