Full Stack • Java • System Design • Cloud • AI Engineering

AWS CodePipeline, CodeBuild & CodeDeploy with Spring Boot

Learn how to build a complete CI/CD pipeline for Spring Boot applications using AWS CodePipeline, CodeBuild, and CodeDeploy. This guide covers automated builds, testing, Docker image creation, Amazon ECR integration, ECS/EC2 deployments, blue-green deployments, rollback strategies, and production best practices.


Introduction

Modern software development requires applications to be delivered quickly, safely, and consistently.

Imagine a development team manually performing these tasks for every release:

  • Pull source code
  • Build project
  • Run unit tests
  • Create JAR
  • Build Docker image
  • Push image to Amazon ECR
  • Deploy to ECS
  • Verify deployment
  • Rollback on failure

Doing these manually is:

  • Slow
  • Error-prone
  • Difficult to scale
  • Difficult to audit

AWS provides a complete CI/CD solution using:

  • AWS CodePipeline
  • AWS CodeBuild
  • AWS CodeDeploy

Together, these services automate the entire deployment lifecycle.


Learning Objectives

After completing this article, you will understand:

  • What is CI/CD?
  • AWS CodePipeline
  • AWS CodeBuild
  • AWS CodeDeploy
  • Build Automation
  • Testing Automation
  • Docker Image Creation
  • Amazon ECR Integration
  • ECS Deployment
  • EC2 Deployment
  • Blue-Green Deployment
  • Rollback Strategy
  • Production Best Practices

What is CI/CD?

CI = Continuous Integration

Developers frequently merge code into a shared repository.

CD = Continuous Delivery / Deployment

Applications are automatically built, tested, and deployed.

Benefits:

  • Faster releases
  • Reduced manual work
  • Higher software quality
  • Safer deployments

Manual Deployment

Developer

↓

Build

↓

Test

↓

Copy JAR

↓

Deploy

↓

Restart Server

Problems:

  • Human error
  • Downtime
  • Slow deployments
  • Difficult rollback

Automated CI/CD

Developer

↓

Git Push

↓

Pipeline

↓

Production

Everything happens automatically.


AWS CI/CD Services

Service Purpose
CodeCommit / GitHub Source Code
CodePipeline Workflow Orchestration
CodeBuild Build & Test
Amazon ECR Store Docker Images
CodeDeploy Deployment
ECS / EC2 / Lambda Runtime

High-Level Architecture

flowchart LR

Developer

GitHub

CodePipeline

CodeBuild

AmazonECR

CodeDeploy

AmazonECS

Developer --> GitHub
GitHub --> CodePipeline
CodePipeline --> CodeBuild
CodeBuild --> AmazonECR
AmazonECR --> CodeDeploy
CodeDeploy --> AmazonECS

Enterprise CI/CD Architecture

flowchart TD

Developer

GitHub

CodePipeline

CodeBuild

SonarQube

JUnit

Docker

AmazonECR

CodeDeploy

AmazonECS

CloudWatch

Developer --> GitHub
GitHub --> CodePipeline

CodePipeline --> CodeBuild
CodeBuild --> SonarQube
CodeBuild --> JUnit
CodeBuild --> Docker

Docker --> AmazonECR

AmazonECR --> CodeDeploy
CodeDeploy --> AmazonECS

AmazonECS --> CloudWatch

CodePipeline

AWS CodePipeline orchestrates every deployment stage.

Typical stages:

  • Source
  • Build
  • Test
  • Approval
  • Deploy

Pipeline Flow

flowchart LR

Source

Build

Test

Approval

Deploy

Source --> Build
Build --> Test
Test --> Approval
Approval --> Deploy

Source Stage

Source can be:

  • GitHub
  • AWS CodeCommit
  • Bitbucket

Developer pushes code.

Pipeline starts automatically.


Example Repository

springboot-demo

├── src
├── Dockerfile
├── buildspec.yml
├── appspec.yml
└── pom.xml

CodeBuild

CodeBuild compiles the application.

Typical tasks:

  • Maven Build
  • Gradle Build
  • Unit Tests
  • Static Analysis
  • Docker Build

Build Workflow

flowchart LR

Source

Compile

Test

Package

DockerImage

Source --> Compile
Compile --> Test
Test --> Package
Package --> DockerImage

buildspec.yml

version: 0.2

phases:

  install:
    runtime-versions:
      java: corretto21

  build:
    commands:
      - mvn clean package -DskipTests

Running Tests

mvn test

Pipeline stops if tests fail.


Docker Build

docker build \
-t springboot-demo:1.0 .

Push Image to Amazon ECR

docker push \
123456789012.dkr.ecr.us-east-1.amazonaws.com/springboot-demo:1.0

CodeDeploy

CodeDeploy automates deployments.

Supports:

  • EC2
  • ECS
  • Lambda
  • On-Premises

Deployment Flow

flowchart LR

CodeDeploy

Target

Deploy

Verify

Success

CodeDeploy --> Target
Target --> Deploy
Deploy --> Verify
Verify --> Success

appspec.yml

version: 0.0

os: linux

files:
  - source: /
    destination: /home/ec2-user/app

hooks:

  ApplicationStart:

    - location: scripts/start.sh

ECS Deployment

Pipeline

Build Docker Image

Push to ECR

Deploy ECS Task

New Containers Running


