10

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!

4 Answers 4

10

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
1
  • 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, 2016 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)"'
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.

1

Simplest to get the autoscaling group name like this:

aws autoscaling describe-auto-scaling-instances --query=AutoScalingInstances[].AutoScalingGroupName --instance-ids=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) --output text

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .