Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Created March 8, 2017 23:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalgjose/fb274568fa0a4a47ec80e1a3913d09dd to your computer and use it in GitHub Desktop.
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.
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