https://pulumi.com logo
#typescript
Title
# typescript
s

square-hair-965

09/21/2022, 4:06 PM
hey everyone, quick question: I frequently want to have optional infrastructure for my stacks, in this instance a cron worker auto scale group that only runs in the primary region. My problem is that I want to output all my auto scale groups using
pulumi.all().apply()
and you can probably see the problem I run into. Now I could just output groups as individual constants and be done with it, which I'll likely do as I only have 4-5 autoscale groups per region, but I was wondering if anyone's ever encountered this scenario and figured out a certain code pattern to make it work? As of right now my go-to solution is to output "none" strings when trying to output arns and ids of optional infrastructure.
i wonder if pushing infra to the array that will be fed to
pulumi.all({group1, group2})
and then doing dot notation
.apply([...groups])
would work and not break pulumi plan tracking?
l

little-cartoon-10569

09/21/2022, 8:12 PM
You want to output the ASGs.. do you mean, you want to export them as stack exports? You can use ... notation for this, and you don't need
all()
or
apply()
.
If you mean, you want to use some properties of the ASGs in an
apply()
, then the answer depends on what's happening in the
apply()
. If it's a simple
<http://pulumi.info|pulumi.info>.log()
or similar, then you can use the ... notation still. If you're creating more resources, then plan tracking will break, and you'll need to find another way to solve the problem.
s

square-hair-965

09/21/2022, 8:14 PM
i guess it might be a little hard to follow, but I'm having difficulty with outputting things that might only exist in certain stacks, like in a primary web region
things that are conditionally created based on a configuration boolean flag for example
l

little-cartoon-10569

09/21/2022, 8:16 PM
I don't know what "outputting" means in this context. If you create something conditionally, what's stopping you from "outputting" it, either conditionally or unconditionally (may be undefined)?
s

square-hair-965

09/21/2022, 8:17 PM
right so by outputting I mean exporting resources, so say I conditionally created a cron server in one web region and tried to get that output in another projects stack, that output is actually undefined for this corresponding stack but I wouldn't know that at the time the plan is made
is this just kind of a common problem you run into or is there some way to work around this
well I would know that, but pulumi does not
l

little-cartoon-10569

09/21/2022, 8:20 PM
If it's just exporting, and you want to export "all the things" without explicitly naming them all, then you can push them into an array and export the array?
The conditional things won't get pushed in some stacks, the code will continue to work and be correct in all stacks.
s

square-hair-965

09/21/2022, 8:21 PM
ok that was my intuitive thought as well, i'll run with that idea
thx
l

little-cartoon-10569

09/21/2022, 8:21 PM
That said, you'll find when consuming those exports, that naming and exporting them one at a time will make life easier for the consumer of the stack (usually).
s

square-hair-965

09/21/2022, 8:22 PM
yeah that's been my usual process, and what I ended up doing with the first example, but i've gone with array exports for large collections, for instance a collection of sqs queues
l

little-cartoon-10569

09/21/2022, 8:23 PM
If you implement your infra in a spec-first / test-first "fulfill my ultimate requirement and let my stack design evolve", you'll find how you need to access those exports before you create the code that does the exporting. This will save implementation time by designing the correct API before the implementation.
s

square-hair-965

09/21/2022, 8:26 PM
right, i'll keep that in mind as the project grows, thx for your help
so yeah just confirmed that this does work:
Copy code
export const metricAlarms = pulumi.all(_metricAlarms).apply(([...alarms]) => alarms.map((alarm) => {
    return {
        id: alarm.id,
        arn: alarm.arn
    }
}));
i get the stripped down export I want and can simply just add new MetricAlarms to the _metricAlarm array
👍 1