Open In App

How to Build a CI/CD Pipeline with AWS?

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

CI/CD stands for Continuous Integration and Continuous Deployment. A CI/CD Pipeline is a series of steps that must be performed in order to deliver a new version of software.

CI stands for Continuous Integration, which means bringing together all the developers working copies to a shared main line. All the developers working on parallel branches of a certain upgrade of an application merge their changes into one main branch.

CD stands for Continuous deployment. It is about the processes that have to happen after the code is integrated for the app to be delivered to the users. These processes involve testing, staging and deploying the code.

AWS CODEPIPELINE

Similarly, AWS CodePipeline is a continuous integration and continuous deployment(CI/CD) AWS service that allows you to automate the release process for your application or service. Every time you commit a code change to your source(GitHub, AWS CodeCommit, etc.), CodePipeline automatically builds, tests and deploys your code based on the release process models you define while initializing your CodePipeline. This enables you to rapidly and reliably deliver features and updates.

  • It can also integrate with AWS services like AWS CodeCommit, Amazon S3, AWS CodeDeploy, Elastic BeanStalk, OpsWorks and Amazon Lambda.
  • You can configure the pipeline either with your Command Line Interface(CLI) or with your Graphical User Interface(GUI).
  • Like most of the services on AWS, even with pipeline, you only have to pay for what you use.
  • Advantages –
    • Automate your process.
    • Choice of Tools
    • Consistent Release
    • Provides History

Lets now build a CI/CD Pipeline with AWS by going through a step-by-step procedure.

Steps to Create AWS Code Pipleline

Step 1: Create IAM Role for EC2 and AWS CodeDeploy. Go to services and search for IAM service. Then go to roles and create a new role. And now for creating a new role, you have to follow 3 steps.

1. Select trusted entity

  • Trusted entity type = AWS Service
  • Service or use case = EC2

Select trusted entity

2. Add permissions

  • Select AmazonS3ReadOnlyAccess. It will allow our EC2 instance to access stored artifacts from the Amazon S3 bucket.
Add Permissions

Add permissions

3. Name, review and create

– Select an appropriate role name and click on create role.

Role details

Name, review and create

Create a new service role for CodeDeploy and attach AWSCodeDeployRole policy which will provide the permissions for our service role to read tags of our EC2 instance, publish information to Amazon SNS topics and much more task.

Again the same above 3 steps have to followed.

Select trusted entity

  • Trusted entity type – AWS Service
  • Use case – CodeDeploy

Select trusted entity

Add permissions

Add Permissions

Name, review and create

Name,Review,Create

Name, review and create

Step 2: Launch an Linux EC2 instance

Select the instance and click on connect to run the below commands in the console.And then Install code deploy agent by following commands on terminal.The command “sudo su” in Linux is used to switch the current user to the superuser or root user. It’s a way to start a new shell session as the superuser, which has elevated administrative privileges and can perform actions that are typically restricted to regular users.

sudo su

The command “sudo yum update” is used in Amazon Linux, CentOS, and Red Hat Linux distributions to update installed packages on your system to their latest available versions.

sudo yum update

The command sudo yum install ruby is used to install the Ruby programming language on a system running Amazon Linux, CentOS, or Red Hat Linux distributions using the YUM package manager.

sudo yum install ruby

The command sudo yum install wget is used to install the “wget” package on a system running Amazon Linux, CentOS, or other Red Hat-based Linux distributions that use the YUM package manager

sudo yum install wget

The command wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install is used to download a script from the specified URL using the wget command. In this case, you are attempting to download the AWS CodeDeploy agent installation script from the AWS S3 bucket.

Downloading the AWS CodeDeploy agent installation script from the AWS S3 bucket is an essential step in setting up AWS CodeDeploy for your infrastructure. The CodeDeploy agent is a lightweight, scalable software component that enables AWS CodeDeploy to deploy and manage applications on your EC2 instances or on-premises servers.

wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install

The command chmod +x ./install is used to make a file executable in a Unix-like operating system, including Linux.

chmod +x ./install

The command sudo ./install auto is likely used to run an installation script with superuser (administrator) privileges and pass the “auto” argument to the script. This command is typically used for automating the installation process, and the specific behavior can vary depending on the installation script.

sudo ./install auto

The command sudo service codedeploy-agent status is used to check the status of the AWS CodeDeploy agent running on your system.

sudo service codedeploy-agent status

