Deploying a Spring Boot Application to AWS EKS using Terraform, Amazon ECR, and GitHub Actions CI/CD.

Mohamed Rifkhan
4 min readApr 4, 2023

Introduction

This project demonstrates how to seamlessly deploy a Spring Boot application to the AWS Elastic Kubernetes Service (EKS) using Terraform for infrastructure management, Amazon Elastic Container Registry (ECR) for container image storage, and GitHub Actions for Continuous Integration and Continuous Deployment (CI/CD) automation. The objective is to create a scalable, efficient, and manageable infrastructure for deploying containerized Spring Boot applications.

In the initial phase, a Spring Boot application is created and dockerized to ensure portability and ease of deployment. Next, the project focuses on setting up the AWS infrastructure using Terraform, which includes creating an Amazon ECR repository for storing Docker images and provisioning an Amazon EKS cluster, VPC, and necessary networking components. After setting up the infrastructure, the project then configures a GitHub Actions CI/CD pipeline to automate building, testing, and deploying the application.

EKS-Terraform-SpringBoot Architecture

Upon pushing the code changes to the GitHub repository, the CI/CD pipeline is automatically triggered, building the Docker image, pushing it to the Amazon ECR repository, and deploying the application to the EKS cluster using the Terraform configurations. The deployed application can then be accessed via the Kubernetes service’s external IP or DNS name.

This project showcases a practical and efficient approach to deploying and managing Spring Boot applications on the AWS cloud using modern DevOps practices and tools.

Prerequisites

  1. An AWS account
  2. Terraform installed on your local machine (https://www.terraform.io/downloads.html)
  3. A GitHub account with AWS credentials configured in GitHub Actions secrets

Step 1: Create a Spring Boot Application

1.1. Use the Spring Initializr (https://start.spring.io/) to generate a new Spring Boot project or create one manually.

1.2. Add necessary dependencies and configurations to your application.

  1. 3. Test your application locally to ensure everything is working as expected.

Step 2: Dockerize the Spring Boot Application

2.1. Create a Dockerfile in the root directory of your project with the following content:

FROM openjdk:11-jdk-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

Step 3: Set Up Terraform infrastructure

3.1. Create a main.tf file in your project's root terraform directory.

3.2. Define the necessary Terraform providers, resources, and configurations. In this project, we will create the following resources:

  • Amazon ECR repository for storing the Docker image
  • Amazon VPC with necessary subnets, route tables, and security groups
  • Amazon EKS cluster and worker nodes

3.3. Run terraform init to initialize the backend and provider plugins.

3.4. Run terraform plan to review the execution plan.

3.5. Run terraform apply to create the necessary resources in your AWS account.

Step 4: Configure GitHub Actions CI/CD

4.1. In your GitHub repository, create a .github/workflows directory.

4.2. Create a new YAML file (e.g., main.yml) for your GitHub Actions workflow.

4.3. Define the workflow, including steps for building the Docker image, pushing it to ECR, and deploying it to EKS using Terraform.

4.4. Configure AWS credentials as secrets in your GitHub repository by adding the following secrets:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

Step 5: Deploy the Application

5.1. Push your changes to your GitHub repository.

5.2. GitHub Actions will automatically trigger the CI/CD pipeline.

5.3. Monitor the progress of the workflow in the “Actions” tab of your GitHub repository.

5.4. Once the deployment is complete, access your application through the Kubernetes service’s external IP or DNS name.

Conclusion:

You have now successfully deployed a Spring Boot application to AWS EKS using Terraform, Amazon ECR, and GitHub Actions for CI/CD. This setup provides a scalable and efficient infrastructure for running containerized applications. Feel free to customize the workflow and Terraform configuration to meet your specific requirements.

For a detailed look at the source code and complete project setup, please visit my GitHub repository: https://github.com/rifkhan107/java-app-aws-github-actions.git

--

--

Mohamed Rifkhan

💻 DevOps Engineer | 🎓 Bsc in Network System Engineering | 🚀 Always learning and pushing boundaries