Python, Docker and Amazon Elastic Beanstalk

When delving into the world of infrastructure there are many choices of cloud platform, ranging from completely custom setups, to off-the-shelf solutions such as Google AppEngine.

Today i'm looking at how off-the-shelf infrastructure (Amazon Elastic Beanstalk in particular) can speed up DevOps and deployments, and reduce the overhead on maintenance.

For my particular scenario I wanted to replicate a common technology stack I like to use:
  • Backend: Python + Django
  • Frontend: HTML + JavaScript
  • Task runners: NPM + Gulp
  • Infrastructure: Docker + AWS
There are many ways to implement this particular stack, but Amazon Elastic Beanstalk promises to give you a one-click deploy solution. So let's give it a go!

First step was to install the Amazon Elastic Beanstalk Client using the command:

brew install awsebcli

If you already have Django installed, you can auto-generate a project using the command:

django-admin startproject mysite

You are able to test your django project locally with python runserver:

cd mysite
python manage.py runserver 0.0.0.0:8080

Next we need to add a Docker file at /mysite/Dockerfile. This will tell docker to extend from a pre-configured Amazon base Docker image. So add the following lines to the file:

# Use the AWS Elastic Beanstalk Python 3.4 image
FROM amazon/aws-eb-python:3.4.2-onbuild-3.5.1

# Expose port
EXPOSE 8080

If you prefer you can write your own custom Docker images, there are plenty of examples on github. We can now initiate our amazon project using the command (follow the steps to complete):

eb init

Your mysite/ folder should now contain an .elasticbeanstalk sub-folder containing the settings you just entered. This will be used for building and deploying to amazon. Before you run the Docker container you should ensure you have a .dockerignore file containing:

.elasticbeanstalk/*
.git
.gitignore
.pyc

Without this, Docker will copy ALL files and could cause build/performance issues. You can now run the docker container using the handy Elastic Beanstalk Client shortcut:

eb local run

Or if you'd prefer, you can use the docker commands directly:

docker build -t python-docker-amazon .
docker run -it -p 3000:8080 python-docker-amazon

To deploy your application you can use the Elastic Beanstalk Client commands:

eb start
eb deploy

A very simple intro into using Elastic Beanstalk!

You can view the completed project on github here:
https://github.com/kmturley/python-docker-amazon

I have also created a Flask branch to show how it works with multiple Python frameworks:
https://github.com/kmturley/python-docker-amazon/tree/flask

No comments:

Post a Comment