Create an AWS Code Pipeline with AWS Code Commit, Code Build & Code Deploy with Amazon Fargate (ECS)

Create an AWS Code Pipeline with AWS Code Commit, Code Build & Code Deploy with Amazon Fargate (ECS)

·

6 min read

In this project, I have implemented a fully automated CodePipeline which creates containerized application and deployed it to Amazon FARGATE.

AWS Fargate is an Amazon ECS solution that allows you to run containers without managing servers or clusters. Amazon ECS is a fully managed container orchestration service that helps you easily deploy, manage, and scale containerized applications. It integrates with the rest of the AWS platform to provide a secure and easy-to-use solution for running container workloads in the cloud.

This project includes AWS CodePipeline with AWS CodeCommit, CodeBuild, and CodeDeploy. It is a simple web page application with few pictures. Every time you commit new changes in the CodeCommit repository, it creates a new docker image, pushes it to Amazon ECR and then deploys a new container with the latest image to the ECS-Fargate cluster and we can access a newly updated web page.

Stage1 - Migrate a Git repository to AWS CodeCommit

Below are the steps to migrate an existing Git repository to a CodeCommit repository.

#Create a CodeCommit repository

Use the CodeCommit console to create the CodeCommit repository

Learn how to create and configure an IAM user for accessing AWS CodeCommit.

After it is created, Clone your repository to your local computer. Configure the AWS CLI with a profile by using the configure command. And then run the following command:

git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/ECS-CodePipeline-App

#Clone the repository and push it to the CodeCommit repository

Clone a GitHub repository to your local computer, creating what is called a local repo. You then push the contents of the local repo to the CodeCommit repository you created earlier.

From your local computer, run the git clone command with the --mirror option to clone a bare copy of the remote repository into a new folder named github-repo. This is a bare repo meant only for migration. It is not the local repo for interacting with the migrated repository in CodeCommit.We will delete this repo once we copied its content to CodeCommit.

Change directoty github-repo and run the git push command, specifying the URL and name of the destination CodeCommit repository and the --all option

git clone --mirror https://github.com/sunitabachhav2007/CodePipeline-ECS-App.git github-repo
cd github-repo
git push https://git-codecommit.us-east-1.amazonaws.com/v1/repos/ECS-CodePipeline-App --all

You can delete the github-repo folder and its contents after you have migrated the repository to CodeCommit.

#Check files in CodeCommit

Stage2 - Configure CodeBuild

#Create Repository in Elastic Container Registry

Go to Amazon Elastic Container Registry console and create a repository.

This is the repository that CodeBuild will store the docker image in, created from the CodeCommit repository.

#Setup a CodeBuild project

Now, we will configure a CodeBuild project city-pipeline-codebuild to take what's in the CodeCommit repo, build a docker image & store it within ECR in the above repository.

AWS_DEFAULT_REGION with a value of us-east-1
AWS_ACCOUNT_ID with a value of your <AWS_ACCOUNT_ID>
IMAGE_TAG with a value of latest
IMAGE_REPO_NAME with a value of your <ECR_REPO_NAME>

Make sure IMAGE_REPO_NAME's value should be matching with your ECR repository name.

#Build security and permissions

Our build project will be accessing ECR to store the resultant docker image, and we need to ensure it has the permissions to do that. The build process will use an IAM role created by CodeBuild, so we need to update that role permissions with ALLOWS for ECR.

Goto IAM console.

Goto CodeBuild console and select city-pipeline-codebuild and run Build.

Stage3 - Create a CodePipeline

Whenever a new commit is made to the CodeCommit repository, a new docker image is created and pushed to ECR.

Goto AWS CodePipeline Console and create a new Pipeline with namecity-CodePipeline

Skip Deploy Stage for now. Review and Create Pipeline.

The pipeline gets initiated and succeeded as below.

Stage4 - Create CodeDeploy

Now you will configure the automated deployment of the city-pipeline application to ECS Fargate.

#Configure a load balancer

Go to the EC2 Console -> then Load Balancing -> Load Balancers -> Create Load Balancer.
Create an application load balancer with name city-pipeline-alb

Internet-facing IPv4 For network, select your default VPC and pick ALL subnets in the VPC.

Create a new security group city-pipeline-alb-sg and delete the default VPC in the list Add an inbound rule, select HTTP and for the source IP address choose 0.0.0.0/0 Create the security group.

Return to the original tab, click the refresh icon next to the security group dropdown, and select city-pipeline-alb-sg and remove the default security group.

Under listners and routing make sure HTTP:80 is configured for the listener.

Create a target group, this will open a new tab call it city-pipeline-alb-targetgroup.

Select target type as IP Addresses, HTTP:80, HTTP1 and the default VPC are selected.

Return to the load balance tab, hit the refresh icon next to target group and pick city-pipeline-alb-targetgroup from the list. Skip steps for register targets.
Then create the load balancer.

#Create a ECS-Fargate cluster

Move to the ECS console, Create a Cluster with name city-pipeline-ecs-cluster

#Create Task and Container Definitions

In Container Details, Name put city-pipeline, then in Image URI move back to the ECR console and click Copy URI next to the latest image.

Make sure Container Name city-pipeline should be matched with Amazon ECR repository name city-pipeline.
Scroll to the bottom and click Next

Go to next and configure the environment as mentioned below.

Create ECS-task-role for providing access to AWS Services to run ECS tasks.

Go to next and Create.

#Deploy To ECS - Create a Service

Create Service with name city-pipeline-service and Launch Type as FARGATE and Desired Tasks 2.

For Networking pick the default VPC for Subnets make sure all subnets are selected.

Keep rest other setting as it is and create Service.

The service is now running with the :latest version of the container on ECR.

#Verify the application on ALB.

Move to the load balancer console and copy ALB DNS.

Open it to browser.

Stage5 - Configure Deploy Stage to Code Pipeline

Goto Code pipeline console. Select city-CodePipeline then edit Click + Add Stage

Click + Add Action Group. And select and fill as below. Click Done Click Save & Confirm

In the local repository edit the index.html file.

git add -A .
git commit -m "test pipeline"
git push

Watch the code pipeline console.

Move to the load balancer console and copy ALB DNS. And open it in the browser.

Congratulation! Your application is now running on ECS-Fargate Cluster. We have automated this deployment through AWS Code Pipeline with AWS Code Commit, Code Build & Code Deploy.

Stage6 - Cleanup

In this stage, you're going to clean up and remove all resources which we created during the session. So that it will not be charged to you afterward.

ECS: Remove Service, Task Definition and Cluster

EC2: Remove ALB, Target Groups

Code: Remove CodePipeline, CodeCommit, CodeBuild

S3: Remove Artefact Bucket

ECR: Remove the repository

Thank you

Thanks for reading to the end; I hope you gained some knowledge.