I'm continuing to struggle with stack references. ...
# golang
s
I'm continuing to struggle with stack references. Following up on this thread (https://pulumi-community.slack.com/archives/CCWP5TJ5U/p1595004364263000), I've adopted the recommended
ApplyStringArray
and
ApplyString
functions to the outputs from the stack reference, but trying to create an instance using a security group and subnet from the stack reference fails. I guess it's defaulting to the first subnet in the default VPC and not using my subnet at all. Code in thread below.
I have a StackReference:
Copy code
ref, err := pulumi.NewStackReference(ctx, "user/project/stack", nil)
		if err != nil {
			return err
		}
Then I have this code to pull out the first subnet from an array of subnets created by the first stack:
Copy code
privSubnets := ref.GetOutput(pulumi.String("ref")).ApplyStringArray(func(out interface{}) []string {
			var res []string
			if out != nil {
				for _, v := range out.([]interface{}) {
					res = append(res, v.(string))
				}
			}
			return res
		})

		// Get the ID of the first public subnet
		privSubnet0 := privSubnets.ApplyString(func(a []string) string {
			var res string
			if len(a) > 0 {
				res = a[0]
			}
			return res
		})
When I try to use
privSubnet0
to create an instance, it must have a nil value because Pulumi tries to create the instance in a subnet in the default VPC (which is completely wrong). So what am I missing here?
l
what's the output of
pulumi stack output
for the original stack (the one used to construct the stackref "user/proj/stack")?
s
Copy code
Current stack outputs (16):
    OUTPUT                  VALUE
    EIP                     
    amiId                   ami-0288fa05d2ca8b5c7
    bastionInstanceId       i-0b93a76f7d1db768f
    bastionSecGrpId         sg-0045840efd05c2db3
    defaultRoute            rtb-0a55c701f0d9bccb4
    gatewayId               igw-045fa2082d3efcd1e
    inetRoute               r-rtb-0a55c701f0d9bccb41080289494
    k8sSecGrpId             sg-0a4687591102705da
    natGateway              nat-027db1764b3ad56a1
    nodeSecGrpId            sg-06648ddf8796bf63c
    numOfAZs                3
    privRouteTableAssocIds  ["rtbassoc-0db084e8424e969c2","rtbassoc-068e695773ce5bc7a","rtbassoc-03da8e12e6db0ef47"]
    privRouteTableId        rtb-090cc2bd626d9f5e9
    privSubnetIds           ["subnet-0484737a31b183d10","subnet-0db65b831d7dc5603","subnet-0a7e23caa1a74d92e"]
    pubSubnetIds            ["subnet-0a1b1395860d29849","subnet-00865b0cc48b28a77","subnet-04e8dfbb0e7eba4cf"]
    vpcId                   vpc-0e9bf44c8efd1133b
All the outputs seem valid.
👍 1
l
Can you share the error output you're seeing? and the code that creates the uses
privSubnet0
?
s
Yep! Here's the error output:
Copy code
error: Error launching source instance: InvalidParameter: Security group sg-06648ddf8796bf63c and subnet subnet-13f5c17b belong to different networks.
    	status code: 400, request id: dbdce577-3acc-4134-821d-b1d17eb7a84d
The referenced subnet ID is in the default VPC, not the VPC created by the referenced stack. Here's the code that references `privSubnet0`:
Copy code
controlPlane, err := ec2.NewInstance(ctx, "controlplane", &ec2.InstanceArgs{
			Ami:                      pulumi.String(amiID.Id),
			InstanceType:             pulumi.String("m5a.large"),
			AssociatePublicIpAddress: pulumi.Bool(false),
			KeyName:                  pulumi.String(keyPair),
			SubnetId:                 privSubnet0,
			VpcSecurityGroupIds:      pulumi.StringArray{nodeSecGrp},
			Tags: pulumi.StringMap{
				"Name": pulumi.String("controlplane"),
			},
		})
Note that
nodeSecGrp
, used in the code above, is the result of a stack reference and is working fine.
l
@salmon-account-74572 is this the exact code you used?
Copy code
ref.GetOutput(pulumi.String("ref"))
Should this use the name of your output instead?
Copy code
ref.GetOutput(pulumi.String("privSubnetIds")
s
Doh! You're right...I was referencing an non-existent output. Can't believe I missed that! hangs his head in shame
And now it works, as expected.
l
Awesome, glad to hear it's working. Copy paste bites us all! Eventually it would be nice to have some strongly typed way to do stack outputs and references.