Folks, I am writing a script that will export the tags for a running instance as environment variables. Problem with autoscaling groups, is that these tags are not available to the instance.
How does one know which autoscaling group an instance belongs to via api/aws cli commands? I need to use instance tags in scripts, however, they are not available. Suggestions?
Thanks!
You can use the
autoscaling describe-auto-scaling-instances
command together with the option--instance-ids
, like this:I'm interpreting this as you want to get the tags of the autoscaling group from which the instance belongs to? Using the
AutoScalingGroupName
returned from the command shown above, you can use the following command:The simplest way of finding the autoscaling group of an instance is to examine the tags. Any instances in an autoscaling group will have a tag named
aws:autoscaling:groupName
that contains the group name. You can retrieve that tag using the following command:Then you can grab the tags as described by Bazze.
An alternate solution to this is to propagate the tags from the autoscaling group to the instance.
If you are using cloudformation to create the ASG then set
"PropagateAtLaunch": "true"
for each tag you want to propagate (see Aws Cloud Formation Docs).If you are creating the group manually make sure you check the "Tag New Instances" box for each tag.
Once you've done this, the tags will be copied to the instance and you can simply inspect the tags of your instance using the describe tags command:
I find
jq
helpful for processing the output of the aws cli tool. For example, the following command produces akey=value
list from the JSON:To put it all together :
Do an
echo $ASG_NAME
and you should see it.Simplest to get the autoscaling group name like this: