• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 12 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sugar on top of boto for accessing EC2 instances and security groups

Amazon EC2

Ever try to query for some instances with boto? It sucks.

>>> import ec2
>>> ec2.instances.filter(state='running', name__startswith='production')
[...]

Install

$ pip install ec2

Usage

AWS credentials

Credentials are defined as a global state, either through an environment variable, or in Python.

ec2.credentials.ACCESS_KEY_ID = 'xxx'
ec2.credentials.SECRET_ACCESS_KEY = 'xxx'
ec2.credentials.REGION_NAME = 'us-west-2'  # (optional) defaults to us-east-1

Credentials can also be loaded from a CSV file generated by Amazon's IAM. Note: REGION_NAME still needs to be specified.

ec2.credentials.from_file('credentials.csv')

Querying

All instances

ec2.instances.all()

All Security Groups

ec2.security_groups.all()

All Virtual Private Clouds

ec2.vpcs.all()

Filtering

Filter style is based on Django's ORM All filters map directly to instance/security group properties.

ec2.instances.filter(id='i-xxx')  # Exact instance id
ec2.instances.filter(state='running')  # Exact instance state

Filters will also dig into tags.

ec2.instances.filter(name='production-web')  # Exact "Name" tag

Filters support many types of comparisons, similar to Django's ORM filters.

ec2.instances.filter(name__exact='production-web-01')  # idential to `name='...'`
ec2.instances.filter(name__iexact='PRODUCTION-WEB-01')  # Case insensitive "exact"
ec2.instances.filter(name__like=r'^production-web-\d+$')  # Match against a regular expression
ec2.instances.filter(name__ilike=r'^production-web-\d+$')  # Case insensitive "like"
ec2.instances.filter(name__contains='web')  # Field contains the search string
ec2.instances.filter(name__icontains='WEB')  # Case insensitive "contains"
ec2.instances.filter(name__startswith='production')  # Fields starts with the search string
ec2.instances.filter(name__istartswith='PRODUCTION')  # Case insensitive "startswith"
ec2.instances.filter(name__endswith='01')  # Fields ends with the search string
ec2.instances.filter(name__iendswith='01')  # Case insensitive "endswith"
ec2.instances.filter(name__isnull=False)  # Match if the field exists

Filters can also be chained.

ec2.instances.filter(state='running', name__startswith='production')

Filters can also be used with security groups.

ec2.security_groups.filter(name__iexact='PRODUCTION-WEB')

Filters can also be used with virtual private clouds.

ec2.vpcs.filter(cidr_blocks__startswith='10.10')

get() works exactly the same as filter(), except it returns just one instance and raises an exception for anything else.

ec2.instances.get(name='production-web-01')  # Return a single instance
ec2.instances.get(name='i-dont-exist')  # Raises an `ec2.instances.DoesNotExist` exception
ec2.instances.get(name__like=r'^production-web-\d+$')  # Raises an `ec2.instances.MultipleObjectsReturned` exception if matched more than one instance
ec2.security_groups.get(name__startswith='production')  # Raises an `ec2.security_groups.MultipleObjectsReturned` exception
ec2.vpcs.get(cidr_block='10.10.0.0/16')

Search fields

Instances

  • id (Instance id)
  • state (running, terminated, pending, shutting-down, stopping, stopped)
  • public_dns_name
  • ip_address
  • private_dns_name
  • private_ip_address
  • root_device_type (ebs, instance-store)
  • key_name (name of the SSH key used on the instance)
  • image_id (Id of the AMI)

All fields can be found at: https://github.com/boto/boto/blob/d91ed8/boto/ec2/instance.py#L157-204

Security Groups

  • id (Security Group id)
  • name
  • vpc_id

Virtual Private Clouds

  • id (Virtual Private Cloud id)
  • cidr_block (CIDR Network Block of the VPC)
  • state (Current state of the VPC, creation is not instant)
  • is_default
  • instance_tenancy
  • dhcp_options_id (DHCP options id)

Examples

Get public ip addresses from all running instances who are named production-web-{number}

import ec2
ec2.credentials.ACCESS_KEY_ID = 'xxx'
ec2.credentials.SECRET_ACCESS_KEY = 'xxx'

for instance in ec2.instances.filter(state='running', name__like=r'^production-web-\d+$'):
    print instance.ip_address

Add a role to a security group

import ec2
ec2.credentials.ACCESS_KEY_ID = 'xxx'
ec2.credentials.SECRET_ACCESS_KEY = 'xxx'

try:
    group = ec2.security_groups.get(name='production-web')
except ec2.security_groups.DoesNotExist:
    import sys
    sys.stderr.write('Not found.')
    sys.exit(1)
group.authorize('tcp', 80, 80, cidr_ip='0.0.0.0/0')

More Repositories

1

jinja2-cli

CLI for Jinja2
Python
471
star
2

django-sudo

Extra security for your sensitive pages
Python
277
star
3

colors.py

Colors aren't that scary!
Python
231
star
4

proofread

Test your Django app without writing tests!
Python
89
star
5

email-me

email me when a thing is done
Go
53
star
6

sourcemap-validator

Service for validating sourcemaps
Python
51
star
7

node-snsclient

Client for Amazon AWS's SNS service
JavaScript
46
star
8

node_nibbler

Base32/base64 encode/decode library for Node
JavaScript
43
star
9

