How to Set Up AWS CodeDeploy on Linux
Introduction
Deploying applications to multiple servers manually using FTP or SFTP can be time-consuming and difficult to manage. AWS CodeDeploy helps automate application deployments to Amazon EC2 instances and other supported environments.
In this guide, we will set up AWS CodeDeploy on an Ubuntu/Linux server and deploy application code from GitHub.
AWS CodeDeploy supports different deployment sources, including:
- Amazon S3
- GitHub
This article covers deployment using GitHub.
Prerequisites
Before you begin, ensure you have:
- An AWS account
- An Ubuntu/Linux EC2 instance
- SSH access to the instance
- A GitHub repository containing the application
- Basic knowledge of AWS IAM and EC2
Implementation
Step 1: Create IAM Roles
CodeDeploy requires IAM roles to allow communication between the CodeDeploy service and the EC2 instances.
Create the following two IAM roles:
CodeDeployServiceRoleCodeDeployInstanceRole
CodeDeployServiceRole
Create a role from:
AWS Console → IAM → Roles → Create Role
Attach the required CodeDeploy service policy to the role.
CodeDeployInstanceRole
Create another role for the EC2 instance and attach the required CodeDeploy and Auto Scaling permissions.
Step 2: Attach the IAM Role to the EC2 Instance
When launching the EC2 instance, select:
CodeDeployInstanceRole
as the IAM Role.
If you already have an EC2 instance, the IAM role can also be attached to the instance afterward.
Step 3: Install CodeDeploy Agent
In this example, we are using Ubuntu.
Install the required packages:
apt-get -y install python-pip apt-get -y install ruby apt-get -y install wget
Download the CodeDeploy agent:
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 agent:
service codedeploy-agent start
Verify that the agent is running:
service codedeploy-agent status
Step 4: Create appspec.yml
CodeDeploy requires an appspec.yml file to define how the application should be deployed.
Create appspec.yml in the root of your GitHub repository.
Example:
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html
permissions:
- object: /var/www/html
owner: ubuntu
group: ubuntu
mode: 755
type:
- directory
- object: /var/www/html
owner: ubuntu
group: ubuntu
type:
- file
hooks:
ApplicationStart:
- location: scripts/restart_nginx.sh
timeout: 30
runas: root
The appspec.yml file defines:
- Where the application files should be copied.
- File and directory permissions.
- Deployment lifecycle hooks.
- Scripts that should run during deployment.
Step 5: Create Deployment Scripts
Create a directory named:
scripts
Inside the directory, create:
restart_nginx.sh
Add the following:
#!/bin/bash service nginx restart
Make the script executable:
chmod +x scripts/restart_nginx.sh
Commit and push the files to your GitHub repository.
Step 6: Create a CodeDeploy Application
Log in to the AWS Management Console and navigate to:
Services → CodeDeploy
Select Create Application.
Enter the application details and create the application.
Next, create a deployment group with settings similar to:
Application name: <Your Application Name> Deployment group: <Deployment Group Name> Deployment type: In-place deployment
Under Environment configuration, select:
Amazon EC2 instances
You can identify the target EC2 instance using its tags.
For example:
Key: Name Value: <Your Instance Name>
Select the required deployment configuration, such as:
CodeDeployDefault.OneAtATime
Finally, select the previously created:
CodeDeployServiceRole
as the service role.
Step 7: Deploy the Application
Once the application and deployment group are configured:
- Go to AWS CodeDeploy → Applications.
- Select your application.
- Select Deploy New Revision.
- Select GitHub as the revision source.
- Enter your GitHub repository details.
- Enter the required commit ID.
- Click Deploy.
CodeDeploy will begin deploying the selected revision to the EC2 instance.
You can monitor the deployment from the Deployment ID page and check each deployment lifecycle step.
Conclusion
AWS CodeDeploy provides an automated way to deploy applications to EC2 instances without manually copying files using FTP or SFTP. By configuring IAM roles, installing the CodeDeploy agent, adding an appspec.yml file, and connecting the application to GitHub, deployments can be managed directly through AWS.
This setup can also be extended with deployment hooks for tasks such as restarting services, updating configurations, running migrations, and performing application health checks.
Related Article
How to Create an AWS EC2 Instance Using Terraform in Ubuntu
