:thinking_face: Pulumi transformations - override ...
# python
l
šŸ¤” Pulumi transformations - override stack transformation with resource transformation ? Hi there ! I'd like to apply tags on all AWS taggable resources we provisioned (+future ones). After long research (including ones on
defaultTags
, only working for future resources), I discovered transformations, which seems to do the trick with auto_tagging (see here). I have a successful
pulumi preview
to tag all my resources. For that, I used pulumi_aws_tag pypi package. After this stack transformation, I'm looking to add "local" transformations to resources (e.g: EC2 instance). Example: • I have the following (truncated for readability) stack config (Pulumi.test.yaml):
Copy code
...
config:
  # Those tags could be overridden at component-level.
  global_tags:
    foo: value1
... <variables for my instances>
ec2_instances:
  - url: my-ec2instance-test
    instance_type: r5.xlarge
    os_version: Ubuntu22
    local_tags:
      foo: value2
      bar: value3
main.py (truncated):
Copy code
global_tags = config.require_object("global_tags")
# Automatically inject tags to created AWS resources
register_auto_tags(global_tags)
...
ec2_instances = config.get("ec2_instances")
edge_instances_configs = json.loads(edge_instances_configs)
    for instance in edge_instances_configs:
      local_tags = instance.get("local_tags")
      new_instance = aws.ec2.Instance(
                   "my-instance",
                   instance_type=instance.get("instance_type"),
                   ami="ami-0bb75d95f668ff5a7",
                   opts=pulumi.ResourceOptions(
                       transformations=[register_local_tags(local_tags)],
                   ),
      )
During a
pulumi preview
, I see the instance will have the 3 tags. However, it seems like the value from
global_tags
takes precedence (in short,
foo=value1
for my instance). I'd like to get value from stack transformation by default, but the possibility to override it at resource-level. Maybe is there a way to order my transformations ? Or a way to combine/merge them in a way that resource transformation (understand tags) would override inherited values by default ? I'm also wondering if moving to
transform
will bring me this benefit šŸ¤” It seems there are no concept of "Stack transformation" anymore with transforms. Many thanks beforehand ! šŸ™
āœ… 1
šŸ‘€ 1
Oh, btw the
register_local_tags()
function simply does the following:
Copy code
def register_local_tags(cloud_tags: dict):
    """Add prefix to every tag and register those """
    return lambda args: _transform(args, prefixed_tags)


def _transform(args: pulumi.ResourceTransformationArgs, auto_tags: dict) -> pulumi.ResourceTransformationResult | None:
    """Enforces the given tags to EC2 instances and its child resources."""
    args.props["tags"] = {**(args.props["tags"] or {}), **auto_tags}
    return pulumi.ResourceTransformationResult(args.props, args.opts)
update: I've found a way ! šŸŽ‰ I actually need to pass transforms (instead of transformations) to my EC2 instance constructor ! So: • for my
_transform
function, replace
ResourceTransformationArgs
-->
ResourceTransformArgs
and
ResourceTransformationResult
-->
ResourceTransformResult
• when you call the Instance constructor (transforms instead of transformations):
Copy code
opts=pulumi.ResourceOptions(
                       transforms=[register_local_tags(local_tags)],
                   )
Note: this works but it's a mix of usage of transformations (via register_auto_tags) and transforms, but it works šŸ˜„
l
Indeed,
transformations
is (somewhat confusingly) deprecated since it uses a legacy mechanism that doesn't work reliably.
transforms
is the newer, better implementation of the feature.
šŸ‘ 1
l
Yup ! To avoid mixing
transformations
and
transforms
, I wrote my own implementation of
register_auto_tags
to migrate to
transforms
. I saw that the contributor of
pulumi_aws_tags
(@proud-art-41399 ? šŸ˜„ ) had the same idea as me, so I'll contribute to his Github directly šŸ˜‰ Thanks for clarification @lively-crayon-44649 ! šŸ™
l
No problem -- glad you sorted it yourself!
p
@late-diamond-18080 I already have prepared migration from transformations to transforms in a feature branch of pulumi-aws-tags and will release a new major version soon. The only missing thing are tests which is a bit problematic with transforms (see one of the latest issue at pulumi package by me)
šŸ™Œ 1
@late-diamond-18080 this should be fixed now in the new major version of the library
šŸ™Œ 1
šŸ‘€ 1
l
Awesome ! šŸŽ‰ I'm gonna take a look and see if I get any issues then šŸ’Ŗ