Spring Boot with AWS Elastic Beanstalk
Learn how to deploy a Spring Boot application on AWS Elastic Beanstalk step by step with project setup, JAR build, AWS console deployment, CLI deployment, environment configuration, input/output examples, logs, monitoring, and best practices.
Introduction
In the previous article, we deployed a Spring Boot application manually on AWS EC2.
Manual EC2 deployment is useful for learning, but in real projects we usually want a managed deployment platform that can handle infrastructure, application deployment, load balancing, scaling, monitoring, and health checks.
AWS Elastic Beanstalk helps us deploy applications without manually managing all EC2 details.
Elastic Beanstalk supports Java, Spring Boot, Node.js, Python, Docker, .NET, PHP, Ruby, and Go applications.
In this article, we will deploy a Spring Boot application on AWS Elastic Beanstalk step by step.
What You Will Learn
- What is AWS Elastic Beanstalk?
- Why use Elastic Beanstalk?
- Elastic Beanstalk architecture
- How to create Spring Boot application
- How to package Spring Boot as JAR
- How to deploy using AWS Console
- How to deploy using Elastic Beanstalk CLI
- How to configure environment variables
- How to view logs
- How to monitor application health
- Input and output examples
- Common issues and fixes
- Production best practices
What is AWS Elastic Beanstalk?
AWS Elastic Beanstalk is a Platform as a Service offering from AWS.
It allows developers to upload application code, and AWS automatically handles infrastructure provisioning.
Elastic Beanstalk can manage:
- EC2 instances
- Load Balancer
- Auto Scaling Group
- Security Groups
- Application versions
- Health checks
- Logs
- Monitoring
- Environment variables
Manual EC2 vs Elastic Beanstalk
| Feature | Manual EC2 | Elastic Beanstalk |
|---|---|---|
| Server setup | Manual | Managed |
| Java installation | Manual | Platform managed |
| Deployment | SCP / SSH | Upload JAR / CLI |
| Load balancer | Manual | Optional built-in |
| Auto Scaling | Manual | Built-in |
| Monitoring | Manual setup | Built-in |
| Best for | Learning / custom control | Faster app deployment |
High Level Architecture
flowchart TD
DEV[Developer]
JAR[Spring Boot JAR]
EB[Elastic Beanstalk]
EC2[EC2 Instance]
APP[Spring Boot Application]
CW[CloudWatch Logs]
USER[User Browser]
DEV --> JAR
JAR --> EB
EB --> EC2
EC2 --> APP
APP --> CW
USER --> APP
Production Architecture
flowchart TD
U[Users]
R53[Route 53 Domain]
ALB[Application Load Balancer]
EB[Elastic Beanstalk Environment]
EC2A[EC2 Instance AZ1]
EC2B[EC2 Instance AZ2]
APP1[Spring Boot App Instance 1]
APP2[Spring Boot App Instance 2]
RDS[(Amazon RDS Database)]
CW[CloudWatch Logs]
U --> R53
R53 --> ALB
ALB --> EB
EB --> EC2A
EB --> EC2B
EC2A --> APP1
EC2B --> APP2
APP1 --> RDS
APP2 --> RDS
APP1 --> CW
APP2 --> CW
Deployment Flow
flowchart LR
A[Create Spring Boot App]
B[Build JAR]
C[Create Beanstalk App]
D[Upload JAR]
E[Deploy Environment]
F[Test URL]
A --> B
B --> C
C --> D
D --> E
E --> F
Prerequisites
Before starting, you need:
AWS Account
Java 17 or Java 21
Maven
Spring Boot project
AWS CLI
Elastic Beanstalk access
Basic AWS knowledge
Check Java:
java -version
Output:
openjdk version "17.0.x"
Check Maven:
mvn -version
Output:
Apache Maven 3.x.x
Check AWS CLI:
aws --version
Output:
aws-cli/2.x.x
Step 1: Create Spring Boot Project
Create a Spring Boot project with:
Spring Web
Spring Boot Actuator
Java 17
Maven
Project structure:
springboot-elastic-beanstalk-demo
┣ src/main/java/com/codewithvenu/beanstalk
┃ ┣ BeanstalkDemoApplication.java
┃ ┗ controller
┃ ┗ HelloController.java
┣ src/main/resources
┃ ┗ application.yml
┗ pom.xml
Step 2: Create Main Class
package com.codewithvenu.beanstalk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BeanstalkDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BeanstalkDemoApplication.class, args);
}
}
Step 3: Create REST Controller
Create file:
src/main/java/com/codewithvenu/beanstalk/controller/HelloController.java
package com.codewithvenu.beanstalk.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.Map;
@RestController
public class HelloController {
@Value("${spring.application.name}")
private String appName;
@Value("${app.environment:local}")
private String environment;
@GetMapping("/")
public Map<String, Object> home() {
return Map.of(
"message", "Spring Boot application is running on AWS Elastic Beanstalk",
"application", appName,
"environment", environment,
"timestamp", LocalDateTime.now().toString()
);
}
@GetMapping("/health")
public Map<String, String> health() {
return Map.of("status", "UP");
}
}
Step 4: Configure application.yml
Create file:
src/main/resources/application.yml
server:
port: 5000
spring:
application:
name: springboot-elastic-beanstalk-demo
app:
environment: ${APP_ENVIRONMENT:local}
management:
endpoints:
web:
exposure:
include: health,info
Important:
Elastic Beanstalk Java SE platform commonly expects the application to listen on port 5000.
Step 5: Update pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codewithvenu</groupId>
<artifactId>springboot-elastic-beanstalk-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-elastic-beanstalk-demo</name>
<description>Spring Boot deployment on AWS Elastic Beanstalk</description>
<properties>
<java.version>17</java.version>
<spring.boot.version>3.3.0</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>
Step 6: Test Locally
Run:
mvn spring-boot:run
Input:
curl http://localhost:5000/
Output:
{
"environment": "local",
"application": "springboot-elastic-beanstalk-demo",
"message": "Spring Boot application is running on AWS Elastic Beanstalk",
"timestamp": "2026-06-25T10:30:00"
}
Health check input:
curl http://localhost:5000/health
Output:
{
"status": "UP"
}
Step 7: Build JAR File
Run:
mvn clean package
Output:
BUILD SUCCESS
Generated JAR:
target/springboot-elastic-beanstalk-demo-0.0.1-SNAPSHOT.jar
Test JAR locally:
java -jar target/springboot-elastic-beanstalk-demo-0.0.1-SNAPSHOT.jar
Input:
curl http://localhost:5000/
Output:
{
"environment": "local",
"application": "springboot-elastic-beanstalk-demo",
"message": "Spring Boot application is running on AWS Elastic Beanstalk",
"timestamp": "2026-06-25T10:35:00"
}
Deployment Option 1: Deploy Using AWS Console
Step 8: Open Elastic Beanstalk
Go to:
AWS Console
→ Elastic Beanstalk
→ Create application
Step 9: Create Application
Input:
Application name: codewithvenu-springboot-eb
Environment: Web server environment
Environment name: codewithvenu-springboot-eb-dev
Domain: codewithvenu-springboot-eb-dev
Platform: Java
Platform branch: Corretto 17 running on Amazon Linux
Application code: Upload your code
Upload:
target/springboot-elastic-beanstalk-demo-0.0.1-SNAPSHOT.jar
Output:
Elastic Beanstalk environment creation started
Step 10: Wait for Environment Creation
Elastic Beanstalk will create:
EC2 Instance
Security Group
Instance Profile
Application Version
CloudWatch Logs
Elastic Beanstalk Environment URL
Output example:
Environment health: Ok
Status: Ready
Step 11: Test Elastic Beanstalk URL
Elastic Beanstalk provides URL like:
http://codewithvenu-springboot-eb-dev.us-east-1.elasticbeanstalk.com
Input:
curl http://codewithvenu-springboot-eb-dev.us-east-1.elasticbeanstalk.com/
Output:
{
"environment": "local",
"application": "springboot-elastic-beanstalk-demo",
"message": "Spring Boot application is running on AWS Elastic Beanstalk",
"timestamp": "2026-06-25T10:45:00"
}
Deployment Option 2: Deploy Using EB CLI
Step 12: Install EB CLI
On macOS:
brew install awsebcli
Verify:
eb --version
Output:
EB CLI 3.x.x
Step 13: Configure AWS CLI
Run:
aws configure
Input:
AWS Access Key ID: your-access-key
AWS Secret Access Key: your-secret-key
Default region name: us-east-1
Default output format: json
Validate:
aws sts get-caller-identity
Output:
{
"UserId": "AIDAEXAMPLE",
"Account": "123456789012",
"Arn": "arn:aws:iam::123456789012:user/codewithvenu"
}
Step 14: Initialize Elastic Beanstalk Project
From project root:
eb init
Input example:
Select a default region: us-east-1
Application name: codewithvenu-springboot-eb
Platform: Java
Platform branch: Corretto 17 running on Amazon Linux
Set up SSH: No
Output:
Application codewithvenu-springboot-eb has been created.
This creates:
.elasticbeanstalk/config.yml
Step 15: Create Environment
Run:
eb create codewithvenu-springboot-eb-dev
Output:
Creating application version archive
Uploading codewithvenu-springboot-eb/app.zip
Environment details for: codewithvenu-springboot-eb-dev
Application name: codewithvenu-springboot-eb
Environment status: Launching
Check status:
eb status
Output:
Environment details for: codewithvenu-springboot-eb-dev
Application name: codewithvenu-springboot-eb
Region: us-east-1
Deployed Version: app-xxxx
Environment ID: e-xxxx
Platform: Corretto 17 running on Amazon Linux
Tier: WebServer-Standard
CNAME: codewithvenu-springboot-eb-dev.us-east-1.elasticbeanstalk.com
Status: Ready
Health: Green
Step 16: Open Application
Run:
eb open
Or test using curl:
curl http://codewithvenu-springboot-eb-dev.us-east-1.elasticbeanstalk.com/
Output:
{
"environment": "local",
"application": "springboot-elastic-beanstalk-demo",
"message": "Spring Boot application is running on AWS Elastic Beanstalk",
"timestamp": "2026-06-25T11:00:00"
}
Step 17: Set Environment Variables
Elastic Beanstalk allows application environment variables.
Using Console:
Elastic Beanstalk
→ Environment
→ Configuration
→ Software
→ Environment properties
Add:
APP_ENVIRONMENT=dev
Using EB CLI:
eb setenv APP_ENVIRONMENT=dev
Output:
Environment update is starting.
Test again:
curl http://codewithvenu-springboot-eb-dev.us-east-1.elasticbeanstalk.com/
Output:
{
"environment": "dev",
"application": "springboot-elastic-beanstalk-demo",
"message": "Spring Boot application is running on AWS Elastic Beanstalk",
"timestamp": "2026-06-25T11:05:00"
}
Step 18: Deploy New Version
Make code change:
@GetMapping("/version")
public Map<String, String> version() {
return Map.of("version", "1.0.1");
}
Build:
mvn clean package
Deploy:
eb deploy
Output:
Creating application version archive
Uploading codewithvenu-springboot-eb/app.zip
Environment update completed successfully.
Test:
curl http://codewithvenu-springboot-eb-dev.us-east-1.elasticbeanstalk.com/version
Output:
{
"version": "1.0.1"
}
Step 19: View Logs
Using EB CLI:
eb logs
Output:
----------------------------------------
/var/log/web.stdout.log
----------------------------------------
Started BeanstalkDemoApplication
Tomcat started on port 5000
Stream logs:
eb logs --stream
Using AWS Console:
Elastic Beanstalk
→ Environment
→ Logs
→ Request logs
Step 20: Check Health
Using EB CLI:
eb health
Output:
Environment health: Green
Status: Ready
Application requests: 100% 2xx
Using Console:
Elastic Beanstalk
→ Environment
→ Health
Step 21: Configure Health Check URL
Recommended health check endpoint:
/health
In AWS Console:
Elastic Beanstalk
→ Environment
→ Configuration
→ Load balancer
→ Health check path
→ /health
Input:
Health check path: /health
Expected output:
Environment health becomes Green
Elastic Beanstalk Health Flow
flowchart TD
EB[Elastic Beanstalk]
LB[Load Balancer]
APP[Spring Boot Application]
HEALTH[Health Endpoint]
STATUS[Environment Health Green]
EB --> LB
LB --> APP
APP --> HEALTH
HEALTH --> STATUS
Step 22: Connect to Database
For production, use Amazon RDS.
Environment variables:
DB_HOST=mydb.xxxxxx.us-east-1.rds.amazonaws.com
DB_PORT=5432
DB_NAME=appdb
DB_USERNAME=appuser
DB_PASSWORD=secret
application.yml:
spring:
datasource:
url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
Recommended:
Do not hardcode DB password in application.yml.
Use environment variables or AWS Secrets Manager.
Step 23: Cleanup Resources
To avoid AWS charges, terminate environment when not needed.
Using CLI:
eb terminate codewithvenu-springboot-eb-dev
Input:
Do you want to continue? y
Output:
Environment termination completed successfully.
Using Console:
Elastic Beanstalk
→ Environment
→ Actions
→ Terminate environment
Common Errors and Fixes
Error 1: Application shows Severe health
Reason:
Application failed to start
Wrong port
Missing environment variable
JAR not executable
Fix:
eb logs
Check application logs.
Error 2: Port Issue
If app runs on port 8080, Elastic Beanstalk may not route correctly.
Fix:
server:
port: 5000
Error 3: 502 Bad Gateway
Possible reason:
Application not running
Wrong port
Startup failed
Fix:
eb logs
Check:
Tomcat started on port 5000
Error 4: Access Denied During Deployment
Reason:
IAM user does not have Elastic Beanstalk permissions
Fix:
Add required permissions:
Elastic Beanstalk
EC2
S3
CloudFormation
CloudWatch
IAM PassRole
Error 5: Environment Creation Taking Long Time
Possible reason:
EC2 instance creation
Load balancer provisioning
Health check failing
Fix:
eb events
eb logs
eb status
Best Practices
- Use Java 17 or Java 21
- Use environment variables for configuration
- Do not hardcode secrets
- Use RDS for database
- Enable CloudWatch logs
- Use health check endpoint
- Use rolling deployment
- Use separate environments for dev, test, and prod
- Enable HTTPS for production
- Use IAM roles instead of access keys
- Terminate unused environments to avoid cost
- Monitor cost with AWS Budgets
Elastic Beanstalk vs EC2
| Criteria | EC2 | Elastic Beanstalk |
|---|---|---|
| Control | High | Medium |
| Setup effort | High | Low |
| Scaling | Manual | Built-in |
| Deployment | Manual | Managed |
| Logs | Manual setup | Available |
| Best for | Custom infrastructure | Fast app deployment |
Interview Questions
What is Elastic Beanstalk?
Elastic Beanstalk is an AWS managed service that deploys and manages applications by provisioning resources like EC2, load balancer, auto scaling, health checks, and monitoring.
Is Elastic Beanstalk serverless?
No. Elastic Beanstalk is not serverless. It provisions EC2 and other AWS resources behind the scenes.
What port should Spring Boot use for Elastic Beanstalk?
For Java SE platform, Spring Boot commonly runs on port 5000.
How do you pass environment variables?
You can pass environment variables through Elastic Beanstalk Console or EB CLI using:
eb setenv KEY=value
How do you view logs?
Use:
eb logs
or check logs from AWS Console.
How do you deploy a new version?
Build new JAR and run:
eb deploy
Summary
In this article, we deployed a Spring Boot application on AWS Elastic Beanstalk.
We covered:
- Spring Boot project setup
- REST API creation
- Local testing
- JAR packaging
- AWS Console deployment
- EB CLI deployment
- Environment variables
- Health checks
- Logs and monitoring
- Common issues
- Production best practices
Elastic Beanstalk is a good next step after learning manual EC2 deployment. It gives developers a simple way to deploy applications while AWS manages much of the infrastructure.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...