https://pulumi.com logo
#dotnet
Title
# dotnet
c

colossal-vr-62639

06/23/2021, 5:51 AM
Hey everyone! What is the canonical way in pulumi to store an array of items and read it as outputs? For example storing a list of subnets...
Copy code
return new Dictionary<string, object?>()
    {
        {
            "subnets-public",
            new[] {componentOne.PublicSubnet0.Apply(x => x.Id), componentOne.PublicSubnet1.Apply(x => x.Id)}
        },
    };
and then in some other project, read the outputs from the stack...
Copy code
var subnets = stack.GetOutput("subnets-public")
b

bored-oyster-3147

06/23/2021, 12:42 PM
to convert your 2
Output<string>
into
Output<string[]>
it would be:
Copy code
var subnetIdArray = Output.Tuple(componentOne.PublicSubnet0, componentOne.PublicSubnet1).Apply((subnet0, subnet1) => {
    return new[] { subnet0.Id, subnet1.Id };
});
And then you could do:
Copy code
return new Dictionary<string, object?>()
{
    ["subnets-public"] = subnetIdArray,
};
But I've never personally tried to output an array, I've always kept it
string:string
. So not sure what
var subnets = stack.GetOutput("subnets-public")
will look like
To clarify this
new[] {componentOne.PublicSubnet0.Apply(x => x.Id), componentOne.PublicSubnet1.Apply(x => x.Id)}
is giving you
Output<string>[]
instead of
Output<string[]>
. So you need to use
.Tuple(...)
to merge your outputs.