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.
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.
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.
Adding index.html, scripts folder, and appspec.yml for newly created repository and push to AWS CodeCommit.
Step 2 - Create two EC2 instances
Create IAM Role for EC2 access S3. Use AWS Service as EC2 and use this AWS managed policy
Next, go to the EC2 console and hit create instances. Use AMI as Amazon Linux 2 AMI and select the previously created IAM Role.
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
Use the following tags for both dev and prod instances. Use Environment as dev for development and prod for production.
Use Security Group Inbound rule as SSH(port 22) & HTTP(port 80).
Finally, Your EC2 instances are up and running.
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.
Create IAM Role for CodeDeploy. Use AWS managed "AWSCodeDeployRole" policy.
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.
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.
In the load balancer section unchecked enable the load balancing button and hit create deployment group button.
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.
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
Inside the S3 bucket, you can see the app.zip file.
Now go to again CodeDeploy and use revision type as My Application stored in Amazon S3. After that select revision location S3 bucket.
Click create deployment button.
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.
Next, select the source as AWS CodeCommit and select repository name and branch.
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.
After you can see the pipeline is triggered and you can check using dev instance public IP or Public IPv4 DNS.
Now we going to add a stage for the production. Go to Codepipeline and click the edit the pipeline button.
After the deploy stage add a new stage for Prod-Approval.
Next, add an action group for the added stage.
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.
Finally, add a new stage for Production deployment. After that click add action group.
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.
Next hit save button. After that click the Release change button to start the pipeline.
The pipeline is triggering and you can see the Prod-Approval stage wants to manually approve.
Also, can get email related to the SNS topic subscribed.
Click the review button and check whether the dev deployment running success. Next give the comment and click approve button.
You can see the pipeline is triggering successfully.
Check production deployment using prod instance Public IPv4 address or Public IPv4 DNS.
References - https://aws.amazon.com/getting-started/hands-on/set-up-ci-cd-pipeline/
Thanks for reading the Article.
Top comments (1)
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.