Setting Up CI/CD with CodeDeploy and CodePipeline on Amazon Linux 2
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)
Open the IAM console.
Go to Roles and select Create role.
Under Trusted entity type, choose AWS service and select EC2.
Search for the
AmazonEC2RoleforAWSCodeDeploy
policy, then select it.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:
Go to the EC2 Console and select your instance.
Choose Actions > Security > Modify IAM role.
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
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.
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.
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:
Open the CodePipeline console.
Choose Create pipeline and follow the setup to select your CodeCommit repository as the source.
Set CodeDeploy as the deployment provider and select your application and deployment group created in CodeDeploy.
Helpful Resources
Install CodeDeploy Agent: CodeDeploy Installation Guide
Troubleshoot EC2 Instances: CodeDeploy EC2 Troubleshooting
CodeDeploy Resource Kit: CodeDeploy Resources
Error Codes for AWS CodeDeploy: AWS CodeDeploy Error Codes
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!
Subscribe to my newsletter
Read articles from Trushang Suthar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by