8

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!

8

How does one know which autoscaling group an instance belongs to via api/aws cli commands?

You can use the autoscaling describe-auto-scaling-instances command together with the option --instance-ids, like this:

aws autoscaling describe-auto-scaling-instances --instance-ids="i-zzxxccvv"

Problem with autoscaling groups, is that these tags are not available to the instance. [...] I need to use instance tags in scripts, however, they are not available. Suggestions?

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:

aws autoscaling describe-tags --filters Name=auto-scaling-group,Values=name-of-the-auto-scaling-group-here
| improve this answer | |
  • 4
    This is also helpful if you're trying to get the ASG name in the UserData script of a LaunchConfig in a CloudFormation Template. If you use a Ref, it will cause a circular dependency between the Launch Config and the ASG, so you have to use the autoscaling CLI. – Ryan Gross Sep 22 '16 at 20:30
5

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:

aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE}" "Name=key,Values=aws:autoscaling:groupName"

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:

aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE}"

I find jq helpful for processing the output of the aws cli tool. For example, the following command produces a key=value list from the JSON:

aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE}" | jq -r '.Tags[] | @text "\(.Key)=\(.Value)"'
| improve this answer | |
1

To put it all together :

INSTANCE_ID=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
ASG_NAME=`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"  --region us-east-2 | jq '.Tags[] | select(.["Key"] | contains("aws:autoscaling:groupName")) | .Value'`
ASG_NAME=`echo $ASG_NAME | tr -d '"'`

Do an echo $ASG_NAME and you should see it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.