Setting Up CI/CD with CodeDeploy and CodePipeline on Amazon Linux 2

Trushang SutharTrushang Suthar
3 min read

In this guide, we’ll walk through configuring a CI/CD pipeline on Amazon Linux 2 using AWS CodeDeploy and CodePipeline. Follow these steps to deploy your application seamlessly and automate updates.

Step 1: Install AWS CodeDeploy Agent on Your EC2 Instance

Connect to your EC2 instance and install the necessary dependencies and CodeDeploy agent.

# SSH into your EC2 instance
ssh ec2-user@<your-instance-public-ip>
cd ~
sudo yum update
sudo yum install -y wget ruby

# Download and install the CodeDeploy agent
cd /tmp
wget https://aws-codedeploy-ap-south-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto

# Start and check the status of the CodeDeploy agent
sudo service codedeploy-agent start
sudo service codedeploy-agent status

# View the CodeDeploy agent log for troubleshooting
tail -f /var/log/aws/codedeploy-agent/codedeploy-agent.log

Step 2: Create a CodeCommit Repository

Set up a CodeCommit repository in the same AWS region as your EC2 instance to store your code. Then, clone the repository locally using the following command:

git clone ssh://your-repository-url

Step 3: Create Configuration Files for Deployment

Add the appspec.yml file to your repository. This file defines deployment instructions for CodeDeploy.

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/
    overwrite: true
    file_exists_behavior: OVERWRITE
permissions:
  - object: /var/www/html
    pattern: "**"
    owner: ec2-user
    group: apache
    mode: 755
    type:
      - file
hooks:
  AfterInstall:
    - location: scripts/env_deploy.sh
      runas: root
    - location: scripts/laravel_deploy.sh
      runas: root

Step 4: Create an IAM Role for EC2 Instances (AmazonEC2RoleforAWSCodeDeploy)

  1. Open the IAM console.

  2. Go to Roles and select Create role.

  3. Under Trusted entity type, choose AWS service and select EC2.

  4. Search for the AmazonEC2RoleforAWSCodeDeploy policy, then select it.

  5. Name the role (e.g., DevEC2RoleforAWSCodeDeploy) and create it.

Step 5: Attach IAM Role to Your EC2 Instance

Attach the created IAM role to your EC2 instance:

  1. Go to the EC2 Console and select your instance.

  2. Choose Actions > Security > Modify IAM role.

  3. Select your IAM role (AmazonEC2RoleforAWSCodeDeploy) and attach it.

Refer to AWS documentation for detailed steps.

Step 6: Tag Your EC2 Instance

Tag your EC2 instance to identify it for CodeDeploy. These tags are used in CodeDeploy to target the instance for deployments.

Step 7: Set Up an Application in CodeDeploy

  1. Create a CodeDeploy service role:

    • Open the IAM console.

    • Go to Roles and create a new role with the CodeDeploy service.

    • Name it CodeDeployRole and create it.

  2. Create an Application in CodeDeploy:

    • Open the CodeDeploy console.

    • Choose Create application, enter an application name, and select EC2/On-premises as the compute platform.

  3. Set up a Deployment Group in CodeDeploy:

    • Choose your application and select Create deployment group.

    • Enter a name for the deployment group, select the service role (CodeDeployRole), and choose In-place deployment.

    • Under Environment configuration, select Amazon EC2 Instances and specify the tag key.

    • Choose CodeDeployDefault.OneAtATime as the deployment configuration, and complete the setup.

Step 8: Create a Pipeline in CodePipeline

Create your pipeline to automate the deployment process upon changes to your CodeCommit repository:

  1. Open the CodePipeline console.

  2. Choose Create pipeline and follow the setup to select your CodeCommit repository as the source.

  3. Set CodeDeploy as the deployment provider and select your application and deployment group created in CodeDeploy.

Helpful Resources


By following these steps, you can set up a robust CI/CD pipeline on AWS, ensuring streamlined deployments to your Amazon Linux 2 instances. Happy coding!

0
Subscribe to my newsletter

Read articles from Trushang Suthar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Trushang Suthar
Trushang Suthar