kagarlickij
kagarlickij

Reputation: 8107

Determine if instance is a part of some AutoScaling Group in AWS

Is it possible to determine from instance if it's a member of AutoScaling Group in AWS?

Thanks in advance!

Upvotes: 8

Views: 10409

Answers (3)

Raffi
Raffi

Reputation: 1920

If the instance metadata service (IMDS) is enabled for the EC2 instance, then the easiest way is to see if it reports a target-lifecycle-state.

endpoint="http://169.254.169.254/latest/meta-data/autoscaling/target-lifecycle-state"

if [[ `curl -o /dev/null -s -w "%{http_code}" "${endpoint}"` == "200" ]]; then
    echo "AutoScalingGroup"
else
    echo "Instance"
fi

Upvotes: 0

Vibin Andrews
Vibin Andrews

Reputation: 9

Install AWS CLI on the instance and run this command: aws autoscaling describe-auto-scaling-instances

You can query the instance meta-data and compare the instance-id you have received from the previous command to check if the instance is part of autoscaling group.

To query instance-meta data, try curl http://169.254.169.254/latest/dynamic/instance-identity/document

Upvotes: 0

Mark B
Mark B

Reputation: 200682

You can run this command on an EC2 Linux instance to check if the current instance is in an AutoScaling group. You will need to check if the returned value is empty to determine if it is in an AutoScaling group or not.

aws autoscaling describe-auto-scaling-instances --instance-ids `curl --silent http://169.254.169.254/latest/meta-data/instance-id 2>&1`

Note, you will have to have the AWS CLI tool installed and configured before you can run this command.

Upvotes: 11

Related Questions