After running the following commands, select the instance and click on “Actions”, then click on “Security” and click on “Modify IAM Role”. Then choose the above created IAM Role and click on “Update IAM Role”. After this step, your EC2 instance gets attached with your above created IAM Role.

Instance

Modify IAM Role

Modify IAM Role

Update IAM Role

After this process, go to the console where your instance is connected and run the command “exit” to exit from the root folder and go back to the EC2 folder. Make a directory on the EC2 folder named “server”, this is the directory where my source code will be deployed.

EC2 instance

Then after doing the above process, come back to the running instances list. Select your currently created running instance and go to the “Security” section present at the end of the page. Click on the link present under the “Security Groups”. After redirecting to the required page, click on “Edit Inbound rules” under the section of “Inbound rules” present at the end of the page. Then add a rule, select a port range of your choice and select the source as “Anywhere-IPv4” from the dropdown menu and then click on “Save rules”.

Basically, let me give you a overview what we are actually doing here. In brief, when you add an inbound rule to a security group for an instance with port range (in my case, it was 4000) and set the source to “Anywhere-IPv4,” you are allowing any computer or device on the internet to connect to your instance through port 4000. This is like opening a door (port 4000) on your server and letting anyone from anywhere access the service or application running on that port.

Inbound rules

Step 3: Create a CodePipeline using Github, CodeBuild and CodeDeploy

1. Create CodePipeline

  • Let’s navigate to CodePipeline via AWS Management Console and click on Create pipeline.

Choose pipeline settings

2. Choose Github in Code Source

  • After selecting GitHub as the source provider, click on the Connect to GitHub button. You’ll then be prompt to enter your GitHub login credentials.
  • Once you grant AWS CodePipeline access to your GitHub repository, you can select a repository and branch for CodePipeline to upload commits to this repository to your pipeline.

Add Source Stage

3. Configure CodeBuild (Optional)

  • If you haven’t created a project prior to creating your pipeline, then you can create a project directly from here by clicking Create project button.
  • Note: Buildspec file is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build. For my project, I created a buildspec.yaml file and added it in the root of my project directory.

Add build stage

4. Add Deploy Stage

Note : Before going to configure Add Deploy Stage, Let’s make duplicate tab of current tab.

  • Go to code deploy in the navigation, Select Application, then add create a deployment group.

Create application

Deployment groups

In deployment group Select EC2 instances and select Tag and Value

Create deployment

Environment configuration

Untick Load Balancer Option

Agent configuration

Finally Come on Add Deploy Stage and select that created Application name & Deployment group

Add Deploy stage At the end, just review and create.

OutPut

Source – Build – Deploy

Conclusion

In conclusion, implementing a CI/CD pipeline using AWS CodePipeline can significantly enhance your software development process. It brings automation, efficiency, and reliability to your deployments, allowing you to stay competitive in a rapidly evolving tech landscape. Embrace CI/CD as a mindset for constant improvement and innovation, and AWS CodePipeline as your trusted tool for this journey. Happy coding!

FAQs On CI/CD Pipeline with AWS

1. What are AWS CodeBuild and AWS CodeDeploy, and how do they fit into a CI/CD pipeline?

AWS CodeBuild is for building and testing code. AWS CodeDeploy automates deployments to EC2 instances, Lambda functions, or ECS clusters. They work together to ensure code changes are built and deployed reliably.

2. Can I use third-party tools like Jenkins or GitLab in an AWS-based CI/CD pipeline?

Yes, you can use third-party CI/CD tools within AWS pipelines. AWS services are flexible and can integrate with popular tools like Jenkins and GitLab to extend your pipeline capabilities.

3. What’s the cost of running a CI/CD pipeline on AWS, and how can I optimize it?

Costs vary based on usage. Use the AWS Simple Monthly Calculator to estimate costs. Optimize costs by choosing the right instance types, minimizing idle time, and considering reserved instances.

4. How do I troubleshoot common issues in my CI/CD pipeline on AWS?

Use AWS CloudWatch Logs and CloudWatch Alarms for monitoring and debugging. Check CodePipeline logs and notifications for pipeline issues.

5. Are there any alternatives to AWS CodePipeline for building a CI/CD pipeline on AWS?

Yes, alternatives include Jenkins, GitLab CI/CD, and Travis CI. These tools can be used alongside AWS services for building CI/CD pipelines.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads