This tutorial covers:
- Setting up a Django application on AWS
- Using the Elastic Beanstalk command line interface
- Creating a continuous deployment pipeline
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.
In addition to a basic understanding of Django and Python, you will need these items to get the most from this tutorial:
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 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-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:
The domain name is the value of the
Once the application has been deployed, add the
CNAME property to the
ALLOWED_HOSTS setting in
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
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: firstname.lastname@example.org eb: email@example.com 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
Next, select the
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:
The value of
AWS_DEFAULT_REGION is specified in the
.elasticbeanstalk/config.yml file in the project, under the
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
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
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.