Welcome to an exciting journey into the world of AWS DevOps! This comprehensive guide will walk you through the steps to set up a robust CI/CD pipeline for deploying a Todo List web app. By leveraging AWS CodeCommit, CodeBuild, CodeDeploy, and CodePipeline, you'll not only learn the basics but also witness the deployment of your own Nginx-powered web application.
Before diving into the tutorial, make sure you have the following:
- AWS account with the necessary permissions.
- Git installed on your local machine.
Navigate to the AWS Management Console, select CodeCommit, and click "Create Repository." Provide a unique name for your repository.
Create an IAM user with administrative permissions and generate HTTPS Git credentials for the IAM user.
Clone the repository to your local machine using the HTTPS URL and the credentials generated.
Inside the local repository, create the following files: index.html, app.js, styles.css. Push the changes to the CodeCommit repository.
# Example Git commands
git add .
git commit -m "Initial commit"
git push origin mainOpen the AWS CodeBuild console and create a new build project. Use the provided buildspec.yaml file as the build specification.
# buildspec.yaml
version: 0.2
phases:
install:
commands:
- echo Installing Nginx...
- sudo apt-get update
- sudo apt-get install nginx -y
build:
commands:
- echo Build started on `date`
- cp index.html /var/www/html/
- cp styles.css /var/www/html/
- cp app.js /var/www/html/
- cp appspec.yml /var/www/html/
post_build:
commands:
- echo Restarting Nginx...
artifacts:
files:
- index.html
- appspec.yml
- scripts/**
- app.js
- styles.cssEdit the build project to specify an S3 bucket and a folder for storing artifacts. Create a folder in the S3 bucket named code-output-demo-app.
Trigger a build in the CodeBuild console. Verify that the build is successful, and artifacts are stored in the S3 bucket.
Navigate to the AWS CodeDeploy console and create a new application named demo-app-application. Specify the compute platform as ec2/on-premises.
Create a deployment group and associate it with an EC2 instance.
SSH into the EC2 instance. Execute the provided shell script to install the CodeDeploy agent.
#!/bin/bash
# This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.
sudo apt-get update
sudo apt-get install ruby-full ruby-webrick wget -y
cd /tmp
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb
mkdir codedeploy-agent_1.3.2-1902_ubuntu22
dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb
systemctl list-units --type=service | grep codedeploy
sudo service codedeploy-agent statusCreate an appspec.yaml file with the provided content. Create a scripts folder with install_nginx.sh and start_nginx.sh scripts.
# appspec.yaml (Continued)
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/
hooks:
AfterInstall:
- location: scripts/install_nginx.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_nginx.sh
timeout: 300
runas: root# scripts/install_nginx.sh
#!/bin/bash
apt-get update
apt-get install nginx -y# scripts/start_nginx.sh
#!/bin/bash
service nginx startTrigger another CodeBuild to ensure the latest code artifact is in the S3 bucket. Create a deployment in CodeDeploy, specifying the S3 location of the artifact.
Create a role for the EC2 instance with permissions to access S3 and CodeDeploy. Attach the role to the EC2 instance.
On the EC2 instance, restart the CodeDeploy agent.
Open the AWS CodePipeline console and create a new pipeline. Add CodeCommit as the source, CodeBuild as the build stage, and CodeDeploy as the deploy stage.
Click "Create Pipeline" to initiate the pipeline. The pipeline will automatically fetch code from CodeCommit, build using CodeBuild, and deploy using CodeDeploy.
Access the public IP of the EC2 instance. Verify that the Todo List web app is successfully deployed.
Congratulations! You've successfully set up a CI/CD pipeline for deploying a Todo List web app using AWS DevOps tools. This tutorial has covered each step in detail, providing you with a solid foundation for implementing similar projects in the future.
For a detailed walkthrough and explanation of each step, visit the accompanying blog post: [https://arjunmenon.hashnode.dev/cicd-pipeline-using-aws-codecommit-codebuild-codedeploy-and-codepipeline]