AWS Builder Center
Build an Automated CI/CD Pipeline on AWS Using GitHub, CodeBuild, ECR, and ECS Fargate

Build an Automated CI/CD Pipeline on AWS Using GitHub, CodeBuild, ECR, and ECS Fargate

Build a real-world DevOps workflow that automatically builds, tests, containerizes, and deploys a Java Spring Boot application. Explore GitHub integration, AWS CodePipeline, CodeBuild, Docker, Amazon ECR, and ECS Fargate through a practical end-to-end CI/CD project.

Cloud & DevOps Enthusiast

Week 05: Build an Automated CI/CD Pipeline on AWS Using GitHub, CodeBuild, ECR, and ECS Fargate

Introduction

In modern software development, writing application code is only one part of the development lifecycle. Every application must be built, tested, packaged, deployed, and monitored.
Imagine making a small change to a Java Spring Boot application. Traditionally, a developer might need to:
  • Build the application manually.
  • Run tests.
  • Create a Docker image.
  • Push the image to a container registry.
  • Update the deployment.
  • Restart the application.
  • Verify whether the new version is working.
Repeating these steps for every code change takes time and introduces human errors.
This is where Continuous Integration and Continuous Delivery (CI/CD) become important.
In this article, we'll build an automated deployment pipeline using AWS services. Whenever new code is pushed to GitHub, AWS will automatically build, test, containerize, and deploy the application to Amazon ECS using AWS Fargate.
Our project will be a containerized Java Spring Boot application called CloudDeploy.
1. Understanding the CI/CD Architecture

Before implementing the pipeline, let's understand the responsibility of each service.
Service
Responsibility
GitHub
Stores application source code
AWS CodePipeline
Orchestrates the deployment workflow
AWS CodeBuild
Builds and tests the application
Docker
Packages the application into a container
Amazon ECR
Stores Docker images
Amazon ECS
Manages container deployment
AWS Fargate
Runs containers without managing servers
AWS IAM
Controls permissions between AWS services
Amazon CloudWatch
Monitors application and deployment logs

Architecture workflow

GitHub → CodePipeline → CodeBuild → Maven Tests → Docker Build → Amazon ECR → Amazon ECS Fargate.
When a developer pushes new code to GitHub, CodePipeline detects the change and starts the workflow.
CodeBuild runs the build and test commands, creates a Docker image, and pushes it to ECR. CodePipeline then updates the ECS service to run the newly created image.
The entire process happens automatically.

2. Prerequisites

Before starting, make sure you have:
  • An AWS account.
  • A GitHub account.
  • Java 21 installed.
  • Maven installed or a Maven wrapper in your project.
  • Docker installed for local testing.
  • AWS CLI configured with appropriate permissions.
  • Basic knowledge of Spring Boot and Docker.
For this project, we'll use the following configuration:
Component
Configuration
Application
Spring Boot REST API
Language
Java 21
Build tool
Maven
Container
Docker
Registry
Amazon ECR
Deployment
Amazon ECS Fargate
Pipeline
AWS CodePipeline
Build service
AWS CodeBuild
Use the same AWS Region for the pipeline, ECR repository, ECS cluster, and related resources.

3. Prepare the Spring Boot Application

First, create a Spring Boot application.
Our application will expose a simple REST endpoint that returns a message and the application version.
Create the following project structure:
CloudDeploy/
│
├── src/
│ ├── main/
│ │ └── java/com/example/clouddeploy/
│ │ └── CloudDeployApplication.java
│ │ └── HealthController.java
│ │
│ └── test/
│
├── pom.xml
├── Dockerfile
├── buildspec.yml
└── .gitignore

Create the REST Controller

package com.example.clouddeploy;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthController {

@GetMapping("/")
public String home() {
return "CloudDeploy is running on AWS!";
}

@GetMapping("/health")
public String health() {
return "Application Status: UP";
}
}
The application provides two endpoints:
  •  /  – Returns the application welcome message.
  •  /health  – Returns the application status.
Run the application locally:
mvn spring-boot:run
Test the endpoints:
http://localhost:8080/
http://localhost:8080/health
Expected response:
CloudDeploy is running on AWS!
Once the application works locally, prepare it for containerization.
4. Containerize the Application Using Docker

Docker allows us to package an application with its runtime dependencies into a portable container image.
Create a file named  Dockerfile  in the project root.
FROM maven:3.9-eclipse-temurin-21 AS build

WORKDIR /app

COPY pom.xml .
COPY src ./src

RUN mvn clean package -DskipTests

FROM eclipse-temurin:21-jre

WORKDIR /app

COPY --from=build /app/target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]
This is a multi-stage Docker build.
Build stage: Uses Maven and JDK 21 to package the application.
Runtime stage: Uses a smaller Java runtime image to execute the generated JAR file.
Build the Docker image:
docker build -t clouddeploy .
Run the container:
docker run -p 8080:8080 clouddeploy
Verify that the application works:
curl http://localhost:8080/health
Expected response:
Application Status: UP
Now our application is ready for deployment.

5. Create an Amazon ECR Repository

