AWS Elastic Beanstalk
Core concept: Beanstalk is a PaaS โ you upload your code (WAR/JAR), and AWS handles EC2, Auto Scaling, Load Balancer, and RDS provisioning.
What Beanstalk Managesโ
You provide: Application code (JAR/WAR/ZIP)
Beanstalk provides:
โโโ EC2 instances (with Java runtime)
โโโ Auto Scaling Group
โโโ Load Balancer (ALB or CLB)
โโโ RDS (optional, not recommended for prod)
โโโ CloudWatch monitoring
โโโ Deployment automation
Every Beanstalk environment is backed by a CloudFormation stack.
Environment Tiersโ
| Tier | Use Case | Infrastructure |
|---|---|---|
| Web Server | Handle HTTP requests | ELB + EC2 + Auto Scaling |
| Worker | Process background tasks from SQS | EC2 + SQS (no ELB) |
Supported Java Platformsโ
- Corretto 17 / 21 (Amazon-managed Java)
- Tomcat (deploy WAR files)
- Docker (any JVM version)
- Multi-container Docker (ECS under the hood)
Deployment Policiesโ
1. All at Once (Default)โ
[v1][v1][v1] โ [v2][v2][v2]
- Fastest, but causes downtime
- Good for dev/test only
2. Rollingโ
[v1][v1][v1][v1]
โ [v2][v2][v1][v1] โ batch 1 updating
โ [v2][v2][v2][v2] โ batch 2 updating
- No downtime, reduced capacity during deployment
- If deploy fails: complex partial state to fix
3. Rolling with Additional Batchโ
[v1][v1][v1][v1]
โ [v1][v1][v1][v1][v2][v2] โ add batch first
โ [v2][v2][v1][v1][v2][v2]
โ [v2][v2][v2][v2] โ remove extra batch
- Full capacity maintained throughout
- Higher cost temporarily (extra instances)
4. Immutableโ
New ASG with v2 instances (all pass health check)
โ Swap into main ASG
โ Terminate old instances
- Zero downtime, fastest rollback (just terminate new ASG)
- Most expensive โ doubles instance count temporarily
5. Blue/Green (Traffic Splitting)โ
Blue env (v1): 100% traffic
โ
Green env (v2): deployed, tested
โ
Swap CNAMEs (or weighted routing)
โ
Green: 100% traffic | Blue: kept for rollback
- Complete environment swap
- Rollback = swap CNAME back
- Not a Beanstalk native feature โ done via Route 53 or swap URL
Deployment Policy Comparisonโ
| Policy | Downtime | Rollback Speed | Cost | Capacity |
|---|---|---|---|---|
| All at once | โ Yes | Re-deploy | Lowest | Full โ Zero |
| Rolling | โ | Re-deploy | Normal | Reduced |
| Rolling + batch | โ | Re-deploy | Higher | Full |
| Immutable | โ | Fast (terminate) | Highest | Full โ 2x |
| Blue/Green | โ | Instant (CNAME) | 2x | Full |
.ebextensionsโ
Customize the environment with config files in .ebextensions/ directory:
# .ebextensions/jvm.config
option_settings:
aws:elasticbeanstalk:container:java:jvmoptions:
JVM Options: "-Xms512m -Xmx1024m -XX:+UseG1GC"
# .ebextensions/env.config
option_settings:
aws:elasticbeanstalk:application:environment:
SPRING_PROFILES_ACTIVE: prod
DB_URL: "jdbc:postgresql://mydb.rds.amazonaws.com:5432/orders"
# .ebextensions/cloudwatch.config โ install CW agent
packages:
yum:
amazon-cloudwatch-agent: []
commands:
01_start_cwagent:
command: "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a start"
Procfile (Java)โ
# Procfile โ at root of deployment ZIP
web: java -jar target/myapp.jar --server.port=5000
Spring Boot Deploymentโ
# Build JAR
mvn clean package
# Deploy via CLI
eb init my-app --region us-east-1 --platform corretto-17
eb create prod-env
eb deploy
# Or deploy JAR directly
eb deploy --staged
๐งช Practice Questionsโ
Q1. A developer needs to deploy a new version to Elastic Beanstalk with no downtime and the ability to instantly roll back if issues are found. Which deployment policy is BEST?
A) Rolling
B) All at Once
C) Rolling with Additional Batch
D) Immutable
โ Answer & Explanation
D โ Immutable deploys to a fresh set of instances. If anything is wrong, just terminate the new Auto Scaling Group โ the old instances keep running. It's the fastest rollback of any in-place deployment strategy.
(Blue/Green is also valid but is more complex to set up.)
Q2. A Beanstalk deployment must maintain full capacity throughout the deployment (no reduced capacity). The cost of extra instances during deployment is acceptable. Which policy?
A) Rolling
B) All at Once
C) Rolling with Additional Batch
D) Immutable
โ Answer & Explanation
C โ Rolling with Additional Batch adds extra instances first (maintaining full capacity), updates old batches, then removes the extras. Full capacity throughout, at a slightly higher temporary cost.
Q3. Where should Elastic Beanstalk environment configuration (JVM options, environment variables) be placed in the application package?
A) application.properties
B) beanstalk.yml
C) .ebextensions/*.config YAML files
D) Lambda environment variables
โ Answer & Explanation
C โ Beanstalk reads configuration from YAML/JSON files in the .ebextensions/ directory in your deployment package. These configure option settings, run commands, install packages, etc.