late-diamond-18080
08/27/2024, 2:51 PMdefaultTags
, 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):
...
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):
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 ! šlate-diamond-18080
08/27/2024, 2:55 PMregister_local_tags()
function simply does the following:
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)
late-diamond-18080
08/29/2024, 8:36 AM_transform
function, replace ResourceTransformationArgs
--> ResourceTransformArgs
and ResourceTransformationResult
--> ResourceTransformResult
⢠when you call the Instance constructor (transforms instead of transformations):
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 šlively-crayon-44649
08/29/2024, 4:12 PMtransformations
is (somewhat confusingly) deprecated since it uses a legacy mechanism that doesn't work reliably. transforms
is the newer, better implementation of the feature.late-diamond-18080
08/29/2024, 4:16 PMtransformations
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 ! šlively-crayon-44649
08/29/2024, 4:17 PMproud-art-41399
08/29/2024, 4:20 PMproud-art-41399
08/29/2024, 4:23 PMproud-art-41399
09/04/2024, 1:04 PMlate-diamond-18080
09/04/2024, 1:07 PM