go-celery

WIP
Go
42
star
10

python-sourcemap

Parse JavaScript source maps
Python
39
star
11

jwt-node

Adapt jwt-js for use in Node.js, nothing special.
JavaScript
30
star
12

sentry-twilio

A plugin for Sentry that sends SMS notifications via Twilio
Python
22
star
13

size

A package for working with byte capacities similar to time.Duration
Go
21
star
14

firewall

Punch a hole into an AWS EC2 security group temporarily
Python
19
star
15

go-memcached

Memcached library for Go
Go
19
star
16

UT2k4-Mods

Unreal Tournament 2004 Maps and Characters
Shell
17
star
17

semaphore

A Go semaphore primitive
Go
17
star
18

go-bubs

β’·β“Šβ’·β’·β“β’Ίβ“ˆ ⒾⓃ β’Όβ“„
Go
16
star
19

mywsgi

Opinionated uWSGI setup
Python
15
star
20

node_pickle

A really, admittedly, lazy interface into Python for pickling/unpickling an object.
JavaScript
12
star
21

django-snippets

Just a bunch of small reusable snippets for projects. Middlewares, decorators, templatetags, etc.
Python
11
star
22

sentry-sprintly

A plugin for Sentry that creates Sprint.ly defects out of exceptions
Python
9
star
23

envtpl

Render a template from environment variables
Go
9
star
24

envdir-plusplus

kinda like envdir, kinda like dotenv, kinda like docker env-file
Go
9
star
25

docker-ut2004

Unreal Tournament 2004 Dedicated Server
Smarty
8
star
26

https

https proxy for local testing
Go
8
star
27

go-randid

Alternative random id generation to uuid
Go
7
star
28

connect-access

Access controls (ACLs) by location as a Connect middleware
JavaScript
7
star
29

whichpkg

Locate the path of a specific python module
Python
7
star
30

go-httpheaders

Canonicalized forms of common HTTP headers for Go
Go
7
star
31

python-stathat

A better Python library for StatHat
Python
7
star
32

node_signedcookies

Middleware for Express to sign/unsign cookies with a secret key.
JavaScript
6
star
33

node-mock-udp

Mocking and testing for UDP
JavaScript
5
star
34

sentry-groveio

A plugin for Sentry that logs errors to an IRC room on Grove.io
Python
5
star
35

pgtobq

postgres table -> bigquery table
Go
5
star
36

python-bandpage

The Python library for BandPage.
Python
5
star
37

breadsticks

Boom, you got breadsticks.
Python
5
star
38

ember

experiments with my ember mug
Python
4
star
39

python-zohodocs

Wrapper for Zoho Docs (Writer, Sheet, Show)
Python
4
star
40

twilio-js

Javascript library for Twilio
JavaScript
4
star
41

blart

to observe and report
Go
4
star
42

django-registration

Merge of https://bitbucket.org/ColeVsCode/django-registration/ & https://bitbucket.org/ubernostrum/django-registration
Python
4
star
43

tracker

Exploration with a BitTorrent tracker in Python
Python
3
star
44

emptygif

A little module for serving up an empty 1x1 transparent gif
Go
3
star
45

s3autoindex

autoindex for private s3 buckets
Go
3
star
46

django-locking-south

Prevent South from running concurrently
Python
3
star
47

nginx-and

run nginx... and something else!
Go
3
star
48

imgur-cli

upload clipboard data to imgur
Objective-C
3
star
49

python-svnserve

I'm trying to emulate the svn protocol with the Pythons
Python
3
star
50

libvmod-shards

Shell
3
star
51

jquery-sprite-plugin

Animate a sprite sheet with jQuery
JavaScript
2
star
52

connect-brotli

brotli compression for connect-go
Go
2
star
53

sentry-proxy

Go
2
star
54

python-casscache

Python
2
star
55

weather.withmatt.com

JavaScript
2
star
56

go-memcached-siege

Trying to break memcached.
Go
2
star
57

AS3-Classes

My collection of AS3 classes I wrote years ago
ActionScript
2
star
58

mattrobenolt.github.com

CSS
2
star
59

docker-twemproxy

Twemproxy (nutcracker)
Shell
1
star
60

node-redisq

A wrapper for simple Redis pub/sub in Node
JavaScript
1
star
61

callsite-shim

A shim for other browsers to conform to the CallSite API
JavaScript
1
star
62

python-npm

Python
1
star
63

hosthash

1
star
64

redis-py-contrib

Missing contrib packages for redis-py
Python
1
star
65

7languages

7 languages, 7 weeks, 1 man.
Ruby
1
star
66

update-maxmind

A little library that should be run on a cronjob to update the free MaxMind GeoIP libraries.
Python
1
star
67

withmatt.com

1
star
68

docker-vault

docker images for hashicorp's vault
Makefile
1
star
69

drunk-javascript

wtf Javascript exceptions
1
star
70

commands.py

Awesome python CLI shit, I think.
Python
1
star
71

mime.types

A collection of some normal filetypes for use in Python or Apache.
1
star
72

inboxb.in

1
star
73

jsonpd

jsonp -> esi
Go
1
star
74

django-proofread

Moved to https://github.com/mattrobenolt/proofread
1
star
75

twilio-node

A Node.js module for communicating with the Twilio API and generating TwiML
JavaScript
1
star
76

sublime-text-json-prettifier

1
star