DEV Community

Cover image for AWS CI/CD Pipeline Creation - Step by Step
Lasantha Sanjeewa Silva for AWS Community Builders

Posted on • Updated on

AWS CI/CD Pipeline Creation - Step by Step

AWS provides many services for CI/CD. Examples of AWS CodeCommit, AWS CodeBuild, AWS CodeDeploy, and AWS CodePipeline. In this article, I'm going to create CI/CD pipeline for a web application.

Architecture Diagram
Image description

Git Repository URL

Step 1 - Create AWS CodeCommit Repository

First of all, go to the AWS management console and search AWS CodeCommit and create a repository. You can use any name & description. After filling, in the details click Create.
Image description

Next, Clone the repository. You want to create an AWS CodeCommit Username & password. Go to the IAM console and create an IAM user. Next, you can generate username & password. You can select HTTPS Git credentials for AWS CodeCommit and hit generate. Copy Username and Password for later use.
Image description

Adding index.html, scripts folder, and appspec.yml for newly created repository and push to AWS CodeCommit.
Image description

Step 2 - Create two EC2 instances

Create IAM Role for EC2 access S3. Use AWS Service as EC2 and use this AWS managed policy AmazonS3ReadOnlyAccess.
Image description

Next, go to the EC2 console and hit create instances. Use AMI as Amazon Linux 2 AMI and select the previously created IAM Role.
Image description

Add the following script inside User Data Section

#!/bin/bash
sudo yum update -y
sudo yum install -y ruby wget
wget https://aws-codedeploy-eu-west-1.s3.eu-west-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
Enter fullscreen mode Exit fullscreen mode

Image description

Use the following tags for both dev and prod instances. Use Environment as dev for development and prod for production.
Image description

Use Security Group Inbound rule as SSH(port 22) & HTTP(port 80).

Finally, Your EC2 instances are up and running.
Image description

Step 3 - Create AWS CodeDeploy Application

Go to the AWS CodeDeploy service and hit create the application. Select compute platform as EC2/On-premises and hit create application button.
Image description

Create IAM Role for CodeDeploy. Use AWS managed "AWSCodeDeployRole" policy. Also you can used AWS Managed AmazonEC2RoleforAWSCodeDeploy role.
Image description

Next, create a separate deployment group for dev and prod.

Enter deployment group name as dev and select previously created IAM role for a service role.
Image description

In the Environment configuration section select Amazon EC2 Instances and use EC2 instance tag name. Ex- for dev you can use Environment as dev. After that, you can see 1 unique matches instances.
Image description

In the load balancer section unchecked enable the load balancing button and hit create deployment group button.
Image description

Use the same senario for creating a prod deployment group.

Next, we going to create a deployment.

1st of all, We want to create an s3 bucket for artifacts. Please consider Enable bucket versioning.
Image description

Next, Deploy artifacts to s3 using the following command.
aws deploy push --application-name cicd-webapp-deploy --s3-location s3://cicd-webapp-96/codedeploy-demo/app.zip --ignore-hidden-files --region us-east-2 --profile default
Image description

Inside the S3 bucket, you can see the app.zip file.
Image description

Now go to again CodeDeploy and use revision type as My Application stored in Amazon S3. After that select revision location S3 bucket.
Image description

Click create deployment button.
Image description
Image description

Use the same scenario for creating prod deployment. Now Code Deployment is completed, Next, create AWS CodePipeline.

Step 4 - Setup AWS CodePipeline

Go to AWS CodePipeline and click create pipeline button. Give the pipeline Name and Hit the next button.
Image description

Next, select the source as AWS CodeCommit and select repository name and branch.
Image description

Skip the Build stage and go to the deploy stage. After selecting the build provider as CodeBuild and selecting the application name and deployment group.
Image description

After you can see the pipeline is triggered and you can check using dev instance public IP or Public IPv4 DNS.
Image description

Now we going to add a stage for the production. Go to Codepipeline and click the edit the pipeline button.
Image description

After the deploy stage add a new stage for Prod-Approval.
Image description

Next, add an action group for the added stage.
Image description

Give the action name and add the action provider as Manual approval. and create an SNS topic and select it for notifications. add the URL for the preview section Dev Public IPv4 DNS. Give the Comments and click the done button.
Image description

Finally, add a new stage for Production deployment. After that click add action group.
Image description

Give the action group name and select the action provider as AWS CodeDeploy. select input artifacts as SourceArtifacts and select CodeDeploy application name and Deployment group.
Image description

Next hit save button. After that click the Release change button to start the pipeline.
Image description

The pipeline is triggering and you can see the Prod-Approval stage wants to manually approve.
Image description

Also, can get email related to the SNS topic subscribed.
Image description

Click the review button and check whether the dev deployment running success. Next give the comment and click approve button.
Image description

You can see the pipeline is triggering successfully.
Image description

Check production deployment using prod instance Public IPv4 address or Public IPv4 DNS.
Image description

References - https://aws.amazon.com/getting-started/hands-on/set-up-ci-cd-pipeline/

Thanks for reading the Article.

Top comments (2)

Collapse
 
rinkunayagparaitp profile image
rinku-nayagpara-itp

Hey there. I would like to suggest some modification. The EC2 will need another policy permission apart from the S3read to do the operation. It will need AmazonEC2RoleforAWSCodeDeploy policy too. Kindly update that.

Collapse
 
lasanthasilva profile image
Lasantha Sanjeewa Silva

Sorry for late reply!. Thanks for your information. I will update it.