Skip to content

Instantly share code, notes, and snippets.

@lavie
Created June 15, 2014 18:05
Show Gist options
  • Save lavie/720b5a6c1eb322dd91ba to your computer and use it in GitHub Desktop.
Save lavie/720b5a6c1eb322dd91ba to your computer and use it in GitHub Desktop.
List all EC2 instances in a region and their public IPs (handy for populating hosts file)
from boto.ec2 import *
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("access_key", help = "AWS Access Key")
parser.add_argument("secret_key", help = "AWS Secret Key")
parser.add_argument("--region", help = "AWS Region", default = "us-east-1")
parser.add_argument("--all", help = "show not just running instances", action = "store_true")
args = parser.parse_args()
conn = connect_to_region(args.region, aws_access_key_id = args.access_key, aws_secret_access_key = args.secret_key)
instances = conn.get_only_instances()
filters = { }
if not args.all:
filters = {
'instance-state-name' : 'running'
}
instances = conn.get_only_instances(filters = filters)
for instance in instances:
print instance.ip_address + " " + instance.tags['Name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment