Bit of a related question about patterns for manag...
# general
f
Bit of a related question about patterns for managing `pulumi.Output`s... several AWS resources, to use an example, are configured by passing a JSON object as a string. Many times, the values in that JSON object are outputs from other resources. Often, you can do this pretty simply, like so:
Copy code
role.arn.apply(lambda arn: json.dumps({"foo": arn}))
When you have multiple things that need to be interpolated, things get a little more complicated:
Copy code
pulumi.Output.all(
   foo.arn,
   bar.arn,
   baz.arn
).apply(
    lambda args: json.dumps({
        "foo": args[0],
        "bar": args[1],
        "baz": args[2]
    })
)
While that's awkward, it's doable when you have all the `pulumi.Output`s that you need readily at hand. I'm using `ComponentResource`s to model my domain. In general, I'd like to be able to pass a dictionary of values into my
ComponentResource
, and then have it merge that with another dictionary of default values within the
ComponentResource
itself. Then, this merged dictionary would need to be converted into a JSON string to pass to the low-level Resource.
Copy code
env = {
   "foo": "my_foo",
   "message": "hello world"
}

pulumi.Output.all(
   foo.arn,
   bar.arn,
   baz.arn
).apply(
    lambda args: json.dumps({
        "foo": args[0],
        "bar": args[1],
        "baz": args[2],
        **env
    })
)
If that inner "default values" dictionary is the one that contains the
pulumi.Output
values, then I can control things with
pulumi.Output.all
, as shown above. If I've got
pulumi.Output
values in that dictionary that I'm merging in, though, it's not clear how I can generically manage things. I'm curious if others have run into similar situations, or if people have some patterns for managing such data (generic, if possible; Python-specific works too). Thanks in advance.
l
I think the best solution to this that I've seen is Pulumi's own aws.iam.PolicyDocument type. I presume it's used in many places, since the code is fairly generic: https://github.com/pulumi/pulumi-terraform-bridge/blob/ea90ddd011cdf688615b1fae6e705b5449dc4e7f/pkg/tfbridge/transforms.go#L26
Which type of AWS resource / JSON doc are you looking to create?
If it's not already supported, you can raise an issue. The ability to create normal objects in your language of choice, and have Pulumi convert them to the correctly-formatted JSON doc at deploy time, is a real winner.
f
I'll look at that Go code for ideas. At the moment, I'm working with an ECS container definition (https://www.pulumi.com/docs/reference/pkg/aws/ecs/taskdefinition/#container_definitions_python)
r
I’ll admit that I’m not following all the way through, but I’m wondering if passing keyword args to your
Output.all
makes a difference.
If I’ve got
pulumi.Output
values in that dictionary that I’m merging in, though, it’s not clear how I can generically manage things.
Output.all(**dict).apply(lambda args: json.dumps({**args, **env}))
f
I think I was making some silly mistakes, but using keyword args ultimately helped show me those silly mistakes (and made for more readable code!). I was able to get it working; thanks!
🎉 1
partypus 8bit 1