This tutorial covers:
- Setting up a Django application on AWS
- Using the Elastic Beanstalk command line interface
- Creating a continuous deployment pipeline
Your software development team has an enormous number of tools available to them. Some older tools are being used in new ways, which has inspired the creation of more new tools to choose from. For example, JavaScript has grown from a language used to add interactivity on websites to a full-stack language for both frontend and backend needs. JavaScript has paved the way for Express, Nest.js, and many others.
Just as dev teams can now build APIs with JavaScript, they can also build web applications powered by Python. This is supported by mature frameworks like Flask and Django. And more tooling providers are adding support for Python-based applications in their service offering.
In this article, I will guide you through deploying a Django application to AWS Elastic Beanstalk. We will use the CircleCI AWS Elastic Beanstalk orb to handle authentication and deployment.
The AWS Elastic Beanstalk helps you deploy and manage applications in the Amazon Cloud without having to learn about the infrastructure that runs those applications. Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.
Prerequisites
In addition to a basic understanding of Django and Python, you will need these items to get the most from this tutorial:
- GitHub account
- CircleCI account
- AWS account
- AWS Elastic Beanstalk CLI installed on your computer
Our tutorials are platform-agnostic, but use CircleCI as an example. If you don’t have a CircleCI account, sign up for a free one here.
Cloning the demo project
Clone the sample Django project by running this command:
git clone https://github.com/CIRCLECI-GWP/django-exchange-api exchangeRateApi
Next, go to the root of the new folder created by the previous command. Set up the project by running these commands:
cd exchangeRateApi
pip install -r requirements.txt
python manage.py runserver
By default, your application will be served to http://127.0.0.1:8000/
. You can review the JSON response at that endpoint.
Reviewing the deployment strategy
Now that you have the application running locally, it is a good idea to review your deployment strategy. There is no need for any project code changes other than including the configuration file to set up deployment for CircleCI.
Here is a list of the deployment strategy steps:
- Deploy the app to Elastic Beanstalk
- Create a configuration file to build the application on CircleCI
- Push the project to a repository on GitHub
- Set up the project on CircleCI
- Provide Amazon credentials and authenticate using the AWS Elastic Beanstalk orb
Deploying the app to Elastic Beanstalk
In the project directory, create a new directory named .ebextensions
.
mkdir .ebextensions
In the .ebextensions
directory, create a new file named django.config
and add this to it:
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: exchangeRateApi.wsgi:application
The WSGIPath starts with the directory containing your wsgi.py script. It will be used to start your application.
Next, initialize your EB CLI repository using this command:
eb init -p python-3.8 exchangeRatesApi
If you did not provide an access-id and secret-key when installing the awsebcli
, you will see an error similar to this:
ERROR: The current user does not have the correct permissions.
Reason: Operation Denied. The security token included in the request is invalid.
You have not yet set up your credentials or your credentials are incorrect You must provide your credentials.
Provide a valid aws-access-id
and aws-secret-key
to complete the set up process. If you don’t have one already, you can follow the steps in the next section.
Add a user with programmatic access
Go to the AWS Add User page. Select a user name and AWS access type.
Select Access key - Programmatic access for accessing your Beanstalk environment using the CLI.
Next, select the permissions for the user.
The previous screenshot shows the minimum needed to manage your application on AWS Elastic Beanstalk.
Click Next twice to create the new user.
You will be greeted with a success message along with your user credentials. This page is displayed just once so, make a note of the Access Key ID and the Secret access key. These will be used later on in this tutorial. You can download a CSV file that contains the credentials.
Use these credentials to authenticate awsebcli
and continue with the deployment process.
Create a new environment
Create a new environment using this command:
eb create django-env
This creates an Elastic Beanstalk environment named django-env
; it takes about five minutes. Once the process is completed, you can find the domain for the new environment by running this command:
eb status
The domain name is the value of the CNAME
property.
Once the application has been deployed, add the CNAME
property to the ALLOWED_HOSTS
setting in exchangeRateApi/settings.py
.
ALLOWED_HOSTS = ['127.0.0.1', 'localhost','INSERT_YOUR_CNAME_HERE']
Next, deploy the changes. Because you are using git for version control, you will need to commit your changes before running eb deploy
.
git add .
git commit -m "Add EBS config"
eb deploy
Now, run the eb status
command again. This time, the health status should be GREEN. Paste the CNAME
property in your browser to review your API.
Create CircleCI configuration
At the root of the project, create a new folder named .circleci
. In this folder, create a new file named config.yml
and add this:
version: 2.1
orbs:
python: circleci/python@2.0.3
eb: circleci/aws-elastic-beanstalk@2.0.1
jobs:
build:
description: "Setup Django application and run tests"
executor: python/default
steps:
- checkout
- python/install-packages:
pkg-manager: pip
- run:
name: "Run tests"
command: python manage.py test
workflows:
build-and-deploy:
jobs:
- build
- eb/deploy:
context: aws-credentials
application-name: exchangeRatesApi
environment-name: django-env
platform-version: python-3.8
requires:
- build
This configuration uses two orbs provided by CircleCI. The python
orb gives access to a Python environment (with pip installed), which is used to test the updates before deploying them to AWS. All of this happens in the build
job. The eb
orb deploys the changes to the environment you created in an earlier step.
After the workflow finishes running the build
job, the deploy
job from the eb
orb deploys the changes. The requires
key ensures that the build
job runs first. The application name, environment name, and platform version are specified for the job. A context named aws-credentials
is passed to the job. You will create this context later in the tutorial.
Commit and push your changes to git:
git add .
git commit -am "Add CircleCI configuration"
git push origin main
Add context for AWS credentials
From your CircleCI dashboard, go to the Organization Settings page. Select Contexts, then click the Create Context button. Enter a unique name for your context. Your context appears in a list with security set to All members
. That means that anyone in your organization can access this context at runtime. As specified in the .circleci/config.yml
configuration for this tutorial, the context name should be aws-credentials
.
Next, select the aws-credentials
context.
Click the Add Environment Variable button and enter the variable name and value. Then click the Add Variable button to save. The aws-credentials
context requires 3 environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
The value of AWS_DEFAULT_REGION
is specified in the .elasticbeanstalk/config.yml
file in the project, under the global
section.
Connecting the application to CircleCI
Next, you need to set up a repository on GitHub and link the project to CircleCI. Review Pushing a project to GitHub for instructions.
Log in to your CircleCI account. If you signed up with your GitHub account, all your repositories will be available on your project’s dashboard.
Click Set Up Project next to your django-exchange
project.
You will be prompted to either write a new configuration file or use the existing one in your project. Select the existing one and enter the name of the branch where your code is housed on GitHub. Click Set Up Project.
Your first workflow will start running and complete successfully.
To confirm that your workflow was successful, you can open your newly deployed app in your browser using the CNAME
property.
Conclusion
In this tutorial, I have shown you how to set up a CI/CD pipeline for a Django API using GitHub, CircleCI, and AWS Elastic Beanstalk. Using what you have learned here, you can implement best practices for automated deployment of Python projects. This creates opportunities for your team if they have felt they couldn’t use their programming language of choice for projects with automated deployments.
The entire codebase for this tutorial is available on GitHub.
Oluyemi is a tech enthusiast with a background in Telecommunication Engineering. With a keen interest in solving day-to-day problems encountered by users, he ventured into programming and has since directed his problem solving skills at building software for both web and mobile. A full stack software engineer with a passion for sharing knowledge, Oluyemi has published a good number of technical articles and blog posts on several blogs around the world. Being tech savvy, his hobbies include trying out new programming languages and frameworks.