EC2 Deployment

Pipeline

Copy JAR

Stop Application

Deploy

Restart Spring Boot


Blue-Green Deployment

flowchart LR

Users

LoadBalancer

Blue

Green

Users --> LoadBalancer
LoadBalancer --> Blue
LoadBalancer --> Green

Benefits:

  • Zero downtime
  • Easy rollback
  • Safe deployment

Rolling Deployment

Server 1

↓

Server 2

↓

Server 3

Instances updated one at a time.


Canary Deployment

Version A

90%

Version B

10%

Traffic gradually shifts.


Rollback Strategy

If deployment fails:

Deploy

↓

Health Check Failed

↓

Rollback

↓

Previous Version

Automatic rollback minimizes downtime.


Manual Approval

Production deployments often include:

Build

↓

QA

↓

Manual Approval

↓

Production

Useful for regulated industries.


Notifications

Integrate with:

  • Amazon SNS
  • Slack
  • Email

Notify:

  • Build Success
  • Build Failure
  • Deployment Failure

Logging

Collect logs from:

  • CodeBuild
  • CodeDeploy
  • ECS
  • EC2

Store in:

CloudWatch Logs


Monitoring

Monitor:

  • Build Duration
  • Deployment Time
  • Failed Builds
  • Failed Deployments
  • Pipeline Success Rate

Production Architecture

flowchart TD

Developer

GitHub

CodePipeline

CodeBuild

Docker

AmazonECR

CodeDeploy

AmazonECS

ALB

SpringBootContainers

Aurora

CloudWatch

Developer --> GitHub
GitHub --> CodePipeline
CodePipeline --> CodeBuild
CodeBuild --> Docker
Docker --> AmazonECR
AmazonECR --> CodeDeploy
CodeDeploy --> AmazonECS
AmazonECS --> ALB
ALB --> SpringBootContainers
SpringBootContainers --> Aurora
SpringBootContainers --> CloudWatch

Security Best Practices

  • Use IAM Roles for CodeBuild
  • Store secrets in AWS Secrets Manager
  • Never hardcode credentials
  • Use least privilege IAM policies
  • Enable artifact encryption
  • Scan Docker images before deployment
  • Use HTTPS for repositories
  • Restrict production approvals

Common Errors

Build Failed

Verify:

  • Maven dependencies
  • Java version
  • Unit tests

Docker Build Failed

Check:

  • Dockerfile
  • Image name
  • Build context

Deployment Failed

Verify:

  • appspec.yml
  • IAM permissions
  • Target group health checks

ECS Task Failed

Review:

  • Container logs
  • Environment variables
  • CPU and memory settings

Best Practices

  • Automate everything
  • Run unit tests on every commit
  • Build immutable Docker images
  • Store images in Amazon ECR
  • Use Blue-Green deployments
  • Enable automatic rollback
  • Keep pipelines small and modular
  • Use separate pipelines for Dev, QA, and Production
  • Monitor pipeline metrics
  • Integrate notifications
  • Scan code and images for vulnerabilities
  • Version all deployment artifacts

Developer Checklist

Before production deployment:

  • Source repository connected
  • CodePipeline created
  • CodeBuild project configured
  • buildspec.yml added
  • Docker image builds successfully
  • Image pushed to Amazon ECR
  • CodeDeploy configured
  • appspec.yml verified
  • Rollback enabled
  • CloudWatch monitoring enabled
  • SNS notifications configured

Interview Questions

What is AWS CodePipeline?

AWS CodePipeline is a managed CI/CD orchestration service that automates the software release process from source to deployment.


What is AWS CodeBuild?

AWS CodeBuild is a fully managed build service that compiles source code, runs tests, and produces deployment artifacts.


What is AWS CodeDeploy?

AWS CodeDeploy automates application deployments to Amazon EC2, Amazon ECS, AWS Lambda, and on-premises servers.


What is the purpose of buildspec.yml?

buildspec.yml defines the build phases, commands, environment, and artifacts executed by AWS CodeBuild.


What is the purpose of appspec.yml?

appspec.yml tells AWS CodeDeploy how to deploy the application, including file locations and lifecycle hooks.


Why use Blue-Green Deployment?

Blue-Green deployments minimize downtime and allow quick rollback by switching traffic between two identical environments.


What are the benefits of CI/CD?

  • Faster releases
  • Reduced manual effort
  • Consistent deployments
  • Early bug detection
  • Improved software quality
  • Easier rollback

Summary

In this article, we learned how to automate Spring Boot deployments using AWS CodePipeline, CodeBuild, and CodeDeploy.

We covered:

  • CI/CD fundamentals
  • CodePipeline workflow
  • CodeBuild configuration
  • buildspec.yml
  • Docker image creation
  • Amazon ECR integration
  • CodeDeploy
  • appspec.yml
  • ECS deployment
  • Blue-Green deployments
  • Rollback strategies
  • Monitoring
  • Production best practices

AWS CodePipeline, CodeBuild, and CodeDeploy provide a complete managed CI/CD platform for Spring Boot applications. When integrated with GitHub, Docker, Amazon ECR, ECS, CloudWatch, and IAM, they enable reliable, repeatable, and secure software delivery pipelines suitable for enterprise-scale systems.


Loading likes...

Comments

Share a question, correction, or practical insight about this article.

Loading approved comments...