Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Last active February 22, 2024 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amalgjose/49c8a665d339bfef2b644e1a6c824bdf to your computer and use it in GitHub Desktop.
Save amalgjose/49c8a665d339bfef2b644e1a6c824bdf to your computer and use it in GitHub Desktop.
Python program to list all ec2 instances in a region using boto3
import boto3
access_key = "XXXXXXXXXXXXXXXXXX"
secret_key = "XXXXXXXXXXXXXXXXXX"
region = 'us-east-1'
client = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key,
region_name=region)
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, instance.tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment