Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Magic Beans - Deploying Django on Elastic Beanstalk

Seb
September 11, 2014

Magic Beans - Deploying Django on Elastic Beanstalk

Seb

September 11, 2014
Tweet

More Decks by Seb

Other Decks in Technology

Transcript

  1. For those who don't know me • Sebastian Vetter •

    Backend Developer @ Snowball One • You can find me on: • twitter: @elbaschid • github: github.com/elbaschid
  2. Deploying Websites Can Be Ugly 1. Set up server infrastructure.

    2. Develop a magical application. 3. Use continuous integration, e.g. Travis 4. On success, manually deploy via scripts
  3. AWS Elastic Beanstalk (EB) • Amazon's PaaS solution • Heroku-style

    deployments • Combines various parts of AWS And now with Docker support
  4. What is Docker? • Isolated processes in userspace • Immutable

    containers • Lightweight images • Git-style container distribution • More on https://www.docker.com/ whatisdocker/
  5. The main steps 1. Push to github 2. Triggers a

    test build on Travis 3. On success: 1. Create deployment artefact 2. Store in S3 3. Beanstalk creates Docker container 4. Deploy to EC2
  6. Options to run/deploy a docker container on EB. • Using

    Dockerrun.aws.json to pull from docker registry • Using Dockerfile to build on the EC2 instance • Using zip archive including both files + more
  7. Dockerfile FROM stackbrew/ubuntu:14.04 RUN apt-get -qq update && \ apt-get

    install -y -q all-the-things && \ curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py && \ python /tmp/get-pip.py ADD www /app/ WORKDIR /app RUN pip install -r deploy/requirements/test.txt EXPOSE 8000 VOLUME ['/var/log'] ADD scripts/start.sh /app/start.sh CMD /app/start.sh
  8. Dockerrun.aws.json { "AWSEBDockerrunVersion": "1", "Volumes": [ { "HostDirectory": "/var/log/my-app", "ContainerDirectory":

    "/var/log" } ], "Ports": [ { "ContainerPort": "8000" } ], "Logging": "/var/log" }
  9. Setting up Travis to release docker image on success. •

    Run the full test suite • After success, build the Docker container • Deploy the new container incl. static files
  10. Putting the magic into beans • No easy to use

    deployment scripts from Amazon • eb: useless and broken • awscli: easy to setup but lots of cli flags
  11. It's Roll-Your-Own Time • Small script beanstalk • Uses beans.yml

    for configuration • Inspired by tools like fig
  12. Settings in beans.yml app_name: my-eb-app bucket_name: my-s3-bucket my-app-env: environment: AWS_ACCESS_KEY_ID:

    AWS_SECRET_KEY: DJANGO_SECRET_KEY: DJANGO_DATABASE_HOST: <RDS host> DJANGO_DATABASE_PORT: <RDS port> DJANGO_DATABASE_NAME: <RDS DB name DJANGO_CONFIGURATION: Test settings: 'command': Timeout: 1000
  13. Using Beansstalk $ python beanstalk.py create_archive <version> $ python beanstalk.py

    release <version> $ python beanstalk.py deploy <env> <version> As an example: export RELEASE_VERSION=$TRAVIS_JOB_ID-$GIT_COMMIT $ python beanstalk.py create_archive ${RELEASE_VERSION} $ python beanstalk.py release ${RELEASE_VERSION} $ python beanstalk.py deploy my-app-env ${RELEASE_VERSION}
  14. Versioning and deployment to EB environment. • Semantic versioning doesn't

    work in CD (<major>.<minor>.<patch>) • How to generate meaningful versions?
  15. What we do: • Use the Travis Job ID •

    And the git commit GIT_COMMIT=$(git rev-parse --short HEAD) RELEASE_VERSION=$TRAVIS_JOB_$GIT_COMMIT
  16. Running the Docker container #!/bin/bash set -e python manage.py migrate

    /usr/local/bin/uwsgi --http :8000 \ --wsgi-file deploy/wsgi/test.py \ --logto /var/log/uwsgi.log
  17. Ideas and improvements • Building docker images on Travis CI

    and alternatives (Circle CI, Wercker). • Running browser tests against the production container instead of live server testcase. • Handling migrations in continiuous deployment. • Deploying with zero downtime.