Deployment on EC2 with AWS CodePipeline

Tameem Rafay
5 min readJan 18, 2024

--

In this article, we will review how you deploy the code on an EC2 instance using the AWS Code Pipeline. We will streamline the whole process deployment using the AWS Code pipeline. When the user pushes the code to his repository it will trigger the pipeline and it will deploy to the server. Most importantly I extensively focus on the commands that will be very helpful in checking the logs of the pipeline and the scripts that are present in your Appspec.yml file.

Setup your Code Pipeline

Setup the CodePipeline is very simple just you have to go to the CodePipeline service and start creating your first pipeline. Here I attached the final screenshot of the code pipeline stages. Let’s start to create your first pipeline

Code pipeline stages

0: — Create an IAM role for EC2

Before starting to create the CodePipeline, first, we will create the role for the EC2 instance so that the code deploy agent that we will install on the EC2 (later on in this article) can fetch the artifacts of your code from the S3 bucket.
Goto → IAM Service → Roles → Assing permissions of GET & List S3 buckets

After creating the role, attach that role to the EC2 instance.

Attach role with EC2

1: — Source Stage (Fetch the code from your repository)

Source stage of CodePipeline

Now, let's start to create your first pipeline. Here I create two stages in the pipeline. The first one is the Source stage which will fetch the code from Git and store that artifact on the S3 bucket. I have connected my GitHub account and created the trigger to run this code pipeline stage when the code is pushed to the master branch.

2: — Deployment Stage (Deploy the code to your EC2 instance)

Deployment stage

This second stage is the deployment stage which will get the artifacts from the s3 that you have fetched in your first stage and then it will deploy these artifacts onto your EC2 instance using your appspec.yml file. Moreover, in this stage, I have created the deployment group in which I have only included one EC2 instance. Here you can also add multiple instances if you have set up the autoscaling group but for simplicity, I have only included only one instance.

3: — Configuring the Code-deploy-agent on your EC2 instance

Once you set up your pipeline, it’s time to configure the CodeDeployAgent on the EC2 server. The deployment agent automates the process of deploying code changes to the EC2 instances. This helps to reduce manual errors and streamline the deployment process. This agent may log deployment activities that are extremely helpful in debugging the issues.

Since I selected the Amazon Linux machine while creating the EC2 instance so, I have written some of the commands to set up the code deploy agent on your instance. These commands will work fine on the Linux machine type. So, if you have any other type of machine then feel free to check the alternate commands for installation.

sudo yum update -y
sudo yum install -y ruby
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto

Great! 😊. Now you have successfully installed the code-deploy agent. Now, it's your turn to start and check the status of your agent and also to fix the issues if your code-deploy-agent is not working fine.

I have written very helpful commands that you can use for debugging your codeDeploy agent issues without any hassle.

sudo systemctl start codedeploy-agent 
sudo systemctl restart codedeploy-agent //restart the agent
sudo systemctl status codedeploy-agent //check status of agent


//CodeDeploy agent starts automatically when your instance is restarted
sudo systemctl enable codedeploy-agent

NOTE: ✔️ → If you have attached the role with the EC2 instance after installing and starting the code-deploy agent then make sure to restart your code-deploy-agent. Otherwise, you might get the error that your EC2 instance has not attached any role. After all the installations, check the status of the code deploy agent.

sudo systemctl status codedeploy-agent

Check the logs of code-deploy-agent

It is very crucial to check the logs of your agent to make sure your deployment will run smoothly without any errors.

cat /var/log/aws/codedeploy-agent/codedeploy-agent.log

Check the logs of your appspec.yml file

The other important logs you usually check are the logs of your appspec.yml file that has the scripts to run during your deployment lifecycle.

cat /opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log

Here is the example of your appspec.yml file that has some scripts that will run during your deployment process. So, to check the logs of these scripts you can use the above command to make sure everything works fine.

version: 0.0
os: linux
files:
- source: /
destination: /var/www/[project]-launch-backend
file_exists_behavior: overwrite

hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/after_install.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/application_start.sh
timeout: 300
runas: root
Deployment Details

Once everything is set up then you can check the deployment details and check if your deployment is completed or if you have some errors during your deployment.

Related Article:

Monitoring AWS EC2 Instances: Creating Alarms for Disk, RAM, and CPU Usage

--

--