
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.
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└── .gitignoreCreate the REST Controller
package com.example.clouddeploy;
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic 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:runTest the endpoints:
http://localhost:8080/http://localhost:8080/healthExpected 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 clouddeployVerify that the application works:
curl http://localhost:8080/healthExpected response:
Application Status: UPNow 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:
- Search for Amazon ECR.
- Open Private repositories.
- Select Create repository.
- Enter the repository name:
clouddeploy - 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-1Replace
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/clouddeployThis 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
- Open Amazon ECS.
- Select Clusters.
- Choose Create cluster.
- Enter the cluster name:
CloudDeployCluster - 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
- Open your ECS cluster.
- Select Create service.
- Choose AWS Fargate.
- Select the task definition.
- Set the desired task count.
- Configure the VPC and subnets.
- Configure a security group to allow the required application traffic.
- 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:
- Download the source code.
- Build the Spring Boot application.
- Run Maven tests.
- Build the Docker image.
- Authenticate with Amazon ECR.
- Push the Docker image.
- 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.jsonImportant 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
- Open AWS CodeBuild.
- Select Create build project.
- Enter the project name
CloudDeployBuild.
- Connect your GitHub source.
- Select a managed Linux build environment.
- Choose a suitable standard build image.
- Enable privileged mode because this example builds Docker images.
- Configure the service role.
- Set the environment variables.
- 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
mainbranch.
- 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
CloudDeployBuildproject.
- 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 mainNow open AWS CodePipeline.
You should see the pipeline execute its stages:
- Source – GitHub change detected.
- Build – CodeBuild starts.
- Test – Maven tests execute.
- Containerization – Docker image is created.
- Image storage – New image is pushed to ECR.
- 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:
- Delete the CodePipeline.
- Delete the CodeBuild project if no longer needed.
- Scale down or delete the ECS service.
- Delete the ECS cluster after removing its services.
- Delete unused ECR images and repositories.
- Remove unused load balancers and networking resources.
- 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
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article