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.
End result of architecture of this section.
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).
Ran the CodeBuild project.
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.
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.
End result of architecture of this section.
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.
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.
End result of architecture of this section.
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.
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.
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.
End result of architecture of this project.
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.
Top comments (0)