how to tag an existing AWS autoscaling group with boto3

Question:

I’m trying to tag an existing autoscaling group, using Python and Boto3.

I can definitely describe the ASGs by name:

import boto3
asg_client = boto3.client("autoscaling")
asg_name = "foo-bar20220502044025104700000001"
response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=[asg_name])

The problem I have with the create_or_update_tags() and delete_tags() methods is that they don’t seem to accept a list of ASG names. E.g. this doesn’t work:

asg_client.create_or_update_tags(
    AutoScalingGroupNames=[asg_name],
    Tags=[my_tag]
)

To make it clear:

  • the ASG already exists, I am not creating it here
  • I do not want to make any other changes to the ASG
  • all I want is be able to tag an ASG if I know its name
  • I need to use boto3 from Python for this

The tag-related methods appear to be different from all the other ASG client methods in that they do not accept an ASG name, or list of names, as a parameter.

Asked By: Florin Andrei

||

Answers:

You can do this, you just need provide a different input. The ResourceId of these client methods can be supplied with an ASG name.

Here is the boto3 docs for the create_or_update_tags call.

Here’s an example:

asg_client.create_or_update_tags(
    Tags=[
        {
            'ResourceId': asg_name,
            'ResourceType': 'auto-scaling-group',
            'Key': 'myTagKey',
            'Value': 'myTagValue',
            'PropagateAtLaunch': True
        },
    ]
)
Answered By: JD D