How to Set Up AWS CodeDeploy on Linux for GitHub Deployments

Introduction

Deploying application code across multiple servers can be challenging when deployments are performed manually. Traditionally, files may be uploaded to servers using FTP or SFTP, followed by manually restarting the application.

AWS CodeDeploy provides a way to automate application deployments to AWS instances. It can be used to deploy application code to multiple servers while providing a structured deployment process.

AWS CodeDeploy supports different revision sources, including:

  1. Amazon S3
  2. GitHub

In this article, we will explain how to configure AWS CodeDeploy on Linux and deploy application code from GitHub.

Prerequisites

Before starting, make sure you have:

  • An AWS account.
  • An AWS EC2 instance.
  • SSH access to the Linux instance.
  • An IAM role with the required permissions.
  • A GitHub repository containing the application code.
  • Basic knowledge of AWS EC2, IAM, GitHub, and Linux commands.

Implementation

Step 1: Create IAM Roles

The first step is to create the IAM roles required for communication between the EC2 instance and AWS CodeDeploy.

Create the following two IAM roles:

  1. CodeDeployInstanceRole
  2. CodeDeployServiceRole

Create the CodeDeploy Service Role

From the AWS Management Console, navigate to:

IAM → Create New Role

Create a role named:

CodeDeployServiceRole

Attach the following policy:

AWSCodeDeployRole

Create the CodeDeploy Instance Role

Create another IAM role named:

CodeDeployInstanceRole

Attach the following policies:

AmazonEC2RolePolicyForAWSCodeDeploy
AutoScalingNotificationAccessRole

These roles are used by the CodeDeploy service and EC2 instance during the deployment process.

Step 2: Launch an EC2 Instance with the IAM Role

The CodeDeployInstanceRole created in the previous step needs to be associated with the EC2 instance.

While launching the EC2 instance, select:

IAM role: CodeDeployInstanceRole

Complete the instance launch process.

After the instance is launched, install the required application stack based on your application requirements.

For example:

  • Apache, PHP
  • Nginx, PHP, MariaDB

Step 3: Install CodeDeploy Agent Dependencies

In this example, Ubuntu is used as the Linux host.

Install the required dependencies:

apt-get -y install python-pip
apt-get -y install ruby
apt-get -y install wget

Step 4: Install the CodeDeploy Agent

Download the CodeDeploy agent installation script:

wget https://aws-codedeploy-eu-central-1.s3.amazonaws.com/latest/install

Make the installer executable:

chmod +x ./install

Install the CodeDeploy agent:

./install auto

Start the CodeDeploy agent:

service codedeploy-agent start

The CodeDeploy agent is now installed on the EC2 instance.

Step 5: Create the Deployment Configuration

AWS CodeDeploy requires an appspec.yml file to define how the application should be deployed.

Create an appspec.yml file in the root of your GitHub repository.

The original example uses the following configuration:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
    except:
    owner: ubuntu
    group: ubuntu
permissions:
  - object: /var/www/html
    owner: ubuntu
    group: ubuntu
    mode: 755
    type:
      - directory
  - object: /var/www/html
    owner: ubuntu
    group: ubuntu
    type:
      - file

hooks:
  BeforeInstall:
    - location: scripts/install_codedeployment
      timeout: 40
      runas: root
  ApplicationStart:
    - location: scripts/restart_nginx.sh
      timeout: 30
runas: root

The appspec.yml file defines the application files, destination directory, permissions, and deployment lifecycle hooks.

Step 6: Add Deployment Scripts

Create a directory named:

scripts

inside the GitHub repository.

Add the required deployment scripts to this directory.

For example, create:

restart_nginx.sh

with the following content:

#!/bin/bash
service nginx restart

Make sure the script is included in the GitHub repository along with the application source code.

Step 7: Create a CodeDeploy Application

Log in to the AWS Management Console.

Navigate to:

Services → CodeDeploy

Select:

Create Application

Enter the application details.

Application Name

Enter your application name:

<Your Application Name>

Deployment Group

Enter the deployment group name:

<Deployment Group Name>

Deployment Type

Select:

In-Place Deployment

Add Instances

Under the instance selection options, choose:

Search by Tags

AWS CodeDeploy provides options for selecting deployment targets, including:

  1. Amazon EC2
  2. Auto Scaling Group
  3. On-Premises Instance

For this example, we will use an Amazon EC2 instance.

Select the EC2 instance using its Name tag.

Deployment Configuration

Select:

CodeDeployDefault.OneAtATime

Service Role

Select the CodeDeploy service role created earlier:

CodeDeployServiceRole

Complete the deployment group configuration.

Step 8: Deploy the Application from GitHub

Once the CodeDeploy application and deployment group are configured, you can deploy the application.

Navigate to:

AWS CodeDeploy → Applications

Select your application.

Choose:

Actions → Deploy New Version

For the revision source, select:

My application is stored in GitHub

Enter the following details:

Repository Name: <Your Git Repository>
Commit ID: <Git Commit ID>

Click:

Deploy

AWS CodeDeploy will retrieve the application revision from GitHub and deploy it to the selected EC2 instance.

Step 9: Monitor the Deployment

After starting the deployment, open the deployment details and check the Deployment ID.

The deployment status allows you to monitor whether the deployment is progressing successfully or whether any lifecycle hook or application step has failed.

Conclusion

AWS CodeDeploy can simplify application deployments by automating the process of transferring application code and executing deployment steps on AWS instances.

In this example, we configured the required IAM roles, launched an EC2 instance with the CodeDeploy instance role, installed the CodeDeploy agent, created an appspec.yml configuration, and configured AWS CodeDeploy to retrieve application code from GitHub.

This provides a basic workflow for deploying application code to an AWS EC2 instance using CodeDeploy and GitHub.

Frequently Asked Questions

1. What is AWS CodeDeploy?

AWS CodeDeploy is an AWS deployment service that automates application deployments to supported compute environments such as EC2 instances.

2. Why is appspec.yml required?

The appspec.yml file defines deployment instructions, including where application files should be placed and which scripts should run during different stages of the deployment.

3. What is the purpose of the CodeDeploy agent?

The CodeDeploy agent runs on the deployment target and handles the deployment instructions provided by AWS CodeDeploy.

AWS Infrastructure Architecture for Startups

How to Troubleshoot SSH Connection Issues in AWS EC2

How to Enable Multi-Factor Authentication (MFA) for an AWS IAM User

Talk to Our Technology Experts

Planning your AWS infrastructure or looking to improve an existing cloud environment? Our team can help with cloud architecture, infrastructure, DevOps, security, deployment, and ongoing optimization.

Connect with our technology experts.

admin

Writes about Cloud & AWS at Pheonix Solutions.

Leave a Reply

Scroll to Top