Deploying a Spring Boot application on AWS Elastic Beanstalk

Deploying a Spring Boot application on AWS Elastic Beanstalk involves several steps. Below is a detailed guide to help you deploy your Spring Boot application:

Step 1: Prepare Your Spring Boot Application

Create a Spring Boot Application: Ensure that your Spring Boot application is working locally. You can use Spring Initializr to create a new Spring Boot project.

Package the Application: Use Maven or Gradle to package your application into a .jar or .war file. For Maven, run the following command:

mvn clean install
This will create the target directory with your .jar file (e.g., my-app-0.0.1-SNAPSHOT.jar).

Step 2: Set Up AWS Elastic Beanstalk

1. Sign in to AWS Management Console: Log in to the AWS Management Console.

2. Create an Elastic Beanstalk Application:

  • Go to Elastic Beanstalk in the AWS Management Console.
  • Click on Create a new application.
  • Give your application a name (e.g., my-spring-boot-app).

3. Create an Elastic Beanstalk Environment:

  • Choose Web server environment for a Spring Boot application.
  • Select the platform as Tomcat (Elastic Beanstalk uses Tomcat to run .war files, but Spring Boot typically runs as a standalone .jar).
  • For Application Code, select Upload your code and upload the .jar file that you generated earlier.
  • Configure any necessary environment settings such as instance size, VPC, and security groups.


Step 3: Configure Elastic Beanstalk Environment

1. Elastic Beanstalk Configuration:

  • Choose the platform: Select Tomcat for Spring Boot (even if you are deploying a .jar file).
  • You can use the default configuration for most cases. You may also want to change environment variables or specify a different instance type depending on your application's requirements.

2. Configure Application Deployment:

  • Ensure the application package is a .jar file (Spring Boot uses embedded Tomcat by default, so no need for an external Tomcat server).
  • Under "Application Version", upload your .jar file.

Alternatively, you can use the Elastic Beanstalk CLI (eb cli) to create and deploy your application. Below are the steps for using the eb CLI.


Step 4: Deploy Using AWS EB CLI

1. Install AWS EB CLI: If you haven’t already, install the AWS Elastic Beanstalk CLI on your machine. Follow the instructions in the AWS documentation.

2. Initialize EB CLI: Navigate to the root directory of your Spring Boot project and run:

eb init
This will guide you through configuring your AWS Elastic Beanstalk environment.

3. Create an Environment: To create a new Elastic Beanstalk environment, run:
eb create
Choose the appropriate environment type (e.g., Web server environment), and select your preferred region.

4. Deploy the Application: Deploy your Spring Boot application using the following command:
eb deploy
This will upload your .jar file to Elastic Beanstalk, create the necessary resources, and start your application.

Step 5: Monitor and Troubleshoot

1. Access Logs: If your application doesn’t work as expected, you can retrieve logs from Elastic Beanstalk by running:

eb logs
2. Access Your Application: After successful deployment, you can access your application by navigating to the URL provided by Elastic Beanstalk (e.g., http://my-spring-boot-app.us-west-2.elasticbeanstalk.com).

3. Scaling and Configuration: You can adjust the scaling (e.g., set the number of instances) or modify other configurations using the AWS Management Console or the eb CLI.


Step 6: (Optional) Set Up a Custom Domain and HTTPS

  • Custom Domain: If you want to use a custom domain for your Spring Boot app, configure a domain in Route 53 or another DNS provider and point it to your Elastic Beanstalk environment's URL.
  • HTTPS: To enable HTTPS, you will need to set up an SSL certificate through AWS Certificate Manager (ACM) and configure the Elastic Load Balancer (ELB) to use the certificate.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete