简体   繁体   中英

Getting a list of instances in an EC2 auto scale group?

Is there a utility or script available to retrieve a list of all instances from AWS EC2 auto scale group?

I need a dynamically generated list of production instance to hook into our deploy process. Is there an existing tool or is this something I am going to have to script?

Here is a bash command that will give you the list of IP addresses of your instances in an AutoScaling group.

for ID in $(aws autoscaling describe-auto-scaling-instances --region us-east-1 --query AutoScalingInstances[].InstanceId --output text);
do
aws ec2 describe-instances --instance-ids $ID --region us-east-1 --query Reservations[].Instances[].PublicIpAddress --output text
done

(you might want to adjust the region and to filter per AutoScaling group if you have several of them)

On a higher level point of view - I would question the need to connect to individual instances in an AutoScaling Group. The dynamic nature of AutoScaling would encourage you to fully automate your deployment and admin processes. To quote an AWS customer : "If you need to ssh to your instance, change your deployment process"

--Seb

The describe-auto-scaling-groups command from the AWS Command Line Interface looks like what you're looking for.

Edit: Once you have the instance IDs, you can use the describe-instances command to fetch additional details, including the public DNS names and IP addresses.

You can use the describe-auto-scaling-instances cli command, and query for your autoscale group name.

Example:

aws autoscaling describe-auto-scaling-instances --region us-east-1 --query 'AutoScalingInstances[?AutoScalingGroupName==`YOUR_ASG`]' --output text

Hope that helps

You can also use below command to fetch private ip address without any jq/awk/sed/cut

$ aws autoscaling describe-auto-scaling-instances --region us-east-1 --output text \
--query "AutoScalingInstances[?AutoScalingGroupName=='ASG-GROUP-NAME'].InstanceId" \
| xargs -n1 aws ec2 describe-instances --instance-ids $ID --region us-east-1 \
--query "Reservations[].Instances[].PrivateIpAddress" --output text

courtesy this

I actually ended up writing a script in Python because I feel more comfortable in Python then Bash,

#!/usr/bin/env python

"""
ec2-autoscale-instance.py

Read Autoscale DNS from AWS

Sample config file,
{
    "access_key": "key",
    "secret_key": "key",
    "group_name": "groupName"
}
"""

from __future__ import print_function
import argparse
import boto.ec2.autoscale
try:
    import simplejson as json
except ImportError:
    import json

CONFIG_ACCESS_KEY = 'access_key'
CONFIG_SECRET_KEY = 'secret_key'
CONFIG_GROUP_NAME = 'group_name'


def main():
    arg_parser = argparse.ArgumentParser(description=
                                         'Read Autoscale DNS names from AWS')
    arg_parser.add_argument('-c', dest='config_file',
                            help='JSON configuration file containing ' +
                                 'access_key, secret_key, and group_name')
    args = arg_parser.parse_args()
    config = json.loads(open(args.config_file).read())
    access_key = config[CONFIG_ACCESS_KEY]
    secret_key = config[CONFIG_SECRET_KEY]
    group_name = config[CONFIG_GROUP_NAME]

    ec2_conn = boto.connect_ec2(access_key, secret_key)
    as_conn = boto.connect_autoscale(access_key, secret_key)

    try:
        group = as_conn.get_all_groups([group_name])[0]
        instances_ids = [i.instance_id for i in group.instances]
        reservations = ec2_conn.get_all_reservations(instances_ids)
        instances = [i for r in reservations for i in r.instances]
        dns_names = [i.public_dns_name for i in instances]
        print('\n'.join(dns_names))
    finally:
        ec2_conn.close()
        as_conn.close()


if __name__ == '__main__':
    main()

Gist

The answer at https://stackoverflow.com/a/12592543/20774 was helpful in developing this script.

Use the below snippet for sorting out ASGs with specific tags and listing out its instance details.

#!/usr/bin/python

import boto3

ec2 = boto3.resource('ec2', region_name='us-west-2')

def get_instances():
        client = boto3.client('autoscaling', region_name='us-west-2')
        paginator = client.get_paginator('describe_auto_scaling_groups')
        groups = paginator.paginate(PaginationConfig={'PageSize': 100})
        #print groups
        filtered_asgs = groups.search('AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format('Application', 'CCP'))

        for asg in filtered_asgs:
                print asg['AutoScalingGroupName']
                instance_ids = [i for i in asg['Instances']]
                running_instances = ec2.instances.filter(Filters=[{}])
                for instance in running_instances:
                        print(instance.private_ip_address)

if __name__ == '__main__':
    get_instances()

for ruby using aws-sdk gem v2 First create ec2 object as this:

ec2 = Aws::EC2::Resource.new(region: 'region', credentials: Aws::Credentials.new('IAM_KEY', 'IAM_SECRET') )

instances = []

ec2.instances.each do |i|
   p "instance id---", i.id
   instances << i.id

end

This will fetch all instance ids in particular region and can use more filters like ip_address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM