AWS Builder Center
Day 36: AWS Elastic Beanstalk – Deploying a Java Application Without Managing the Infrastructure

Day 36: AWS Elastic Beanstalk – Deploying a Java Application Without Managing the Infrastructure

Day 36 of my 90-day AWS challenge: how AWS Elastic Beanstalk takes an uploaded application and sets up the servers, load balancer, scaling, and monitoring around it, the deployment policies it offers, and how it compares to EC2, ECS, and Lambda.

Starting week six

The first five weeks covered building blocks, and I've been wiring them together by hand: EC2 instances, load balancers, Auto Scaling, security groups, and monitoring. Week six is about hosting applications with less setup. The first service is AWS Elastic Beanstalk.

What is Elastic Beanstalk?

Elastic Beanstalk is a platform-as-a-service (PaaS) for deploying web applications. I upload my code, such as a JAR file for a Java application, and Beanstalk provisions and manages the infrastructure around it:
  • EC2 instances (Day 3)
  • An Application Load Balancer and Auto Scaling group (Day 10)
  • Security groups and networking in a VPC (Day 5)
  • CloudWatch logs, metrics, and health reporting (Day 9)
  • An S3 bucket for storing application versions (Day 4)
The resources are created with CloudFormation behind the scenes (Day 16), and they stay in my account, so I can still see and change them. There is no extra charge for Beanstalk itself: I pay for the resources it creates.

Supported platforms

Java (Corretto) with Java SE or Tomcat, Node.js, Python, PHP, Ruby, .NET, Go, and Docker.

Key concepts

Concept
What it is
Application
A logical container for my project
Application version
A specific uploaded build of the code, stored in S3
Environment
A running deployment of one application version, with all of its resources
Environment tier
Web server (handles HTTP requests) or Worker (processes jobs from an SQS queue)
Environment type
Single instance (cheap, for development) or load balanced (for production)
Platform
The operating system and runtime, such as Corretto on Amazon Linux
Configuration
Settings such as instance type, scaling limits, and environment variables

Deploying a Java Spring Boot application

For the Java SE platform:
  1. Build the project into a runnable JAR, for example with  mvn clean package 
  1. Make sure the application listens on port 5000, which is the port that the platform's web server forwards requests to by default. In Spring Boot that means  server.port=5000  or an environment variable
  1. Optionally add a  Procfile  to say how to start it:
web: java -jar application.jar
  1. Upload and deploy through the console, or use the EB CLI:
eb init
eb create my-env --single
eb deploy
eb status
eb open
eb logs
eb terminate my-env
 eb init  asks which application, platform, and Region to use, and  --single  creates a low-cost single-instance environment.

Deployment policies

Policy
How it works
Trade-off
All at once
Updates every instance together
Fastest, but has downtime
Rolling
Updates in batches
No full downtime, but capacity drops during the update
Rolling with additional batch
Adds a new batch first
Keeps full capacity, costs a little more
Immutable
Launches new instances with the new version, then removes the old ones
Safest rollback, slower
Traffic splitting
Sends a share of traffic to the new version first
Canary-style testing
Blue/green
Runs a second environment and swaps the URLs
Near-zero downtime, with the cost of two environments

Configuration and customization

  • Environment properties hold settings and non-secret values; secrets belong in Secrets Manager (Day 19)
  • Files in a  .ebextensions  folder, or platform hooks, customize the environment
  • Managed platform updates apply OS and runtime patches in a maintenance window
  • Enhanced health reporting shows the status of each instance and the application

Databases and Beanstalk

Beanstalk can create an RDS database inside the environment, but then the database is deleted when the environment is. For anything important, create the RDS instance separately (Day 6) and connect to it.

Beanstalk vs. other options

Option
Control
Effort
Best for
EC2
Full
High
Custom setups
Elastic Beanstalk
Medium
Low
Standard web apps that need a managed environment
ECS or Fargate (Day 17)
Medium to high
Medium
Containerized apps and microservices
Lambda (Day 8)
Low
Low
Event-driven code

Security

  • Beanstalk uses a service role and an instance profile, which are IAM roles that should have only the permissions they need (Day 2)
  • Restrict the security group, and prefer HTTPS with a certificate from AWS Certificate Manager on a load-balanced environment
  • Keep instances in private subnets where possible, with the load balancer in public subnets (Day 5)

What I practiced today

  1. Built a small Java application into a JAR file
  1. Created a Beanstalk application and a single-instance environment on the Java platform
  1. Uploaded and deployed the JAR, and opened the environment URL
  1. Checked the health status and the logs
  1. Deployed a second version and watched the update
  1. Terminated the environment, then deleted old application versions and the S3 bucket Beanstalk created

Key takeaways

  • Beanstalk is a good fit when I want my application running without assembling the infrastructure myself
  • The resources are still mine, so a load-balanced environment includes a load balancer that bills while it exists
  • Pick the deployment policy based on how much downtime and risk I can accept
  • Terminate practice environments, because the instances and any load balancer keep billing

Up next: Day 37

AWS App Runner: running containers and source code as a web service.
Learning AWS too? Follow along and share what you're building. #90DaysOfAWS
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