AWS CI/CD Pipeline Creation - Step by Step
Tags: aws, cicd, devops, cloud
Published: 2022-05-10 17:37:00

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jglqfbaaets9ly2qsqr2.png)

[Git Repository URL](https://github.com/sanju2/cicd-webapp)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jui44d8ks01pmlx3tdy1.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcoycy8kx6lm2y2xubda.jpg)

Adding index.html, scripts folder, and appspec.yml  for newly created repository and push to AWS CodeCommit.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60asrp1srb5bi2zxbbq9.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cesb0ok2caux4k8zugvs.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8aguse2s9ltkjquy03yr.jpg
)

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
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv2doaks8mhvlk6su3us.jpg)

Use the following tags for both dev and prod instances. Use Environment as **dev** for development and **prod** for production.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouy9occxkfj8948a25ia.jpg)

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

Finally, Your EC2 instances are up and running.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bptekdn9nud02g5oel18.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gm9pr6xhyxxls9iyn5rf.jpg)

Create IAM Role for CodeDeploy. Use AWS managed "AWSCodeDeployRole" policy. Also you can used AWS Managed **AmazonEC2RoleforAWSCodeDeploy** role.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riq0f312ccoim9kcfjbo.jpg)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep8foumom1ln7b95eq21.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dd2y2wpmz7y2ygkcf0n.jpg)

In the load balancer section unchecked enable the load balancing button and hit create deployment group button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1m77e0or4b7vihs3a7l.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4t4gn46oum6q7fu3ouqv.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7zhi82uipeknp04oswn.jpg)

Inside the S3 bucket, you can see the app.zip file.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/van1zfezr6oye9euw04f.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxr9rtoibei1ebqb0mlq.jpg)

Click create deployment button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edrlnl31qc2j087nuaqf.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntj01kzmkb6vhyt9n55l.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwtm9zw28sn2tuorz4ka.jpg)

Next, select the source as AWS CodeCommit and select repository name and branch.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azjq5e8g6mpysszsagus.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4f7l2pgqdt3kus360v72.jpg)

After you can see the pipeline is triggered and you can check using dev instance public IP or Public IPv4 DNS.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klnmwifq65r33qg9ln5v.jpg)

Now we going to add a stage for the production. Go to Codepipeline and click the edit the pipeline button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6antckk59k2maa3bljm.jpg)

After the deploy stage add a new stage for Prod-Approval.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp6vb5hksgaqzs587xvq.jpg)

Next, add an action group for the added stage. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4enzftp87m2r6g3evfc.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d45p34nd04t43m04vumj.jpg)

Finally, add a new stage for Production deployment. After that click add action group.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/322m2dmjdnxuw3snbpvb.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9c9zlmhre0tjjsok1qm.jpg)

Next hit save button. After that click the Release change button to start the pipeline.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obupjar70ew9gpx4jofh.jpg)

The pipeline is triggering and you can see the Prod-Approval stage wants to manually approve.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzcbktx201skp5dibnel.jpg)

Also, can get email related to the SNS topic subscribed.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqigpmrxaeqyr3tgvirf.jpg)

Click the review button and check whether the dev deployment running success. Next give the comment and click approve button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhbhfa2qnzc1t99j6ez.jpg)

You can see the pipeline is triggering successfully.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhnmyg7rq0s3uexr4y71.jpg)

Check production deployment using prod instance Public IPv4 address or Public IPv4 DNS.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cdqpuhi5ktfy1o092ld.jpg)

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

Thanks for reading the Article.