TASK - Deploy Django App using AWS Code Pipeline

Create an EC2 server.

Pick any Django or python-based sample app from google or create it on your own.

Deploy that on the EC2 server and use Nginx as a web server.

Once the site is up and running create a GitHub repository and push the code into that repository.

Create an AWS Code Pipeline that will build the code and deploy it on the EC2 server.

Attach an Elastic IP to the EC2 instance and apply the domain and SSL.

ADDITIONAL TASK

Once the Pipeline start working properly through Github, deploy it using code commit.

Attach load balancer and auto-scaling to it.

Definition Of Done!

The site should be accessible with the IP on port 80.

whenever any changes would be made in the code then the pipeline will trigger automatically and deployment should happen on the server.

Explore how the deployment can be done with zero downtime.

Step 1 Create an EC2 server

Login to AWS Management Console and open EC2 service.

You can either go to Services -> Compute -> EC2 or type EC2 in the search bar and hit enter. Once you see EC2 option click on that.

This will lead you to EC2 dashboard like below.

To Create EC2 Instance in AWS, Click Launch Instance

Create/Select a Key Pair - Select Create a new key pair option – > name your key pair -> Download Key Pair -> Launch Instance , Download keypair and click Launch Instances

image

Step 2 Pick any Django or python-based sample app from google or create it on your own and Deploy that on the EC2 server and use Nginx as a web server

I am picking up the default Sample app from the Django and followed this simple article to install the Django application using the gunicorn and done the configuration to run this on port 80

image

then pushed all the code files to the github

Step 3 Create an AWS Code Pipeline that will build the code and deploy it on the EC2 server

Firstly, create 2 roles in your IAM , one for the EC2 and another one is for the Code deploy, give permmissions accordingly, and attach the ec2 role to your working EC2 Instance

image

image

Image 1 is of the code deploy role and 2nd is of the EC2 role and attach your IAM role

image

Now, run these Commands on your EC2 Instance to install the codedeploy-agent

sudo apt-get install awscli
sudo apt-get install ruby2.0
cd /home/ubuntu
sudo aws s3 cp s3://bucket-name/latest/install . --region region-name sudo chmod +x ./install
sudo ./install auto
And check the status of the codedeploy agent. it should be in active state.

Now, we will create a codepipeline for the same.

image Write the Name of your application you have created in the code deploy

Let’s navigate to CodePipeline via AWS Management Console and click on Create pipeline. (A new service role for your new pipeline will be created on this page as well. We can let AWS create a new S3 bucket to store artifacts or if you would like to store artifacts on an existing S3 bucket in the same region, you can select “Custom location” and then select your preferred S3 bucket)

And connect your pipeline to the github and enter the repository name that you have done on your github account.

Now create a appspec.yml file for your application, for me it is on my this github repo and then start the pipeline

image

This status should be successed

Step 4 Attach an Elastic IP to the EC2 instance and apply the domain and SSL.

Purchase a Elastic IP for your EC2 instance and mention that IP with your Domain in your domain name registerer to target it to the Domain. I am using the freenom.com here and the purchased domain is hitting the url that i have purchased

image

and for the SSL - https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

image

ADDITIONAL

Step 1 – Pushed the Same Github code in code deploy Create a Repository in code commit and from the github as we done before, clone the same in code in code commit using the git command and for the authentication for the code commit, Go into the IAM -> Users -> Security Credentials -> Git Credentials for AWS code Commit
Get the credentials for the code commit to push the code into the newly created repo.

image

Then Create a pipeline as the same way we created for github, we just have to do one change is - Go to the Console create the pipeline , and choose the option code commit in the source stage instead of github and then enter the code commit repo details. image

Now, release the changes in the pipeline as we done in the github case.

Note – If facing any kind of error while deploying that is related to the path not found, once check that is the code is getting into the S3 or not. If not, try to do with a freshly created Deployment group, It should work

Make some changes in the code commit repo it should automatically trigger the pipeline and check if the changes occurs in the server or not.

image

Step 2 – Attach load balancer and auto-scaling to it

Create the AMI from your instance so that we can use that image to create more instance while auto scaling Go to the AWS console -> EC2 -> target group Create the target group for your instance’s AMI, select the AMI you created and then go to the Load balancer and attach the target group we created from the AMI of our instance.

image

With the help of this check if your load balancer is working or not

image

Then go to the target group and check the instance state is healthy or unhealthy

image

Now Go to the Auto scaling, and go to the launch configuration and click on create launch configurations

image

Enter the details of your roles, IAM instance image and key pair info so that while creating the new instances, it can use those info to create the instances.

image

And then go to the Autoscaling and chose the configuration template that we created earlier in the configuration and enter the number of instance you needed to auto scale.

image

image

Here, I have taken three instance, wait to get the instance prepared and now in the target group check the state of all the instances

image

All the instance are in the healthy state. edit in the autoscaling to 1 or 4 to check if it is working properly

THANK YOU SHRUTI :)