Setting up CI/CD in AWS with CodeCommit, CodeDeploy, CodePipeline, ECR, and ECS
Tags: aws, cicd, codepipeline, codecommit
Published: 2024-02-24 10:33:17

In the fast-paced world of software development, Continuous Integration (CI) and Continuous Deployment (CD) have become essential practices for delivering high-quality software at a rapid pace. In this blog post, we'll explore how to set up a CI/CD pipeline in AWS using services like CodeCommit, CodeDeploy, CodePipeline, ECR, and ECS.

## Introduction

Continuous Integration (CI) involves automatically integrating code changes from multiple contributors into a shared repository. Continuous Deployment (CD) takes it a step further by automating the deployment of code changes to production or other environments. AWS provides a suite of services to seamlessly implement CI/CD pipelines.


### Step 1: Setting up CodeCommit
1. Log in to the AWS Management Console.
2. Navigate to the CodeCommit service.

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

3. Click on Create repository.


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

4.  Give a Repository Name and click “Create”


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

5. Click on Clone URL and then Clone HTTPS


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

6. Navigate to IAM, select "Users," choose the desired IAM user, click on "Security Credentials," and scroll down to generate CodeCommit credentials.


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

7. Save or download the credentials.

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

8. Containerize your application using Docker. Note: I am using a free website template from https://www.free-css.com/free-css-templates/page296/spering for this process.

```
wget https://www.free-css.com/free-css-templates/page296/spering.zip
Unzip spering.zip
```

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

```
#Dockerfile
# Use a base image with a lightweight web server
FROM nginx:alpine
 
# Set the working directory to the web root
WORKDIR /usr/share/nginx/html
 
# Copy the website files to the container
COPY . .
 
# Expose port 80 for the web server
EXPOSE 80
```

9. Push the containerized application into CodeCommit using Git commands.

```
git init 
git status
git add .
git commit –m “enter your message”
git remote origin <repository-url copied from step 5>
git push origin master 
```


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


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


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


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


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




### Step 2: Setting up ECR

1.Navigate to the Elastic Container Registry (ECR) service.

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

2. Click on "Create repository," provide a name for the repository, and proceed to create it.

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

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

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



### Step  3: Setup EC2 

Note: Follow these steps to launch an instance.
[https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/option3-task1-launch-ec2-instance.html](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/option3-task1-launch-ec2-instance.html)



### Step 4: Set up the target group and load balancer.

1. In the left navbar, navigate to Load Balancing, click on "Target Groups," and proceed to create a new target group.

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

2.Give target group a name and create

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

3. Create a load balancer.

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



### Step 5: Set up ECS (Elastic Container Service).

1. Navigate to the Elastic Container Service.

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

2. Create a cluster, assign it a name, and select "External instances" for the infrastructure using ECS Anywhere.

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

3. Register your instance with ECS.

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

4. Create a task definition.

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

5.Provide a name for the task definition, select "EC2" as the launch type, and allocate the required CPU and memory resources (e.g., for a t2.medium instance family, allocate 1 vCPU and 0.8GB memory).

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

6.Under "Container," specify details such as the container name, image URI (copy it from the ECR repository where the image was pushed), and the host and container port settings.

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

7. Navigate to the created cluster, go to the "Services" tab, and proceed to create a new service.


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

8.Select the launch type as EC2, choose the task definition family created earlier, and assign a name to the service.

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

9. Expand the "Load balancer" section, select "Application Load Balancer," use an existing load balancer (choose the one created earlier), and for the listener, select the target group created earlier.

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



### Step 6: Setup Codepipeine

1.Navigate to the CodePipeline service.

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

2.Click on "Create pipeline"

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

3. Specify the pipeline name, select the pipeline type, and provide the role name.

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

4.Select the source provider, then choose the repository and branch created on CodeCommit.

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

5.For the build phase, opt for a build provider such as AWS CodeBuild, and click "Create project" as demonstrated below; a new tab will open.

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

6. Provide a name for the build.

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

7. Select the operating system, runtime, image, image version, and role name. Ensure that the role has the necessary permissions to access ECR and CloudWatch.

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

8. Create a buildspec.yaml file.


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

```
version: 0.2
 
env:
  variables:
    REPOSITORY_URI: "xxxxxxx.dkr.ecr.us-east-1.amazonaws.com/mywebsite"
 
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin xxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $REPOSITORY_URI:latest .
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $REPOSITORY_URI:latest
      - echo Writing image definitions file...
      - printf '[{"name":"mywebsite","imageUri":"%s"}]' $REPOSITORY_URI:latest > imagedefinitions.json
 
artifacts:
  files: imagedefinitions.json
```

Note: Obtain the push command and Repository URI from Amazon ECR.

9. Proceed with the pipeline setup and move to the next steps.

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

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

10. Configure the deployment by selecting Amazon ECS as the deploy provider. Choose the previously created cluster and service on ECS. Provide the image definitions file and proceed by clicking "Next."

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

11. Review the configuration and proceed to create the pipeline.

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

12. Release the changes or push any updates to CodeCommit; AWS CodePipeline will automatically initiate and execute the configured deployment process.

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

13. After a successful build, you can access your website through the Load Balancer DNS or map a custom domain to the Load Balancer using Route 53.

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



### Step 7: Check CI/CD

1. Open your HTML file and make the desired changes.

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

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

2. Push the changes you made in the HTML file to your CodeCommit repository.

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

3. The pipeline will be automatically triggered, and your changes will be deployed automatically after a successful deployment.

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

4. You can now view your changes on your website.

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



## Conclusion

Implementing CI/CD in AWS with CodeCommit, CodeDeploy, CodePipeline, ECR, and ECS streamlines your development workflow, ensuring rapid and reliable delivery of your applications. By following these steps, you can create a robust CI/CD pipeline that integrates seamlessly with AWS services.



### Additional Resources:
AWS CodeCommit: [https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)
AWS CodeDeploy: [https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html](https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html)
AWS CodePipeline: [https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html)
AWS ECR: [https://docs.aws.amazon.com/amazon-ecr/latest/userguide/what-is-ecr.html](https://docs.aws.amazon.com/amazon-ecr/latest/userguide/what-is-ecr.html)
AWS ECS: [https://docs.aws.amazon.com/ecs/latest/userguide/intro.html](https://docs.aws.amazon.com/ecs/latest/userguide/intro.html)