AWS Project Part - 2: Automated Deployment using Code Pipeline

AWS Project Part - 2: Automated Deployment using Code Pipeline

ยท

3 min read

Lets deploy our app using code deploy.

Create a CodeDeploy application:

We will now deploy our application in code deploy.

GoTo Code Deploy and create an application

Create a Deployment group

Our app will be deployed into deployment group. Deployment group is having one or more ec2 instances where our app will be deployed.

Enter name, service role ARN where it has all the required permissions

Add instance. I have already created one instance

To deploy the app in ec2 we need code deploy agent.

Setup an agent in the ec2 server

Setup with the following script in install.sh file

setting-up-aws-codedeploy-agent-on-ubuntu-ec2 (trainwithshubham.com)

Setting Up AWS CodeDeploy Agent on Ubuntu EC2

change the region name accordingly

#!/bin/bash 
# This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.  
sudo apt-get update 
sudo apt-get install ruby-full ruby-webrick wget -y 
cd /tmp 
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb 
mkdir codedeploy-agent_1.3.2-1902_ubuntu22 
dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22 
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control 
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/ 
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb 
systemctl list-units --type=service | grep codedeploy 
sudo service codedeploy-agent status

We successfully setup our code deploy agent in our instance.

appsepc.yml

we also need appspec.yml file. It is like a configuration file for Code deploy similar to buildspec for code build. The agent will run this appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/start_nginx.sh
      timeout: 300
      runas: root

๐Ÿ‘‡- location: scripts/install_nginx.sh ๐Ÿ‘‡

install_nginx.sh


#!/bin/bash

sudo apt-get update
sudo apt-get install -y nginx

start_nginx.sh

#!/bin/bash
sudo service nginx start

Push all these files to code commit repo by pushing.

Now by using code build we will build the app and al its artifacts will be stored into S3. From s3 code deploy will take the artifacts and runs its app in ec2 server.

Create deployment and copy the artifacst.zip URL

Note : while adding roles to code build and code deploy use below file where it allows permission to code build. and for code deploy give code deploy in the below file if u r getting role error.

You can see below deployment is still in progress

This is because we didn't give permission to ec2 to bring artifacts from S3 and also connect with code deploy.

So create a role with these permissions

We modified the permissions. Now we need to restart our code deploy agent in the ec2 server where our install.sh is there.

Now deploy the app in code deploy

deploy the app , check if it is deployed using instance ipaddress.

Pipeline

Add all the steps into pipeline

Our pipeline is successfully deployed

If we make any changes in the code it will automatically triggers and re start the pipeline. This is Automated deployment.


Thank you for reading my blog

ย