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
- Log in to the AWS Management Console.
- Navigate to the CodeCommit service.
- Click on Create repository.
- Give a Repository Name and click “Create”
- Click on Clone URL and then Clone HTTPS
- Navigate to IAM, select "Users," choose the desired IAM user, click on "Security Credentials," and scroll down to generate CodeCommit credentials.
- Save or download the credentials.
- 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
#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
- 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
Step 2: Setting up ECR
1.Navigate to the Elastic Container Registry (ECR) service.
- Click on "Create repository," provide a name for the repository, and proceed to create it.
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.
- In the left navbar, navigate to Load Balancing, click on "Target Groups," and proceed to create a new target group.
2.Give target group a name and create
- Create a load balancer.
Step 5: Set up ECS (Elastic Container Service).
- Navigate to the Elastic Container Service.
- Create a cluster, assign it a name, and select "External instances" for the infrastructure using ECS Anywhere.
- Register your instance with ECS.
- Create a task definition.
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).
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.
- Navigate to the created cluster, go to the "Services" tab, and proceed to create a new service.
8.Select the launch type as EC2, choose the task definition family created earlier, and assign a name to the service.
- 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.
Step 6: Setup Codepipeine
1.Navigate to the CodePipeline service.
2.Click on "Create pipeline"
- Specify the pipeline name, select the pipeline type, and provide the role name.
4.Select the source provider, then choose the repository and branch created on CodeCommit.
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.
- Provide a name for the build.
- Select the operating system, runtime, image, image version, and role name. Ensure that the role has the necessary permissions to access ECR and CloudWatch.
- Create a buildspec.yaml file.
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.
- Proceed with the pipeline setup and move to the next steps.
- 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."
- Review the configuration and proceed to create the pipeline.
- Release the changes or push any updates to CodeCommit; AWS CodePipeline will automatically initiate and execute the configured deployment process.
- 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.
Step 7: Check CI/CD
- Open your HTML file and make the desired changes.
- Push the changes you made in the HTML file to your CodeCommit repository.
- The pipeline will be automatically triggered, and your changes will be deployed automatically after a successful deployment.
- You can now view your changes on your website.
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)