AWS Builder Center

Automating Application Deployment with AWS CI/CD

Learn how to automate application deployment using AWS CI/CD services. This article explains how CodePipeline, CodeBuild, CodeDeploy, and Amazon EC2 work together to build, test, and deploy application updates automatically. Explore the architecture, configuration steps, practical examples, and benefits of automating software delivery on AWS.

Building an Automated CI/CD Pipeline with AWS

Introduction

Deploying an application manually every time new code is developed can become repetitive and time-consuming. Developers need to build the application, run tests, prepare deployment packages, and upload the updated version to the server.
As applications grow, this process becomes harder to manage and increases the possibility of human errors.
This is where Continuous Integration and Continuous Deployment (CI/CD) become useful.
AWS provides several services that help automate the software delivery process. Using services such as AWS CodePipeline, AWS CodeBuild, and Amazon EC2, developers can create a deployment pipeline that automatically builds, tests, and deploys applications whenever new changes are pushed to a Git repository.
In this article, we will explore how to automate application deployment using AWS CI/CD services.

1. Understanding CI/CD

Before building the pipeline, let's understand the two main concepts.

Continuous Integration (CI)

Continuous Integration is the practice of frequently merging code changes into a shared repository. Whenever developers push new changes, an automated process can build the application and run tests to identify errors early.

Continuous Deployment (CD)

Continuous Deployment takes the process further by automatically releasing successfully tested application changes to the target environment.
Together, CI/CD reduces manual work and makes application updates more consistent and reliable.

2. AWS Services Used

For this implementation, we will use the following AWS services:
AWS Service
Purpose
AWS CodePipeline
Coordinates the entire CI/CD workflow
AWS CodeBuild
Builds the application and runs tests
Amazon EC2
Hosts the application
Amazon S3
Stores build artifacts when required
AWS IAM
Manages permissions between AWS services
Amazon CloudWatch
Monitors logs and build activity
Each service has a specific responsibility in the deployment process.

3. Understanding the CI/CD Architecture

The deployment process begins when a developer pushes code to a Git repository.
CodePipeline detects the change and starts the pipeline. CodeBuild then downloads the source code, installs dependencies, builds the application, and runs the configured tests.
After a successful build, the pipeline passes the deployment package to the deployment stage. The updated application is then deployed to the target EC2 instance.
The overall workflow looks like this:
Developer → GitHub → CodePipeline → CodeBuild → Deployment → Amazon EC2

4. Preparing the Application

For this example, let's consider a simple Spring Boot application.
Before configuring AWS services, make sure the application:
  • Is available in a GitHub repository.
  • Builds successfully on your local machine.
  • Contains the required dependencies.
  • Has automated tests configured.
For a Maven-based Spring Boot application, you can build the project using:
mvn clean package
After a successful build, Maven generates the application JAR file inside the  target  directory.
For example:
my-spring-app/
│
├── src/
├── pom.xml
├── buildspec.yml
└── target/
└── my-spring-app.jar

5. Configuring AWS CodeBuild

AWS CodeBuild is a managed build service that compiles source code, runs tests, and generates build artifacts.
To configure it:
  1. Open the AWS Management Console.
  1. Navigate to AWS CodeBuild.
  1. Create a new build project.
  1. Connect your GitHub repository as the source.
  1. Select a suitable managed build environment.
  1. Configure the build instructions.
CodeBuild uses a file called  buildspec.yml  to understand the commands that should execute during the build process.
Create a  buildspec.yml  file in the root directory of your project.
version: 0.2

phases:
install:
runtime-versions:
java: corretto21

build:
commands:
- echo "Building Spring Boot application"
- mvn clean package