Amazon Elastic Container Registry (ECR) is a managed container image registry.
It stores Docker images that can be used by services such as Amazon ECS.
Open the AWS Management Console and:
  1. Search for Amazon ECR.
  1. Open Private repositories.
  1. Select Create repository.
  1. Enter the repository name:
 clouddeploy 
  1. Keep the required settings and create the repository.
You can also create it using AWS CLI:
aws ecr create-repository \
--repository-name clouddeploy \
--region ap-south-1
Replace  ap-south-1  with your chosen AWS Region if necessary.
Note the repository URI displayed in the output.
It will look similar to:
ACCOUNT_ID.dkr.ecr.ap-south-1.amazonaws.com/clouddeploy
This is where CodeBuild will push the Docker image.

6. Create an ECS Cluster and Fargate Service

Amazon Elastic Container Service (ECS) manages containerized applications.
AWS Fargate allows ECS to run containers without requiring us to provision or manage EC2 instances.

Create an ECS cluster

  1. Open Amazon ECS.
  1. Select Clusters.
  1. Choose Create cluster.
  1. Enter the cluster name:
 CloudDeployCluster 
  1. Create the cluster.

Create a task definition

A task definition describes how your container should run.
Configure the following:
Setting
Value
Family
clouddeploy-task
Launch type
AWS Fargate
Operating system
Linux
CPU architecture
X86_64
CPU
0.25 vCPU
Memory
0.5 GB
Container name
clouddeploy
Container port
8080
For the initial deployment, use a valid image URI from your ECR repository.
Configure an ECS task execution role with the permissions needed to pull images from ECR and publish logs to CloudWatch.

Create an ECS service

  1. Open your ECS cluster.
  1. Select Create service.
  1. Choose AWS Fargate.
  1. Select the task definition.
  1. Set the desired task count.
  1. Configure the VPC and subnets.
  1. Configure a security group to allow the required application traffic.
  1. Create the service.
For a public demonstration, configure an Application Load Balancer or another suitable controlled access path. Do not expose application containers directly without considering network security.
At this stage, the ECS service has been created. Next, we'll automate updates to it.

7. Configure AWS CodeBuild

AWS CodeBuild is a managed build service that compiles source code, runs tests, and creates deployment artifacts.
In our project, CodeBuild will:
  1. Download the source code.
  1. Build the Spring Boot application.
  1. Run Maven tests.
  1. Build the Docker image.
  1. Authenticate with Amazon ECR.
  1. Push the Docker image.
  1. Generate the ECS deployment image definition.

Create buildspec.yml

The  buildspec.yml  file tells CodeBuild which commands to execute.
Create this file in the project root:
version: 0.2

phases:
pre_build:
commands:
- echo Logging in to Amazon ECR
- ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
- REPOSITORY_URI=$ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $REPOSITORY_URI

build:
commands:
- echo Building Spring Boot application
- mvn clean test package
- echo Building Docker image
- docker build -t $REPOSITORY_URI:$IMAGE_TAG .

post_build:
commands:
- echo Pushing Docker image
- docker push $REPOSITORY_URI:$IMAGE_TAG
- printf '[{"name":"clouddeploy","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json

artifacts:
files:
- imagedefinitions.json

Important environment variables

Configure these in the CodeBuild project:
Variable
Example
AWS_DEFAULT_REGION
ap-south-1
IMAGE_REPO_NAME
clouddeploy
 CODEBUILD_RESOLVED_SOURCE_VERSION  is supplied by CodeBuild and identifies the source revision.
The image tag uses the first seven characters of the source revision, helping us associate an image with a particular code change.
Why is imagedefinitions.json important?
The ECS standard deployment action in CodePipeline uses this file to identify the container and the new image URI.
For example:
[
{
"name": "clouddeploy",
"imageUri": "ACCOUNT_ID.dkr.ecr.ap-south-1.amazonaws.com/clouddeploy:abc1234"
}
]
The container name must match the container name in the ECS task definition.

Configure the CodeBuild project

  1. Open AWS CodeBuild.
  1. Select Create build project.
  1. Enter the project name  CloudDeployBuild .
  1. Connect your GitHub source.
  1. Select a managed Linux build environment.
  1. Choose a suitable standard build image.
  1. Enable privileged mode because this example builds Docker images.
  1. Configure the service role.
  1. Set the environment variables.
  1. Save the project.
The CodeBuild role needs permissions for the required ECR operations, including authentication and image upload. It also needs access to the build logs and other resources used by the build.
Use a scoped IAM policy rather than granting unrestricted permissions.
8. Create the CI/CD Pipeline Using AWS CodePipeline

Now comes the most important part.
AWS CodePipeline connects our GitHub repository, CodeBuild project, and ECS service into one automated workflow.
Open AWS CodePipeline.
Select Create pipeline and configure the following.

Stage 1: Source

  • Source provider: GitHub through AWS CodeConnections.
  • Connect your GitHub account.
  • Select your CloudDeploy repository.
  • Select the  main  branch.
  • Configure the source change detection option.
Whenever a new commit reaches the configured branch, the pipeline can start automatically.

