Skip to content

Instantly share code, notes, and snippets.

@adrianholovaty
Created May 20, 2013 03:37
Show Gist options
  • Save adrianholovaty/233f0d05afa0c4af093d to your computer and use it in GitHub Desktop.
Save adrianholovaty/233f0d05afa0c4af093d to your computer and use it in GitHub Desktop.
Fab tasks for AWS setup
from fabric import api as fab
import boto
LOAD_BALANCER_NAME = 'my elb name'
SERVER_USER = 'serverusername'
SSH_KEY_FILE = '/path/to/mykey.pem'
def aws_hosts():
# This assumes your bash_profile has AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set.
# Get a list of instance IDs for the ELB.
instances = []
conn = boto.connect_elb()
for elb in conn.get_all_load_balancers(LOAD_BALANCER_NAME):
instances.extend(elb.instances)
# Get the instance IDs for the reservations.
conn = boto.connect_ec2()
reservations = conn.get_all_instances([i.id for i in instances])
instance_ids = []
for reservation in reservations:
for i in reservation.instances:
instance_ids.append(i.id)
# Get the public CNAMES for those instances.
hosts = []
for host in conn.get_all_instances(instance_ids):
hosts.extend([i.public_dns_name for i in host.instances])
hosts.sort() # Put them in a consistent order, so that calling code can do hosts[0] and hosts[1] consistently.
return hosts
def aws():
fab.env.hosts = aws_hosts()
fab.env.key_filename = SSH_KEY_FILE
fab.env.user = SERVER_USER
fab.env.parallel = True
def update_code():
with fab.cd('/opt/code/myapp'):
fab.run('git pull')
def deploy():
update_code()
fab.run('sudo service gunicorn restart')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment