Your CI/CD pipeline on AWS - Part 3

Your CI/CD pipeline on AWS - Part 3

What is CodeDeploy?

AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.

CodeDeploy can deploy application content that runs on a server and is stored in Amazon S3 buckets, GitHub repositories, or Bitbucket repositories. CodeDeploy can also deploy a serverless Lambda function. You do not need to make changes to your existing code before you can use CodeDeploy.

Task-01 :

  • Read about Appspec.yaml file for CodeDeploy.

  • Deploy the index.html file on the EC2 machine using nginx

  • you have to set up a CodeDeploy agent in order to deploy code on EC2.

An AppSpec file, often named appspec.yaml, is a YAML-formatted or JSON-formatted file that provides instructions to CodeDeploy on how to deploy an application revision. It's a crucial component of the deployment process, ensuring that your application's files, scripts, and other resources are copied to the correct locations and that any necessary actions are taken during the deployment lifecycle.

Example of an AppSpec file:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/
hooks:
  BeforeInstall:
    - location: install_dependencies.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: change_permissions.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: start_server.sh
      timeout: 300
      runas: root
  ApplicationStop:
    - location: stop_server.sh
      timeout: 300
      runas: root
  • Deploy the index.html file on the EC2 machine using nginx

Day 50 and Day 51 are all about CodeCommit and CodeBuild respectively.

When you use CodeDeploy, the first step you need to take is to create an Application.

In the AWS Management Console, go to CodeDeploy, then go to Applications and click on "Create application".

When you create an application, you will need to select which platform to deploy your code to.

Provide a name for your application and for the Compute platform, choose EC2/On-premises, then click on "Create application"

Before we continue, we need to give access to deployment to access the AWS resource so we need to create an IAM role.

Go to the IAM service and create a role named "code-deploy-service-role" with the below permissions.

You will need to create an EC2 instance on which you want to deploy the index.html file.

Create a "deployment group". On EC2, a deployment group is just a group of servers you choose to deploy your code to. You can do this by adding individual EC2 instances through their AWS tag or tag groups.

Provide a name for the "Deployment group name" option, and then for "Service role", provide the service role ARN from the role you created in the above step.

For "Deployment type", choose "In-place", and in the "Environmental configuration", select "Amazon EC2 instances".

Now, click on "Create deployment group"

A Deployment group is created.

  • You have to set up a CodeDeploy agent in order to deploy code on EC2

The AWS CodeDeploy agent is a software package that runs on the target instances and enables CodeDeploy to deploy applications to those instances.

If you do not have the agent installed on your instance, your deployment will not work. The agent can be installed either with the AWS CLI or AWS System Manager.

The CodeDeploy agent can be installed on an Ubuntu server from here

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

Add appspec.yaml file to CodeCommit Repository and complete the deployment process.

Create an appspec.yaml file that will instruct CodeDeploy on how to deploy your code and the dependencies file for installing and starting nginx on the server.

Push all the files to code commit using ‘git add’ and ‘git commit’ commands.

Then push to the repository with the "git push" command

Make some changes to the buildspec.yml file so that CodeBuild will build the appspec.yml file and transfer the artifact to the S3 bucket.

All the files are present in the CodeCommit Repository

In Build Projects, go to "Edit" and choose "Artifacts"

In Artifacts, select "artifact type" as Amazon S3 and choose bucket name and for "Artifact packaging" choose Zip and then click on "Update artifacts".

After updating artifacts, click on "Start build"

After the build is successful, navigate to the S3 bucket and copy the S3 URL of your zip file. This will pull the code during CodeDeploy.

Then create Deployment

Deployment is created, but the event is in the pending state

You need to create a role that will give access to the EC2 with all the necessary permissions.

Then, attach the created role to the EC2 instance

After updating the IAM role, restart the CodeDeploy-agent in the EC2 instance.

Deployment is now successful.

You can now go to the public instance ip to view the webpage

Thank you!