salmon-account-74572
07/17/2020, 4:46 PMctx.Export("privSubnetIds", pulumi.StringArray(privSubnetIds))
In another stack that builds on top of that infrastructure stack, I reference the array like this:
infra, err := pulumi.NewStackReference(ctx, "org/project/stack", nil)
privSubnets := infra.GetOutput(pulumi.StringArray("privSubnetIds))
I haven't tried to run this yet, because gopls
inside VS Code is showing errors on the privSubnets
assignment line & when I try to reference that value inside an ec2.NewInstance
stanza. I think the error is in the assignment line, but I'm not quite sure how it needs to be changed to work. Any suggestions?lemon-agent-27707
07/17/2020, 5:21 PMprivSubnetIds
?
Typically when you construct a pulumi array you do so like:
pulumi.StringArray{
pulumi.String("foo"),
pulumi.String("bar)
}
billowy-army-68599
07/17/2020, 5:33 PMsalmon-account-74572
07/17/2020, 5:35 PMbillowy-army-68599
07/17/2020, 5:36 PMsalmon-account-74572
07/17/2020, 6:21 PMprivSubnetIds := make([]pulumi.StringInput, numOfAZs)
for idx := 0; idx < numOfAZs; idx++ {
subnetAddr := (idx * 32) + 16
subnetCidrBlock := fmt.Sprintf("%s%d.0/22", netAddrMap[awsRegion], subnetAddr)
subnet, err := ec2.NewSubnet(ctx, fmt.Sprintf("priv-subnet-%d", idx), &ec2.SubnetArgs{
VpcId: vpc.ID(),
AvailabilityZone: pulumi.String(azNames[idx]),
CidrBlock: pulumi.String(subnetCidrBlock),
MapPublicIpOnLaunch: pulumi.Bool(false),
Tags: pulumi.StringMap{
"Name": pulumi.String(fmt.Sprintf("%s-priv-subnet-%d", baseName, idx)),
k8sTag: pulumi.String("shared"),
},
})
if err != nil {
log.Printf("error creating private subnet: %s", err.Error())
}
privSubnetIds[idx] = subnet.ID()
}
ctx.Export("privSubnetIds", pulumi.StringArray(privSubnetIds))
Should I be a) using a different type for privSubnetIds
, b) using a different pulumi.<type>
function in the Export, c) both a and b, or d) something else entirely? Or is the problem in how I am bringing the value(s) into the second stack?billowy-army-68599
07/20/2020, 5:15 PMlemon-agent-27707
07/20/2020, 5:15 PMGetOutput
to pulumi.StringArrayOutput is unsafe and can fail if the value is nil
.salmon-account-74572
07/20/2020, 5:25 PM[]pulumi.IDOutput
, and then using your idOutputArrayToIDArrayOutput
function when exporting the array; in the stack reference defining it as a StringArrayOutput
). The problem is when I go to reference a specific element in the array, and Go reports that "it cannot index variable of type pulumi.StringArrayOutput".billowy-army-68599
07/20/2020, 5:27 PMsalmon-account-74572
07/20/2020, 5:39 PMbillowy-army-68599
07/20/2020, 5:41 PMlemon-agent-27707
07/20/2020, 5:42 PMpulumi.***(...)
You'll need to write the appropriate apply functions. Here's an example of something I put together. First program exports a pulumi.StringArray
and the second one imports it, and manipulates it to get the first value.
package main
import (
"<http://github.com/pulumi/pulumi/sdk/v2/go/pulumi|github.com/pulumi/pulumi/sdk/v2/go/pulumi>"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
ctx.Export("ref", pulumi.StringArray{
pulumi.String("foo"),
pulumi.String("bar"),
})
return nil
})
}
package main
import (
"<http://github.com/pulumi/pulumi/sdk/v2/go/pulumi|github.com/pulumi/pulumi/sdk/v2/go/pulumi>"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
ref, err := pulumi.NewStackReference(ctx, "EvanBoyle/ref1/dev", nil)
if err != nil {
return err
}
strarr := 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
})
idx0 := strarr.ApplyString(func(a []string) string {
var res string
if len(a) > 0 {
res = a[0]
}
return res
})
ctx.Export("idx0", idx0)
return nil
})
}
ApplyStringArray
and ApplyString
to ApplyIDArray
and ApplyID
.billowy-army-68599
07/20/2020, 7:01 PMsalmon-account-74572
07/20/2020, 7:52 PMstrarr
still listed as type StringArrayOutput, seems like it should just be StringArray?
2. Should I not just be able to do strarr[0]
, why the need for the function to get out the indexed value?lemon-agent-27707
07/20/2020, 8:04 PM1. Why isstill listed as type StringArrayOutput, seems like it should just be StringArray?strarr
GetOutput
will always return an Output
, not a prompt value. Also Apply
and it's many forms will always return outputs.
2. Should I not just be able to doThe value that pulumi sees in the apply is of type, why the need for the function to get out the indexed value?strarr[0]
[]interface{}
. So we must cast to the appropriate type. The reason for this is that GetOutput
is intended to be a generic method that supports all types.
We should improve the experience here by offering common helpers for stackrefs like GetStringArrayOutput
and GetIDArrayOutput