artifacts:
files:
- target/*.jar
This configuration tells CodeBuild to use Java 21, build the Spring Boot application using Maven, and collect the generated JAR file as an artifact.
You can also add test execution commands to the build phase so that deployment only continues when the tests succeed.

6. Creating the Deployment Environment

Amazon EC2 provides virtual servers in the AWS Cloud where applications can run.
For this example, we will use an EC2 instance as the application hosting environment.
The basic setup includes:
  1. Launch an EC2 instance.
  1. Install the required Java runtime.
  1. Configure the application directory.
  1. Configure the necessary security group rules.
  1. Create a deployment process that can replace the running application with the new build.
For example, install Java 21 on an Ubuntu-based instance:
sudo apt update
sudo apt install openjdk-21-jre -y
The EC2 instance must also have the appropriate permissions and network access to receive deployment files.
For a production environment, avoid exposing unnecessary ports and restrict administrative access.

7. Configuring AWS CodePipeline

AWS CodePipeline is responsible for connecting the different stages of the CI/CD workflow.
Create a pipeline through the AWS Management Console.

Stage 1: Source

Connect your GitHub repository using the supported GitHub connection.
Configure the repository and branch that should trigger the pipeline.
Whenever new code is pushed to the selected branch, CodePipeline can automatically start a new execution.

Stage 2: Build

Add AWS CodeBuild as the build provider.
Select the build project created earlier.
During this stage, CodeBuild executes the commands in  buildspec.yml , builds the application, and generates the deployment artifact.
If the build or configured tests fail, the pipeline stops instead of proceeding with deployment.

Stage 3: Deploy

Add a deployment stage that updates the application running on EC2.
For EC2 deployments, AWS CodeDeploy can be integrated with CodePipeline to automate the deployment process.
Create an  appspec.yml  file in the root directory of the repository.
For example:
version: 0.0

os: linux

files:
- source: target/my-spring-app.jar
destination: /opt/my-spring-app/

hooks:
ApplicationStop:
- location: scripts/stop.sh
timeout: 60

ApplicationStart:
- location: scripts/start.sh
timeout: 60
The AppSpec file tells CodeDeploy where to place the application files and which lifecycle scripts to execute.
The referenced scripts must also be included in the repository and configured with executable permissions.
The  stop.sh  script can stop the currently running application, while  start.sh  can launch the updated JAR file.
This allows CodeDeploy to manage the deployment process instead of requiring developers to manually copy and restart the application.

8. Automating the Complete Workflow

Once all the services are configured, the complete deployment process becomes automated.
Here is what happens when a developer makes a change:
  1. The developer updates the application code.
  1. The changes are pushed to GitHub.
  1. CodePipeline detects the repository update.
  1. CodeBuild downloads the source code.
  1. Maven builds the application and executes the configured tests.
  1. The build artifact is passed to the deployment stage.
  1. CodeDeploy deploys the updated application to EC2.
  1. The application starts with the latest changes.
If a build or deployment fails, the pipeline reports the failure so developers can investigate and correct the issue.

9. Monitoring and Troubleshooting

Automation does not eliminate the need for monitoring.
AWS CloudWatch can help developers monitor the pipeline and identify issues during application delivery.
Some useful monitoring areas include:
  • CodeBuild logs to identify compilation and test failures.
  • CodePipeline execution history to identify failed stages.
  • CodeDeploy deployment logs to investigate deployment problems.
  • EC2 monitoring to observe server performance.
  • Application logs to identify runtime errors.
Monitoring these components helps developers understand what happened during each deployment and resolve problems more quickly.

10. Benefits of AWS CI/CD Automation

Automating application deployment offers several advantages:
Reduced manual work: Developers no longer need to repeat every deployment step manually.
Faster releases: Code changes can move from development to deployment through an automated workflow.
Early error detection: Automated builds and tests help identify problems before deployment.
Consistency: Every deployment follows the same configured process.
Better collaboration: Multiple developers can contribute to the same repository while using a shared delivery pipeline.
Improved visibility: Pipeline history and logs help developers understand the status of each release.

Conclusion

Automating application deployment using AWS CI/CD services makes software delivery more efficient and consistent. By combining CodePipeline, CodeBuild, CodeDeploy, and Amazon EC2, developers can create a workflow that automatically builds, tests, and deploys application updates.
Instead of spending time repeating deployment tasks, developers can focus more on writing code, improving application features, and solving real-world problems.
CI/CD is an important part of modern software development, and learning how to implement it using AWS is a valuable skill for anyone interested in cloud computing and DevOps.
What would you prefer for your next project: a simple automated deployment pipeline using EC2, or a container-based CI/CD pipeline using Docker and Amazon ECS?
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