Skip to content

Instantly share code, notes, and snippets.

View garnaat's full-sized avatar

Mitch Garnaat garnaat

View GitHub Profile
@garnaat
garnaat / gist:5154407
Last active December 14, 2015 21:58
JSON policy for read-only access to an S3 bucket
{
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::pyconprod"]
},
{
"Effect": "Allow",
"Action": ["s3:GetObject"],
@garnaat
garnaat / gist:5154381
Last active December 14, 2015 21:49
Create an IAM Role. See comment below for links to JSON policy files.
import boto
# Create a connection to the Identity & Access Management Service
iam = boto.connect_iam()
# Create a new user
user_data = iam.create_user('pycon')
# Create a new group
group_data = iam.create_group('pythonistas')
@garnaat
garnaat / gist:5154370
Created March 13, 2013 17:35
Configure a internet-connected VPC from scratch
import boto.vpc
import time
REGION_NAME = 'us-west-2'
AMI_ID = 'ami-8e27adbe' # Amazon Linux AMI
conn = boto.vpc.connect_to_region(REGION_NAME)
# Create a VPC
vpc = conn.create_vpc('10.0.0.0/16')
import boto.ec2
import boto.ec2.elb
for region in boto.ec2.regions():
ec2_conn = region.connect()
elb_conn = boto.ec2.elb.connect_to_region(region.name)
# do stuff with EC2 and ELB for this region here
@garnaat
garnaat / gist:4111114
Created November 19, 2012 14:59
Set up simple lifecycle config for a bucket
from boto.s3.lifecycle import Lifecycle, Transition, Rule
import boto
c = boto.connect_s3()
b = c.lookup('mybucket')
t = Transition(days=0, storage_class='GLACIER')
r = Rule('foo rule', 'foo', 'Enabled', 1, t)
l = Lifecycle()
l.append(r)
@garnaat
garnaat / gist:3785118
Created September 25, 2012 23:39
Most concise way to extract list of Instances from a list of Reservations in boto
#
# EC2 returns a list of Reservation objects when you call DescribeInstances.
# Usually, you just want the Instance objects that are embedded in the
# Reservation object. This shows how to use the magic of nested list
# comprehensions to construct a one-liner to get the Instance objects.
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> reservations = ec2.get_all_instances()
>>> instances = [i for r in reservations for i in r.instances]
>>>
@garnaat
garnaat / gist:3762068
Created September 21, 2012 15:09
Disable SSL Certificate verification in boto
[Boto]
# Add this line to your boto config file in the Boto section
https_validate_certificates = False
@garnaat
garnaat / gist:2938586
Created June 15, 2012 20:37
Setting up OrdinaryCallingFormat for S3 in boto
import boto
from boto.s3.connection import OrdinaryCallingFormat
c = boto.connect_s3(calling_format=OrdinaryCallingFormat())
@garnaat
garnaat / gist:2917662
Created June 12, 2012 13:55
Example using boto to create an IAM role and associate it with an EC2 instance
In [1]: policy = """{
...: "Statement":[{
...: "Effect":"Allow",
...: "Action":["s3:*"],
...: "Resource":["arn:aws:s3:::mybucket"]}]}"""
In [2]: import boto
In [4]: c = boto.connect_iam()
In [5]: instance_profile = c.create_instance_profile('myinstanceprofile')
In [6]: role = c.create_role('myrole')
In [7]: c.add_role_to_instance_profile('myinstanceprofile', 'myrole')
@garnaat
garnaat / gist:2890351
Created June 7, 2012 17:48
Use the callback parameter to watch progress of an upload to S3 via boto
In [1]: import boto
In [2]: c = boto.connect_s3()
In [3]: b = c.lookup('stats.pythonboto.org')
In [4]: k = b.new_key('test1234')
In [5]: def mycb(so_far, total):
...: print '%d bytes transferred out of %d' % (so_far, total)