Package: ec2_deploy
EC2 Deploy is a convenient deployment tool to facilitate server setup and code deployment across multiple AWS EC2 instances.Why not use Git for deployment?Git is a powerful tool and over the years I have experimented deploying code with it.
Using a bare repo and git hooks was ideal for some time, but as my team and project grew this got unmanageable.
Puppet is amazing, perfect in 90% of the cases, but for smaller clients can get tedious.
EC2 Deploy is NOT a replacement for any of these services!Key FeaturesCreate EC2 instances based on server template.Creates environment variables for different instances staging/production for use in your project.Deploys your codebase across multiple servers.Installation StepsYour project must have arequirements.txtfile even if you don’t have any!Your project must be usingGitwith amasteranddevelopbranch.
Master is used for release to production where develop is used for your staging server.1 - Install The EC2 Deploy PackageYou will need topipinstall EC2 Deploy within your virtual environment.
To get the latest stable release from PyPi enter the following:pip install ec2-deploy2 - Import Fabric CommandsTo make use of the Fabric commands within EC2 Deploy you must import them within your ownfabfile.pyfile.
EC2 Deploy will automatically install Frabric as a dependency if you don’t have it already.
Once you have yourfabfile.pyadd the following import to the top of the file:from ec2_deploy.fab import *3 - Pick Server TemplateThe next task is to pick your server template from the directoryserver-templateand edit the following files to reflect your own remote server setup.For example the templateAWS_Ubuntu14_04_LTSruns a Nginx, Gunicorn and Supervisor setup for Django Applications.default- Nginx Site File.supervisord.conf.gunicorn.conf.py.wsgi.py- Remote server WSGI.You can create your own server template by copying an existing one, then modifying the filetasks.pyas required for your own setup.Environment VariablesEC2 Deploy gives you a very useful environment variable file withpython-dotenv, which
reads values from a local .env file and loads them as environment variables for your application.For your remote server you set these variables invars_production.envandvars_staging.envfound within the
server template used. EC2 Deploy will create your server environment variables based on the command used during
deployment or server creation.For examplefab staging deploywould copy the environment variables from the filevars_staging.envwithin
your server template folder as.env.At a minimum you must set the following environment variable withinvars_production.envandvars_staging.env:EC2_DEPLOY_SERVER_REPO="/srv/[APP_FOLDER]"Replacing[APP_FOLDER]with the folder where your application is located on the remote server.
You can of course use any the same file to store your own custom variables for both your
staging and production environments.Running a local version of your application requires you to create your own.envfile with some additional variables.Examplerequiredlocal.envfile:EC2_DEPLOY_AWS_SECRET_KEY = ''
EC2_DEPLOY_SERVER_REPO="/srv/[APP_FOLDER]"
EC2_DEPLOY_AWS_KEY = ''
EC2_DEPLOY_AWS_PRIVATE_FILE = ''
EC2_DEPLOY_LOCAL_REPO = ''
EC2_DEPLOY_TEMPLATE = 'ubuntu14custom'
EC2_DEPLOY_AWS_USER = 'ubuntu'List of Fabric CommandsThere are a number of convenient Fabric scrips available to facilitate code deployment and other server tasks on AWS EC2.Note:Local environment must be configured correctly to run Fabric tasks(see environment variables above).fab staging/production instance- Creates an EC2 instance from an AMI and configures it based on template.Creates new EC2 instance.Updates OS.Builds essential packages.Deploy Master branch.Installs requirements.Server Setup, Gunicorn, Nginx and ports.Celery Setup.Restarts all services.fab staging/production deploy- Deploys API codebase.Deploy form master(staging)/release branch. Creates a release zip.Updates environment variables.Restarts all services.fab staging/production celery_setup- UpdatesSupervisorandCeleryprocesses fromcelery.confDeploy form Master branch.Updates each Celery worker start process.Restarts Supervisor and Celery worker on the server.fab staging/production environment_vars- Sets or Updates system environment variables.fab staging/production reload- Restarts services.fab staging/production update- Update OS site packages.fab staging/production requirements- Runspip installrequirements.txt.fab staging/production serversetup- Runs all the build server tasks.fab staging/production create_swap- Creates Swap Memory