CI/CD for Spring Boot Deployment on EC2

Izzat Arramsyah
4 min readFeb 12, 2025

--

Introduction

In this modern era, CI/CD is an essential part of application development to automate the build, testing, and deployment processes. With CI/CD, developers can significantly reduce manual errors that typically occur during deployment. Additionally, CI/CD allows for faster application updates. In this article, we will walk through the process of deploying an application using GitLab CI/CD and deploying it to an EC2 instance.

PREREQUISITES

  1. AWS EC2 Instance: Set up an AWS EC2 Instance as a VPS to deploy the application.
  2. Docker: Install Docker on the AWS EC2 Instance to handle pulling and running the application.
  3. GitLab: For repository management and CI/CD.
  4. SSH Key: To access the EC2 instance securely.

Set Up EC2 Instance

To deploy the application, you need to create an EC2 instance first.

  • Login to AWS Console.
  • Launch a New Instance.
  • Configure the instance. Allow SSH access on port 22.
  • Create a New Key Pair. This will generate a .pem key.
  • Launch the Instance.
  • Connect to the EC2 Instance via SSH
ssh -i /path/to/your/key.pem ubuntu@your-ec2-ip

Containerize the Spring Boot Application

Our application will be packaged into a deployment artifact. In this case, we’ll use Docker to containerize our application.

  • Create Dockerfile
  • Test Build and Run the Image Locally
docker build -t my-app .
docker run -d -p 8080:8080 my-app

Set Up GitLab CI/CD Pipeline

  1. Create a new repository.
  • Generate an access token: Go to your GitLab profile -> Settings -> Access Tokens, and generate a new access token to be used as your GitLab password.
  • Add variables to your repository:

CI_REGISTRY -> registry.gitlab.com

CI_REGISTRY_PASSWORD -> Your access token

CI_REGISTRY_USER -> Your GitLab username

EC2_HOST -> EC2 instance’s public IP

EC2_SSH_KEY -> Your SSH key (generated from EC2 instance launch)

EC2_USER -> EC2 username (e.g., ubuntu)

2. Create the .gitlab-ci.yml file

Set Up GitLab Runner

Gitlab Runner is an open-source application used to run CI/CD pipelines defined in the .gitlab-ci.yml

  1. Install GitLab Runner

2. Register GitLab Runner

Pull image : docker pull gitlab-runner
Run Docker :

docker run -d --name gitlab-runner --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest

Docker Exec :

docker exec -it gitlab-runner gitlab-runner register

Then, follow the steps below:

  • GitLab instance URL: https://gitlab.com (or your self-hosted GitLab URL).
  • Token: Enter the token you have copied.
  • Description: Give the runner a name (e.g., docker-runner).
  • Tags: Enter relevant tags (e.g., docker, ci).
  • Executor: Select Docker.
  • Docker image: Use a base image, such as alpine:latest or golang:latest.

3. Setup SSH Key for GitLab Runner and EC2 Instance

  • Enter Gitlab Runner
docker exec -it <container_name_or_id> /bin/bash
  • Generate SSH Key
ssh-keygen -t rsa -b 4096 -C ec_user@ec_host
  • Display SSH Key
cat ~/.ssh/id_rsa.pub

4. Add SSH Key to EC2 Instance

  • Enter the EC2 instance and open the authorized_keys file and add the generated SSH key to this file
nano ~/.ssh/authorized_keys
  • If the .ssh Folder and authorized_keys File Do Not Exist, Run the Following Commands
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Push Your Code to Repo and Test the Application

  • Check Pipeline
  • Check Images on EC2 Instance
Docker Images
Docker Ps
  • Access The Application using public IP
Postman Testing

Conclusion

With CI/CD and Docker, deploying a Spring Boot application to an EC2 instance becomes an automated and seamless process. By following the steps above, we’ve created a pipeline that handles the entire lifecycle from building to deploying the application. This results in faster, more efficient deployment processes with fewer errors.

--

--

No responses yet