Voiced by Amazon Polly |
Overview
Implementing a CI/CD (Continuous Integration/Continuous Deployment) pipeline in AWS for business organizations involves leveraging AWS services and best practices to automate and optimize software development and deployment processes.
Automated testing reduces bugs, instills confidence, and ensures code stability. Continuous deployment enables frequent, reliable releases while monitoring tools offer real-time insights for proactive issue detection and performance optimization.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
The CI/CD pipeline for Node.js applications using AWS CodeDeploy and AWS CodeBuild involves hosting the source code in a version control system, configuring an AWS CodeBuild project to build the application and create deployment artifacts, setting up AWS IAM roles and permissions, creating a AWS CodeDeploy application and deployment group with appropriate configurations, integrating AWS CodeBuild with AWS CodeDeploy to trigger deployments automatically, and establishing a trigger mechanism for changes in the source code. When code changes occur, AWS CodeBuild builds the app, tests it, and uploads artifacts to an Amazon S3 bucket, triggering AWS CodeDeploy to deploy the latest version to the target environment. AWS CodeDeploy allows for rollback conditions in case of deployment failures. This section has three sections setting up the AWS CodePipeline, AWS CodeBuild, and AWS CodeCommit. The process goes like this: first, we need to update the source code section, then we need to update the AWS CodeBuild section, and then we need to update the code deploy section. Combining all these three sections, we call it the AWS CodePipeline.
Pre-Requisites
Configuring AWS CodePipeline:
- Setup infrastructure in AWS
- Create and attach AWS IAM role for Amazon EC2 instance. The role should be as “AmazonEC2RoleforAWSCodeDeploy”
- Create a role for AWS CodeDeploy. The role should be as “AmazonEC2RoleforAWSCodeDeploy”
- After creating the role, attach the role to the ec2 instance.
- Install AWS CodeDeploy-Agent on Amazon EC2 and start and enable the service. The role should be as “AmazonEC2RoleforAWSCodeDeploy”
Requirements:
- Need bitbucket access or GitHub
- env file.
Step-by-Step Guide
Step 1 – AWS CodePipeline
Click on Create AWS CodePipeline.
Step 2 – AWS CodeCommit
Select the source from which we need to commit. For example, I am getting my code from GitHub. Select GitHub and click on Next.
Add the connection, select the repository and branch names, and click next.
Step 3 – AWS CodeBuild
- In the next step, we should select the AWS CodeBuild
- If we create it through the AWS CodeBuild directly, we can see the source section as extra because there should be a connection to Bitbucket or whatever service we use.
- The AWS CodeBuild section has an option to create a project. Using that, we should create a project.
- Click on Create Project.
- Enter the Project name, Environment image as managed image, and the Ubuntu operating system.
- In the build spec update the version
- Add this to the build spec.
Open build projects and go to the build details section, edit the environment section, and update the env in the Name and Value.
Now update the Buildspec. Click on the edit section, and click on insert build with commands. Then click on the editor section and update the below file.
Scripts for Buildspec file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
version: 0.2 phases: install: runtime-versions: nodejs: 14 commands: - echo Installing pre_build: commands: - echo Installing source NPM dependencies. - npm install build: commands: - printenv > .env - cat .env - touch .env.example post_build: commands: - echo Build completed on `date` artifacts: files: - '**/*' |
- In build section we have to update the Appspec.yaml files in the repository
- Go to the bitbucket repo and we need to update the Appspec.yml
- In Appspec.yml we have to update this
Scripts for Appspec file:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
version: 0.0 os: linux files: - source: / destination: /home/ubuntu/(directory name) hooks: BeforeInstall: - location: scripts/before_script.sh runas: root AfterInstall: - location: scripts/after_script.sh runas: root |
Scripts for After_script file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/bash set -x chown -R ubuntu:ubuntu /home/ubuntu/(directory-name)/ echo "***Installing npm package ***" >> /tmp/(directory-name)-api-prod_deploy_logs echo >> /tmp/(directory-name)-api-prod_deploys_logs #runuser -l ubuntu -c 'cd /home/ubuntu/(directory-name) && npm install' #runuser -l ubuntu -c 'cd /home/ubuntu/(directory-name) && npm install --unsafe-perm' #runuser -l ubuntu -c 'cd /home/ubuntu/(directory-name) && npm run build' sleep 10 echo "***starting (pm2 process name)-backend-admin-api-prod application ***" >> /tmp/(directory-name)-api-prod_deploy_logs runuser -l ubuntu -c 'cd /home/ubuntu/(directory-name) && sudo pm2 start dist/src/server.js --name (pm2 process name) --silent' >> /tmp/(directory-name)-api-prod_deploy_logs s1=`pm2 status | grep -we (pm2 process name) | awk '{print $12}'` sleep 5 s2=`pm2 status | grep -we (pm2 process name) | awk '{print $12}'` if [ $s1 == $s2 ] then echo "BUILD SUCCESSFUL" >> /tmp/(directory-name)-api-prod_deploy_logs echo >> /tmp/(directory-name)-api-prod_deploy_logs else echo "Node process is restarting" >> /tmp/(directory-name)-api-prod_deploy_logs echo >> /tmp/(directory-name)-api-prod_deploy_logs fi |
Scripts for Before_script file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/bin/bash set -x echo "** (directory name)-api-prod process status **" >> /tmp/(directory name)-api-prod_deploy_logs runuser -l ubuntu -c 'sudo pm2 status' | grep -wo (pm2 process name) if [ $? -ne 0 ]; then echo "############################## pm2 not running #################################" >> /tmp/(directory name)-api-prod_deploy_logs else echo "############################## pm2 already running Deleting ####################" >> /tmp/(directory name)-api-prod_deploy_logs runuser -l ubuntu -c 'sudo pm2 delete (pm2 process name)' fi rm -rf /home/ubuntu/(directory name) if [ ! -d /home/ubuntu/(directory name) ]; then runuser -l ubuntu -c 'mkdir -p /home/ubuntu/(directory name)' >> /tmp/(directory name)-prod_deploy_logs fi |
- In the After_install script, we must give the process name to run.
Step 4 – AWS CodeDeploy
Go to the deploy section and create an application
In the AWS CodeDeploy section, create a deploy section and select Amazon EC2/On-Premises
In creating a deployment group, create an AWS IAM user of AWS CodeDeploy and attach the ARN to the deployment group.
Attach the service role of code-deploy in the deployment group section.
In the env configuration, select Amazon EC2 and attach the name of the instance.
In deployment settings, select “AWSCodeDeployDefault” one at a time.
In the Appspec.yml, which was created in the Bitbucket, add After_script.sh and Before_script.sh scripts.
Check the overview of the AWS CodePipeline and click on Create Pipeline.
Click on Create a Pipeline.
Conclusion
The integration of AWS CodeDeploy and AWS CodeBuild offers a robust and scalable solution for deploying Node.js applications easily and efficiently. Development teams can significantly reduce manual errors, accelerate time-to-market, and improve overall software quality by automating the build, testing, and deployment processes.
Drop a query if you have any questions regarding AWS CodeDeploy and AWS CodeBuild and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, AWS EKS Service Delivery Partner, and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. How does the CI/CD pipeline benefit development teams?
ANS: – The CI/CD pipeline offers several benefits to development teams, including faster and more frequent releases, reduced manual errors, improved code quality through automated testing, increased collaboration and visibility, and simplified deployment processes. It enables development teams to respond quickly to customer feedback, deliver new features rapidly, and maintain a stable and reliable software delivery process.
2. What is the role of AWS IAM in the CI/CD pipeline with AWS services?
ANS: – AWS IAM (Identity and Access Management) in the CI/CD pipeline is crucial for managing access to AWS resources and services. It ensures that only authorized users or services have the necessary permissions to perform actions on AWS resources, protecting the pipeline from unauthorized access or security breaches. AWS IAM roles and policies must be properly configured for AWS CodeDeploy and AWS CodeBuild to securely interact with other AWS services.
3. Is the CI/CD pipeline suitable for all applications and programming languages?
ANS: – Yes, the CI/CD pipeline concept applies to various applications and programming languages. AWS CodeDeploy and AWS CodeBuild support various programming languages and platforms, making them versatile and suitable for different development environments. Whether it’s web applications, microservices, mobile apps, or backend services, the CI/CD pipeline can be adapted to suit the specific requirements of the application.
WRITTEN BY Yamini Reddy
Click to Comment