Introduction:
Welcome back to our DevOps journey on AWS! In the first part of this series, we delved into CodeCommit and CodeBuild. In this second installment, we’ll continue our exploration by focusing on CodeBuild and CodeDeploy, crucial components for automating software deployment on AWS.
CodeBuild (Continued):
In the previous blog, we set up a build project in CodeBuild. Now, let’s explore some advanced features and best practices:
1. Build Environment:
- CodeBuild provides various pre-configured build environments including Ubuntu, Amazon Linux, and Windows. Choose the environment that best suits your project requirements.
- You can also create custom build environments using Docker images.
2. Build Caching:
- Improve build performance by enabling build caching. CodeBuild caches dependencies and intermediate build artifacts, reducing build times for subsequent runs.
Creating Ec2 Instance & Set-Up Agent:
AWS CodeDeploy automates software deployments to a variety of compute services including Amazon EC2 instances, AWS Lambda functions, and ECS containers. And here we will deploy our build on EC2 instance, so before proceeding further with code deploy we have to create one ec2 instance.
Creating Instance
- Go to EC2 dashboard > instances > launch instance.
2. Choose image Ubuntu 22.04
3. Choose instance type t2.micro
, it is enough for this demo.
4. Allow all http and https traffic in firewall.
Setting-Up CodeDeploy Agent in EC2
The AWS CodeDeploy agent is a software package that, when installed and configured on an instance, makes it possible for that instance to be used in CodeDeploy deployments.
- Connect to your instance.
- Now create a script on the instance,
agent-install.sh
and copy below content in that file.
#!/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-west-2.s3.us-west-2.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 status
- If your region is different than
oregon
then replace us-west-2 in the above script with your region code. - Now run the script with following command.
bash agent-install.sh
- This will install agent inside the instance.
CodeDeploy:
CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.
Here’s how to use CodeDeploy with EC2 instances:
1. Prepare your application:
- Ensure your application is packaged and ready for deployment. This typically involves creating a deployment artifact such as a .zip file containing your application code and dependencies.
- After creating the Application, now create the deployment group.
2. Deployment Group:
In an EC2/On-Premises deployment, a deployment group is a set of individual instances targeted for a deployment. A deployment group contains individually tagged instances, Amazon EC2 instances in Amazon EC2 Auto Scaling groups, or both.
- Navigate to the CodeDeploy service in the AWS Management Console.
- Click on “Create deployment group” and specify details such as deployment configuration, EC2 instances, and deployment type.
3. Service Role
- Now to enter this service role we have to create it first.
- Go to IAM > Roles > Create Role.
- Add the following permission to this Role.
- Give a name and create it.
- After creating a role, return back to your codeDeploy configuration and add this role on service role section.
- Now we have to enter our EC2-instance in `Environment configuration`
- And at final,
save changes
4. Create Deployment
- For creating deployment, go to Application > deployment group > create deployment.
- Paste the arn of your S3 artifact where your build files are stored by code build, in below column.
- And create deployment.
EC2-ROLE
Now after creating deployment, you also have to create one more role for ec2-instance so that it can easily contact to codeDeploy and amazon S3.
- Create Role. Go to IAM > Roles > Create role.
- Add below permissions to this role.
- And after this give a name and create it.
Attach to EC2 instance
After creating this role attach it to your ec2-instance.
Add the service role here to give required permissions to ec2-instance.
After this, restart the code deploy agent service in your instance.
sudo service codedeploy-agent restart
Adding App Specification File
The application specification file (AppSpec file) is a YAML -formatted or JSON-formatted file used by CodeDeploy to manage a deployment.
Create Required Files:
Here we will add the appspec.yml
file with other required files to our code commit repository.
And with this, we will also add some scripts in script folder that will perform the requires task on instance.
Add And Commit All The Changes:
git add .
git commit -m "<message>"
git push
Start The Build:
- Now your artifact is uploaded to the targeted bucket and path.
Deploy your application:
- Start your deployment.
- CodeDeploy will automatically deploy your application to the specified EC2 instances, ensuring minimal downtime and rollback capabilities.
RESULT AND VERIFICATION
- Copy the IP of your ec2 instance and paste on your browser, you will see your
index.html
file content.
Best Practices:
- Use blue-green deployments to minimize downtime and risk during deployments.
- Leverage deployment hooks to run custom scripts before and after deployment.
- Monitor deployment health using CloudWatch metrics and alarms.
Conclusion:
In this second part of our series, we explored advanced features of CodeBuild and learned how to automate software deployments using CodeDeploy. By mastering these services, you can build a robust CI/CD pipeline on AWS. Stay tuned for the final part where we’ll bring everything together with CodePipeline to create a complete end-to-end CI/CD workflow. Happy deploying!
[ LINK TO PART 1: https://harsh05.medium.com/devops-on-aws-part-1-exploring-codecommit-and-codebuild-cfce4a6f2f73 ]
[ LINK TO PART 3: https://harsh05.medium.com/devops-on-aws-part-3-creating-a-complete-ci-cd-pipeline-with-codepipeline-1c594d0f3add ]