Skip to content

Instantly share code, notes, and snippets.

@objectified
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save objectified/78f2f16066ea8327c9e1 to your computer and use it in GitHub Desktop.
Save objectified/78f2f16066ea8327c9e1 to your computer and use it in GitHub Desktop.
Minimal Fabric script to setup an AWS Ubuntu instance and run some commands on it
import time
from fabric.api import run, execute, task, settings, sudo
import boto.ec2
@task
def create_aws_server():
conn = boto.ec2.connect_to_region('eu-west-1')
reservation = conn.run_instances('ami-blabla',
instance_type='t2.micro',
security_group_ids=['sg-bladiebla'],
key_name='nameofkey-given-inconsole'
)
instance = reservation.instances[0]
while instance.state != 'running':
time.sleep(3)
instance.update()
print '\nNow the state of the instance is: %s' % instance.state
print 'Its IP address is: %s' % instance.ip_address
with(settings(host_string=instance.public_dns_name, key_filename='/path/to/ssh/keys.pem', user='ubuntu', connection_attempts=10)):
execute(install_apache)
execute(install_apache)
def install_apache():
sudo('apt-get update')
sudo('apt-get install -y apache2 libapache2-mod-php5')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment