Really struggling with a stackreference/output typ...
# typescript
a
Really struggling with a stackreference/output typing issue trying to reference an array from my central config stack - my typescript isn't the best. Providing a slightly simplified version of my scenario - tried a bunch of things, including trying to JSON.stringify the output and then parse it back on the other side, but I can't seem to turn it back into an array of strings that I can iterate over: Stack A - Config: Exports a bunch of core GCP config values for use in other stacks like so
Copy code
export const gcpConfig = {
  regions: config.requireObject<Array<string>>('gcp-regions'),
};
offending config value looks like
Copy code
myconfig:gcp-regions:
  - us-east1
Stack B - Infra: Utilizes a custom resource that creates a GCP network and a bunch of subnets
Copy code
const envConfig = new pulumi.StackReference('yadayada');
const gcpConfig = envConfig.getOutput('gcpConfig');
const regions = gcpConfig.apply((v) => v.regions);
Unfortunately I can't figure out any way to manipulate
regions
back into an array of strings I can iterate over. Can someone please help?
g
I suspect your problem is that you’re trying to convert an Output back to a raw value (list of strings), which isn’t possible.
If you need to access the values of an Output, it has to be within an apply. The code in an apply is a callback that runs once the value is available.
Copy code
const gcpConfig = envConfig.getOutput("gcpConfig")
gcpConfig.apply((regions: string[]) => {
    for (const region of regions) {
        // Do stuff here
    }
})
a
I need to be able to pass the output into a customresource as an argument and iterate over its values there. If I were iterating over it outside of the resource I'd probably be fine - but I thing a lot of my struggle is figuring out what the type of the arguement for the CR should be. I've been trying
string[]
which obv. doesn't work, and
pulumi.Input<string[]>
but that doesn't give me the ability to apply or iterate. Does it make sense for my custom resource to accept a
pulumi.Output
as an argument? feels wrong, but maybe I'm getting hung up on naming
g
In this case, you’d likely want a
pulumi.Input<string[]>
, which can handle Outputs as well as raw values. If you need to interact with that value in the custom resource logic, then you can do something like
pulumi.output(myInputValue).apply(x => {...})
l
Would you not then be trying to create some resources (the ones that uses
const regions
) inside an
.apply()
? That doesn't work, I think?
When I ran up against this problem in my workflow, the only solution I found was to copy the relevant outputs to my stack config. So long as they're not marked as secrets, the values can be used immediately.