Hey y'all - I'm sure this question has been asked ...
# python
a
Hey y'all - I'm sure this question has been asked before but Google and the Pulumi AI aren't helping me find an answer. I want to tag a Azure resource I am creating with the date it was created. My idea is that I need to instantiate a string when a stack is created, pulling the current date and 'saving' that. Then when I
pulumi up
the next day it doesn't try to change the tag. I tried
Copy code
class CurrentDateComponent(pulumi.ComponentResource):
    def __init__(self, name: str, opts: Optional[pulumi.ResourceOptions] = None):
        super().__init__('custom:resource:CurrentDateComponent', name, None, opts)
        self.date = datetime.now(UTC).date().strftime('%Y-%m-%d')  # Store the current UTC date in ISO format

current_date = CurrentDateComponent("currentDate").date
and this seems to work. But, the resource shows as
creating
the entire time the stack is being created. What is an easy way to do this?
Also hey @miniature-musician-31262 👋 😄
m
Why did you make a component resource for that? Can't you just tag the resource with
datetime.now(UTC).date().strftime('%Y-%m-%d')
directly?
Copy code
current_date = datetime.now(UTC).date().strftime('%Y-%m-%d')

resource1 = SomeAzureResource(..., tags={"created_at": current_date})

resource2 = SomeAzureResource(..., tags={"created_at": current_date})
This should update the resource every time you run
pulumi up
if the value of
current_date
has changed compared to the previous run.
If you don't want to update the tag every time you run
pulumi up
but only if some property of the resource changes (other than the tag) you can use the ignoreChanges resource option for that and ignore changes to the
created_at
tag.