[PYTHON] Get all IP addresses of instances in the autoscaling group

background

Code example

import commands
import re

def ipv4_address(instance_id):
  query = "Reservations[].Instances[?InstanceId=='%(instance_id)s'].PublicIpAddress" % locals()
  cmdline = 'aws ec2 describe-instances --query "%(query)s" --output text' % locals()
  return commands.getoutput(cmdline)

def autoscaling_instance_ids(group):
  query = "AutoScalingInstances[?AutoScalingGroupName=='%(group)s'].InstanceId" % locals()
  result = commands.getoutput('aws autoscaling describe-auto-scaling-instances --query "%(query)s" --output text' % locals())
  regexp = re.compile(r'\s+')
  instance_ids = regexp.split(result)
  return instance_ids

def ip_of_the_group(group):
  ids = autoscaling_instance_ids(group)
  return [ ipv4_address(instance_id) for instance_id in ids ]

References

Recommended Posts

Get all IP addresses of instances in the autoscaling group
Get the caller of a function in Python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Get the size (number of elements) of UnionFind in Python
[Python] Outputs all combinations of elements in the list
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Get the number of specific elements in a python list
How to get all the keys and values in the dictionary
Get the number of occurrences for each element in the list
Get the index of each element of the confusion matrix in Python
Get JSON template of specific Resource in Resource Group (Azure CLI)
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
Get the number of views of Qiita
Get the desktop path in Python
Get the script path in Python
Get the attributes of an object
Get the first element of queryset
The story of the "hole" in the file
Get the number of Youtube subscribers
Get the desktop path in Python
The meaning of ".object" in Django
Get the host name in Python
Get the query string (query string) in Django
Get a capture of the entire web page in Selenium Python VBA
Click the Selenium links in order to get the elements of individual pages
Script to register the IP address used by wercker in Security Group
Get a datetime instance at any time of the day in Python
Get the key for the second layer migration of JSON data in python
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
The story of an error in PyOCR
Get the top nth values in Pandas
Implement part of the process in C ++
Get the column list & data list of CASTable
Get the minutes of the Diet via API
Read all csv files in the folder
I can't get the element in Selenium!
The basics of running NoxPlayer in Python
Get all live tweets of professional baseball
Read all the contents of proc / [pid]
Get the value of the middle layer of NN
In search of the fastest FizzBuzz in Python
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
Get the EDINET code list in Python
[PowerShell] Get the reading of the character string
Get rid of DICOM images in Python
Get your own IP address in Python
[Python] Combine all the elements in the array
How to mention a user group in slack notification, how to check the id of the user group
Output all the email body of the email group searched by Gmail and narrowed down
Use the Java SDK of GoogleMapsAPI to get the result of reverse GeoCoding in Japanese.
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
Get the last element of the array by splitting the string in Python and PHP
How to get a list of files in the same directory with python