DEV Community

Cover image for Setting up CI/CD in AWS with CodeCommit, CodeDeploy, CodePipeline, ECR, and ECS

Setting up CI/CD in AWS with CodeCommit, CodeDeploy, CodePipeline, ECR, and ECS

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

  1. Click on Create repository.

Image description

  1. Give a Repository Name and click “Create”

Image description

  1. Click on Clone URL and then Clone HTTPS

Image description

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

Image description

  1. Save or download the credentials.

Image description

  1. 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
Enter fullscreen mode Exit fullscreen mode

Image description

#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
Enter fullscreen mode Exit fullscreen mode
  1. 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 
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description

Step 2: Setting up ECR

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

Image description

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

Image description

Image description

Image description

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

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

2.Give target group a name and create

Image description

  1. Create a load balancer.

Image description

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

  1. Navigate to the Elastic Container Service.

Image description

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

Image description

  1. Register your instance with ECS.

Image description

  1. Create a task definition.

Image description

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

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
Image description

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

Image description

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

Image description

  1. 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

Step 6: Setup Codepipeine

1.Navigate to the CodePipeline service.

Image description

2.Click on "Create pipeline"

Image description

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

Image description

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

Image description

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

  1. Provide a name for the build.

Image description

  1. 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

  1. Create a buildspec.yaml file.

Image description

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
Enter fullscreen mode Exit fullscreen mode

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

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

Image description

Image description

  1. 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

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

Image description

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

Image description

  1. 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

Step 7: Check CI/CD

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

Image description

Image description

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

Image description

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

Image description

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

Image description

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
AWS CodeDeploy: https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html
AWS CodePipeline: 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
AWS ECS: https://docs.aws.amazon.com/ecs/latest/userguide/intro.html

Top comments (0)