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.

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:

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.

Application on localhost

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:

  1. Deploy the app to Elastic Beanstalk
  2. Create a configuration file to build the application on CircleCI
  3. Push the project to a repository on GitHub
  4. Set up the project on CircleCI
  5. 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.

Create an AWS user with programmatic access

To configure AWS EB CLI you need an AWS user with programmatic access. Skip to the next step if you already have an AWS IAM user with access keys. To add a programmatic user, go to the AWS Add User page. Add a user name and click next.

Add user with Access

On the permissions page, select “AdministratorAccess-AWSElasticBeanstalk” and click on next.

set user permission

On the “review and create” page, ensure you have a view similar to the one below and then click on “create user”.

Create new AWS user

You would be a page with the list of AWS users. Click on the new user to add programmatic access to the user.

List of AWS users

Next click on the “Security credentials” tab, and then on the “Create access key button”.

User security credentials tab

In the appearing list of options, select the “Command Line Interface (CLI) option”.

Acces key best practices

Lastly you would be shown the newly generated access key. Click the “Download .csv file” to download the credentials in a CSV file format.

AWS user access keys

Configure the EB CLI

Next, initialize your EB CLI repository using this command:

eb init -p python-3.9 exchangeRatesApi

If you do not have the credentials already configured, you would be prompted to provide the aws-access-id and aws-secret-key. Enter the values contained in the downloaded CSV file from the previous step. This creates a .elasticbeanstalk folder with a config.yml file containing AWS EB configurations.

Create a new AWS EB 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.

Live app on AWS

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.1.1
  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.9
            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.

Add context

Next, select the aws-credentials context.

Add context variables

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.

Add project

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.

Run workflow 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.

Read more posts by Olususi Oluyemi