Stage 2: Build

  • Build provider: AWS CodeBuild.
  • Select the  CloudDeployBuild  project.
  • Use the source artifact as the build input.
  • Configure the build output artifact.
CodeBuild will execute the commands in  buildspec.yml .
The Docker image will be pushed to Amazon ECR, and  imagedefinitions.json  will be provided as the deployment artifact.

Stage 3: Deploy

  • Deploy provider: Amazon ECS.
  • Select  CloudDeployCluster .
  • Select your ECS service.
  • Set the image definitions file to  imagedefinitions.json .
Save and create the pipeline.
The ECS deployment action will use the new image definition to create an updated task definition revision and update the ECS service.

9. Test the Complete CI/CD Pipeline

Now let's test whether our automation works.
Open the  HealthController.java  file and change the welcome message:
@GetMapping("/")
public String home() {
return "Version 2 - Automatically deployed on AWS!";
}
Commit and push the changes:
git add .
git commit -m "Update application version"
git push origin main
Now open AWS CodePipeline.
You should see the pipeline execute its stages:
  1. Source – GitHub change detected.
  1. Build – CodeBuild starts.
  1. Test – Maven tests execute.
  1. Containerization – Docker image is created.
  1. Image storage – New image is pushed to ECR.
  1. Deployment – ECS service is updated.
Wait until the deployment succeeds and the ECS service reaches a stable state.
Test the application using its configured public endpoint:
https://YOUR_APPLICATION_ENDPOINT/
Expected response:
Version 2 - Automatically deployed on AWS!
<escape>Congratulations!</escape> Your application has been updated without manually rebuilding and deploying it through the console.

10. Monitoring and Troubleshooting

A successful deployment is not the end of the process. We also need to monitor the application and identify failures.

Amazon CloudWatch

Use CloudWatch to monitor:
  • CodeBuild build logs.
  • ECS container logs.
  • Task failures.
  • CPU utilization.
  • Memory utilization.
  • Application errors.
  • Deployment-related events.
Configure the ECS task definition with the  awslogs  log driver to send container output to CloudWatch Logs.

Common errors and solutions

Error
Possible solution
ECR authentication failed
Check CodeBuild ECR permissions and Region
Docker build failed
Verify Dockerfile and build environment
Maven build failed
Check Java version and dependencies
ECS task stopped
Inspect ECS stopped reason and container logs
Image not found
Verify ECR repository URI and image tag
Deployment failed
Verify container name and  imagedefinitions.json 
Application unavailable
Check security groups, target health, and application port
Use the CodeBuild logs and ECS service events to identify the actual cause rather than changing permissions blindly.

11. Advanced Improvements for Production

Our pipeline demonstrates automated deployment, but production systems require additional controls.
Here are some improvements worth exploring:
1. Blue/Green Deployment
Deploy a new application version alongside the existing version. Test it before shifting production traffic. AWS CodeDeploy can coordinate blue/green deployments for ECS.
2. Image Vulnerability Scanning
Use Amazon ECR image scanning and Amazon Inspector to identify vulnerabilities in container images.
3. Infrastructure as Code
Use AWS CloudFormation or Terraform to provision the pipeline, IAM roles, ECR repository, ECS cluster, and networking consistently.
4. Automated Quality Gates
Add unit tests, integration tests, code quality checks, and security scans before allowing deployment.
5. Rollback Strategy
Configure deployment failure handling and rollback procedures so an unhealthy release does not remain in production.
6. Secrets Management
Store database passwords and application secrets in AWS Secrets Manager or Systems Manager Parameter Store instead of embedding them in Docker images or source code.

12. Cost and Resource Cleanup

AWS services used in this project can incur charges.
Potential cost sources include:
  • CodePipeline usage.
  • CodeBuild build minutes.
  • Amazon ECR image storage.
  • ECS Fargate CPU and memory usage.
  • Application Load Balancer.
  • CloudWatch logs and monitoring.
  • Networking and data transfer.
For a learning project, avoid leaving resources running unnecessarily.
After completing your demonstration:
  1. Delete the CodePipeline.
  1. Delete the CodeBuild project if no longer needed.
  1. Scale down or delete the ECS service.
  1. Delete the ECS cluster after removing its services.
  1. Delete unused ECR images and repositories.
  1. Remove unused load balancers and networking resources.
  1. Review CloudWatch log groups and other remaining resources.
Always check the AWS Billing dashboard for any continuing charges.

Conclusion

We have successfully designed an automated CI/CD workflow using AWS.
Instead of manually building and deploying every application update, our pipeline automates the complete process—from a GitHub commit to a running application on ECS Fargate.
This project demonstrates how developers and DevOps engineers use cloud services to improve deployment speed, consistency, and reliability.
The important lesson is that CI/CD is not just about automating deployment; it is about building a repeatable and controlled software delivery process.
Keep building real-world cloud projects, experiment with deployment strategies, and understand how each AWS service works together.
The next step in your cloud journey is not just deploying applications. It is engineering reliable deployment systems.

Official AWS Documentation

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article