generate random string in cloud custodian policy

97 Views Asked by At

I want give a tag like below to my aws resources:

key=<random_string>

unique_name="vrKdtBo2ZO"

In a cloud custodian policy, how to generate a unique random string for tagging a resource?

1

There are 1 best solutions below

0
Boreaz On

You can use the built-in variables.

In your policy, you simply provide them with the notation {<var>} and on custodian/c7n-org run, these will get interpolated at runtime.

For your use-case, {uuid} would be an option for uniqueness.

Some additional variables of interest might be:

{account}: the account where the check was executed.
{region}: the region where the check was executed.
{policy_name}: the name of the policy that was executed.
{now}: a datetime representing utc timestamp (see formatting options https://pyformat.info/#datetime)
{uuid}: a one time uuid

ref: https://cloudcustodian.io/docs/aws/usage.html#s3-logs-records (note: these variables appear to be documented in the custodian reporting section but these are also applicable within policies)