Deploy Dockerize Django App on AWS ECS Fargate using GitHub Actions and Terraform

rahulwath
4 min readJan 2, 2025

--

Table of contents:

  1. Create an Ec2 Ubuntu instance
  2. Install Packages on Ec2 Instance (Docker, AWS CLI)
  3. Configure AWS CLI on EC2, Also on the Local Machine
  4. Setup Self Hosted runner on Ec2
  5. Create db secret(Password) on AWS Secret Manager, with name ‘/dev/djangoapi/db/password’
  6. create ECR Repository on AWS
  7. Create CI/CD Pipeline using Github Actions
  8. GitHub Actions (Build)
  9. Create infra using Terraform
  10. GitHub Actions (Build and Deploy)
  11. Add SSL and domain.

All the code is present in github repo → https://github.com/rahulwath/django-aws-ecs-terraform

1. Create an Ec2 Ubuntu instance

The first step in deploying a Django application on AWS is to create an EC2 instance. An EC2 instance is a virtual server that runs in the AWS cloud. To create an EC2 instance, you will need to log in to your AWS account and go to the EC2 dashboard.

** Make sure you created in the same region where you will create infrastructure.

2. Install Packages on Ec2 Instance (Docker, Terraform, AWS CLI)

SSH to Github-Ubuntu-runner instance (Created on 1st step) and run below command.

Install Docker

sudo apt-get update 
sudo apt install docker.io -y
sudo usermod -aG docker ubuntu
newgrp docker
sudo chmod 777 /var/run/docker.sock

Install AWS CLI

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt-get install unzip -y
unzip awscliv2.zip
sudo ./aws/install

3. Configure AWS CLI on EC2

Before You configure AWS, Get access and secret keys from IAM user.

also save the CSV file for later use.

aws configure

4. Setup Self Hosted runner on Ec2

Now Go to GitHub Repository and click on Settings -> Actions -> Runners

Click on New self-hosted runner

Now select Linux and Architecture X64

Use the below commands to add a self-hosted runner

Note: In pic, Commands are related to my account, Use your commands, they will appear on your GitHub self-hosted runner Page.

Now SSH to your AWS instance (Github-Ubuntu-runner) to connect with your Instance And Past/Run provided commands.

Auto restart or auto connect to github action if EC2 instance is stop/restart.

Create systemd Service File on EC2.

sudo nano /etc/systemd/system/run-script.service

Paste the below content

[Unit]
Description=Script Runner Service
After=network.target

[Service]
Type=simple
Restart=always
User=ubuntu
WorkingDirectory=/home/ubuntu/actions-runner
ExecStart=/home/ubuntu/actions-runner/run.sh
[Install]
WantedBy=multi-user.target

Run These Scripts.

sudo systemctl daemon-reload 
sudo systemctl enable run-script
sudo systemctl start run-script
sudo systemctl status run-script

Now, you can see self hosted runner is present on github action.

5. Create db secret(Password) on AWS Secret Manager, with name ‘/dev/djangoapi/db/password’

First, you have to configure aws cli locally and run a command.

aws ssm put-parameter --name "/dev/djangoapi/db/password" --value "admin123" --type String --tags '[{"Key":"Region","Value":"ap-south-1"},{"Key":"Environment", "Value":"Dev"},{"Key":"Service", "Value":"admin123"}]'

6. Create an ECR Repository on AWS

We have to create an ecr repo with the name django-app.

Go to ECR — → create repository

Now update ECR repository URI on root/main.tf file

Before you create GitHub actions.

Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in github actions variables.

you have secreate key and access key alreay , so add there.

7. Create infra using Terraform

Make sure Terraform is installed on your local system. Use the link to install according to your system

terraform workspace new staging
terraform workspace select staging
terraform init
terraform plan
terraform apply -auto-approve

9. GitHub Actions (Build and Deploy)

Push all the code in github repo and its will trigger the github action.

10. Access Django app using ALB DNS name.

Stay Tuned!

Be sure to follow and subscribe for more updates and upcoming blogs.

--

--

rahulwath
rahulwath

Written by rahulwath

Experienced DevOps pro adept in merging operations & development for rapid code delivery. Skilled in Cloud, monitoring, & DevOps across Mac, & Linux.

No responses yet