https://pulumi.com logo
#general
Title
# general
m

melodic-psychiatrist-50649

01/31/2024, 5:42 AM
I'm fighting against Outputs - seems to be a theme here - and looking for advice. Here's my goal in python: • Spin up a number of EC2 instances, create a twingate remote network. That's all easy. • On an "infra" EC2 instances I want to provide it aws user data that contains values from pulumi-managed objects, e.g.
Copy code
FOO_INSTANCE_IP=<came from launching ec2 instance foo>
BAR_INSTANCE_IP=<came from launching ec2 instance bar>
TWINGATE_KEY=<came from configuring a twingate connector>
<actual provisioning bash script concatenated here>
This "infra" machine will use data about the other resources to do important things. What I'm failing to manage, however, is find a way to delay generating the user data (f-strings,
.format()
, etc) until the dependent tasks are complete so these values are available. Also tried generating the content and uploading to S3, but that suffered a similar fate. I've read https://www.pulumi.com/docs/concepts/inputs-outputs/ but that's not leading me to enlightenment. Pointers? Suggestions?
i

icy-controller-6092

01/31/2024, 6:44 AM
Can your infra machine pull the values instead of having them pushed to it?
m

melodic-psychiatrist-50649

01/31/2024, 7:03 AM
That'd be ideal, but I'd still need to get the values to something in a way we know happens before the infra machine is launched, and then we're back to the same ordering problem.
(or the infra machine could be launched afterwards, outside of pulumi, but that's going backwards.)
i

icy-controller-6092

01/31/2024, 7:06 AM
show me how you'd create the infra machine with the values if you knew what they were ahead of the time
s

salmon-account-74572

01/31/2024, 2:25 PM
This should be doable, based on my understanding of what you're trying to accomplish. Have you created a dependency between `foo`/`bar` & the
infra
machine, so that Pulumi knows those two need to be created first? If you haven't tried that yet, I'd start there (look into
dependsOn
). Also, try using
pulumi.All
to build the user data instead of f-strings to see if that helps.
m

melodic-psychiatrist-50649

01/31/2024, 5:35 PM
Thanks @salmon-account-74572 - it certainly seemed like it should work, and with that confirmation I kept plugging way and got there.
Copy code
user_data = pulumi.Output.all(key=val, key2=val2, ...).apply(lambda a: f"blah = {a['key2']} ...")

infra = aws.ec2.Instance(
  ...
  opts=pulumi.ResourceOptions(depends_on=[other_instances])
)
s

salmon-account-74572

01/31/2024, 5:48 PM
Glad you got it working!
l

little-cartoon-10569

01/31/2024, 7:16 PM
FYI. the Pulumi cloud-init package makes building user_data much simpler.. once you get the hang of it. https://www.pulumi.com/registry/packages/cloudinit/