CodeCommit, Build, Deploy & Pipeline using containers and ECS
Published: 2023-08-26 14:49:59

# Overview
The CodePipeline project is a project created by Adrian Cantrill of https://learn.cantrill.io/ which is a popular resource for people looking to earn their AWS certifications. In this project I created a CI/CD CodePipeline with CodeCommit to store static web app and Dockerfile. CodeBuild to build the Docker images and store them in ECR. ECS Cluster, TGs , and ALB configured to deploy to ECS Fargate.

#### Stage 1 : Configure Security & Create a CodeCommit Repo
First I connected to CodeCommit through SSH and created a repo in CodeCommit and cloned it to my local drive.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubzfodfuplm422vqh8yx.png)

End result of architecture of this section.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm33gzwxnmwl02ddfizt.png)

#### Stage 2 : Configure CodeBuild to clone the repo, create a container image and store on ECR
I set up ECR to store the output of CodeBuild then I created CodeBuild project and updated role for the CodeBuild project to give it permissions to interact with ECR.

I created buildspec.yml and pushed it to the repo. The buildspec.yml file is what tells codebuild how to build your code, the steps involved, what things the build needs, and any testing and what to do with the output (artifacts).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kz79kzsti53iun2d7cf7.png)

Ran the CodeBuild project.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfe6bsu60phr2h5duol4.png)

Now I should see a Docker image in the ECR registry I created that CodeBuild built.

I created an EC2 instance and installed Docker. Next I downloaded the Docker image to the EC2 instance from ECR that was built from the CodeBuild step.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i8fu9ijum1msfbzge8r.png)

I ran the Docker container in the EC2 instance and then checked the IPv4 of the EC2 to see if the webpage was running successfully.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28ov3sjdgv6pgmlwz3u3.png)

End result of architecture of this section.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwzk822kkxxwwnyidlu7.png)

#### Stage 3 : Configure a CodePipeline with commit and build steps to automate build on commit
In this section I automated the pipeline so when code is committed to CodeCommit it is automatically built in CodeBuild and pushed to ECR. I still haven't automated deployment part though (Running the image).

I created a pipeline in CodePipeline and ran it. Success.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2jv06npzf644aask7j1.png)

I updated the buildspec.yml to get the commit hash from CodeCommit and put it in a variable along with the image tag
* Ran docker push with the latest tag and docker push with the image tag 
* Finally create an imagedefinitions.json which will be used by CodeDeploy later to deploy to ECS

I commit this new yaml file and now in the ECR I can see the latest image tagged as 'latest' along with the commit ID. If I were to push another it would remove latest from the name and just be the commit ID.

In S3 I can see the imagedefinitions.json that got created during the automatic build that I will use to automate the deploy to ECS next. It contains the name and imageUri.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd7w5lub3s3csvijlr2u.png)

End result of architecture of this section.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05q1z0nopn9ypt9mb6ak.png)

#### Stage 4 : Create an ECS Cluster, TG's , ALB and configure the code pipeline for deployment to ECS Fargate
I added deploy stage to ECS and created a ALB to attach to ECS cluster to perform scaling. Next I created an ECS cluster.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71af0dry776kejnve0ta.png)

I tested the webpage is being hosted. Success. Next I added a stage to CodePipeline to deploy the image. I then modify the HTML to see if this change is propagated to the web app.
* In the options when configuring select the BuildArtifact which is the S3 bucket holding the JSON file which contains the commit info and imageUri needed to deploy the built image.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldlxwnhn24i6mwmb8zlk.png)

The final pipeline is whenever I commit something to CodeCommit it's going to run a build and generate a docker image, store that docker image in ECR and deploy the image to the ECS service.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ygivqt9cm9uq7l9885f.png)

End result of architecture of this project.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3das5sdql8nbn80ft50.png)

#### Conclusion
Overall this was some great hands on experience to go along with the theory already I had. It helped to reenforce CI/CD concepts by breaking them down into steps. It also gave some insight into what makes ECS Fargate powerful since I didn't have to do all the EC2 management like I did initially to get the container deployed in EC2.