AWS DevOps for your Django project: Automate your CI-CD with AWS CodeCommit & GitHub in simple steps.
Published: 2022-09-16 13:50:25

This guide covers all steps needed for setting up your CI-CD workflow with AWS CodeCommit and GitHub under the hood. We will clone a sample django application, configure CodeDeploy agent files and configure CI-CD pipeline in AWS.

_Link to sample django project:_ [GitHub](https://github.com/vinclairvoyant/django-aws_cicd)

**Contents**

1. Clone GitHub repo (Optional)
2. Preparing Django project files (Optional)
3. Create yaml files to automate AWS CI-CD (Optional)
4. Create roles in AWS
5. Launching EC2 instance for AWS CI-CD
6. Configure AWS Code Deploy
7. Configure AWS Code Pipeline

**1. Clone GitHub Repo**

- Open your chosen command-line interface
- Create new project folder --> 

```
$ mkdir folder_name
```
- Go in to your project folder -->

```
 $ cd folder_name
```
- Clone the remote repository --> 

```
$ git clone git@github.com:vinclairvoyant/django-aws_cicd.git
```
**2. Preparing Django project files**
_For those who have cloned repo need not require any changes to the django files and can skip this step but for others who are deploying their own django project shall continue with below steps:_

If you are deploying your own django application then make sure to allow all hosts by Changing ALLOWED_HOSTS in your django settings file as shown below. (Not recommended for security reasons, eventually we want to change this to an IP)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/709t1h3vhoyqswaa4ome.jpg)

Make sure you have requirements.txt file for installing required dependencies.


**3. Create CodeDeploy yml files to automate AWS CI-CD**

For this basic tutorial we will create 3 files appspec.yml, before_install.sh & after_install.sh to automate the deployment process handled by AWS CI-CD.

Folder/file structure:

![Folder/file structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4kjwjg33b9vs224wqxt.jpg)

Create appscpec.yml file inside your root project directory where manage.py file is.

> Step 1: Create CodeDeploy appspec.yml file    

```
version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu/django-aws_cicd
hooks:
  BeforeInstall:
      - location: scripts/before_install.sh
  AfterInstall:
      - location: scripts/after_install.sh
```

`mkdir scripts
vi scripts/before_install.yml`

> Step 2: Create before_install.yml file

```
#!/usr/bin/env bash

# clean codedeploy-agent files for a fresh install
sudo rm -rf /home/ubuntu/install

# install CodeDeploy agent
sudo apt-get -y update
sudo apt-get -y install ruby
sudo apt-get -y install wget
cd /home/ubuntu
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
sudo chmod +x ./install 
sudo ./install auto

# update os & install python3
sudo apt-get update
sudo apt-get install -y python3 python3-dev python3-pip python3-venv
pip install --user --upgrade virtualenv

# delete app
sudo rm -rf /home/ubuntu/django-aws_cicd
```
`vi scripts/after_install.yml`

> Step 3: Create after_install.sh file

```
#!/usr/bin/env bash

# kill any servers that may be running in the background 
sudo pkill -f runserver

# kill frontend servers if you are deploying frontend
# sudo pkill -f tailwind
# sudo pkill -f node

cd /home/ubuntu/django-aws_cicd/

# activate virtual environment
python3 -m venv venv
source venv/bin/activate

install requirements.txt
pip install -r /home/ubuntu/django-aws_cicd/requirements.txt

# run server
screen -d -m python3 manage.py runserver 0:8000
```
Before we start setting up AWS CI-CD we need to ensure all changes made locally inside django files are pushed to github.

## **AWS CI-CD Configuration:**

Let's get started configuring AWS for CI-CD pipeline, for this we are going to be using 3 services mainly IAM roles, CodeDeploy & CodePipeline.

**4. Create roles in AWS**


We want to create 2 IAM roles:

**AmazonEC2RoleforAWSCodeDeploy** provides EC2 access to S3 bucket to download revision:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54s252m638o8bdvvl53p.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btddn0pxw4r7b27rcdv5.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i90nnizvr9fcu6kat7ek.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo0b47knme1iitkwti16.jpg)

**AWSCodeDeployRole** for CodeDeploy service access to expand tags and interact with Auto Scaling on your behalf.


![2.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0jyy7q2x5a9vm0r316b.jpg)

![2.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/op1hnex6cckkj3wq4agu.jpg)

![2.3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9a236em424nvb40x1own.jpg)

![2.4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rm9zqs70gqq1ii8e982h.jpg)

![2.5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e19cah7wzs1k9y6pqtpi.jpg)

![2.6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/veja4dsalb1bzmfgtcdp.jpg)

**5. Launching EC2 instance for AWS CI-CD**

When launching a new EC2 instance ensure to add IAM