Listing instances in an EC2 auto-scaling group?

Is there a utility or script to list all instances from the AWS EC2 autoscale group?

I need a dynamically generated list of instances to connect to our deployment process. Is there an existing tool or is this what I need to do for the script?

+5
source share
6 answers

I actually finished 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 to https://stackoverflow.com/a/318414/ was helpful in developing this script.

0
source

Here is a bash command that will provide you with a list of the IP addresses of your instances in the 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 can configure the region and filter for each AutoScaling group, if you have several)

At a higher level of vision - I would question the need to connect to individual instances in the AutoScaling group. The dynamic nature of AutoScaling helps you fully automate your deployment and administration processes. To quote the AWS client: β€œIf you need ssh for your instance, change the deployment process”

- seb

+10
source

The describe-auto-scaling-groups command from the AWS command line interface looks like what you are looking for.

Edit: after you have instance IDs, you can use the describe-instances command to get more information, including public DNS names and IP addresses.

+3
source

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

Example:

aws autoscaling description-auto-scaling instances - us-east-1 scope --query 'AutoScalingInstances [? AutoScalingGroupName == `YOUR_ASG`] '--outout text

Hope that helps

+3
source

for ruby ​​using aws-sdk gem v2 First create an ec2 object as follows:

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

cases = []

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

end

This will extract all instance identifiers in a specific region and can use more filters like ip_address.

+1
source

Use the snippet below to sort the ASG with specific tags and list its instance data.

 #!/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() 
0
source

All Articles