Created
March 8, 2017 23:58
-
-
Save amalgjose/fb274568fa0a4a47ec80e1a3913d09dd to your computer and use it in GitHub Desktop.
Simple python code snippet to get the list of all the running ec2 instances across regions. The code will dynamically pick the list of available regions. So this will work perfectly without a code change even if a new region gets added.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
access_key = "XXXXXXXXXXXXXXXXXX" | |
secret_key = "XXXXXXXXXXXXXXXXXX" | |
client = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, | |
region_name='us-east-1') | |
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']] | |
for region in ec2_regions: | |
conn = boto3.resource('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, | |
region_name=region) | |
instances = conn.instances.filter() | |
for instance in instances: | |
if instance.state["Name"] == "running": | |
print (instance.id, instance.instance_type, region) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment