SkillSprout
Chapter 2 · Lesson 4
Advanced
6 min read

Elastic Beanstalk, DNS Traffic Shifting and Rollbacks

Overview

AWS Elastic Beanstalk deploys web applications (Java, .NET, Node.js, Python, PHP, Ruby, Go, Docker) and handles provisioning of EC2, Auto Scaling, load balancing and monitoring. You upload a source bundle; Beanstalk deploys it using a deployment policy you choose.

Beanstalk is like renting a furnished apartment: you bring your belongings (the code) and the landlord (Beanstalk) maintains the plumbing. The deployment policy decides whether you move in room by room, build a second apartment next door, or swap the entire address in one step.

This lesson also covers platform-agnostic traffic shifting: Route 53 weighted records, ALB weighted target groups and CloudFront for blue/green, and a rollback decision framework that compares all strategies.

Key Concepts

  • Beanstalk deployment policies: All at once, Rolling, Rolling with additional batch, Immutable, Traffic splitting.
  • Immutable launches a temporary ASG with new instances, and terminates it on failure, giving the fastest, safest rollback of in-place policies.
  • Traffic splitting is a canary: a percentage of traffic goes to new instances for an evaluation time, with automatic rollback on unhealthy results.
  • Blue/green in Beanstalk is a separate environment plus Swap Environment URLs (CNAME swap); the database should live outside the environment.
  • .ebextensions (YAML or JSON in the source bundle) and platform hooks customise instances and configuration.
  • Managed platform updates and enhanced health reporting keep environments patched and observable.
  • Route 53 weighted records shift DNS traffic by weight, but clients cache DNS so shifts are not instant and rollbacks lag by the TTL.
  • ALB weighted target groups shift traffic instantly without DNS caching concerns.

Detailed Notes

Beanstalk deployment policies

PolicyCapacity during deployDowntimeRollbackExtra cost
All at onceReduced (all updated together)BriefManual redeployNone
RollingReduced by batch sizeNoneManual redeployNone
Rolling with additional batchFull (extra batch launched first)NoneManual redeploySmall, temporary
ImmutableFull (new ASG in parallel)NoneTerminate new ASGDouble instances temporarily
Traffic splittingFull, canary percentageNoneAutomatic on failureDouble instances temporarily
Blue/green (CNAME swap)Two environmentsNoneSwap backDouble environment

Configure the policy in .ebextensions:

yaml
option_settings:
  aws:elasticbeanstalk:command:
    DeploymentPolicy: Immutable
    Timeout: "900"
  aws:elasticbeanstalk:healthreporting:system:
    SystemType: enhanced
  aws:autoscaling:updatepolicy:rolling:
    RollingUpdateEnabled: true
bash
eb deploy --label v42
eb swap orders-blue --destination_name orders-green

Database considerations

If an RDS instance is created inside a Beanstalk environment it is deleted with the environment and cannot be swapped. For production, create the database outside and pass the connection string by environment property or Secrets Manager.

Blue/green by CNAME swap vs DNS weights

TechniqueGranularitySpeed of switchRollback
Beanstalk URL swap100% flipSeconds (DNS CNAME swap, TTL applies for clients)Swap back
Route 53 weightedAny percentage per recordLimited by TTL and resolver cachingChange weight, slow
ALB weighted target groupsAny percentageImmediateChange weight
CloudFront continuous deploymentPercent or header-basedFastPromote or delete staging

Route 53 weighted example:

json
{
  "Changes": [{
    "Action": "UPSERT",
    "ResourceRecordSet": {
      "Name": "app.example.com", "Type": "CNAME", "TTL": 60,
      "SetIdentifier": "green", "Weight": 10,
      "ResourceRecords": [{ "Value": "green-env.us-east-1.elasticbeanstalk.com" }]
    }
  }]
}

Add a health check to each weighted record so unhealthy endpoints stop receiving traffic. Weights of 0 stop traffic but keep the record for fast re-enable.

ALB weighted forwarding

bash
aws elbv2 modify-listener --listener-arn $LISTENER \
  --default-actions '[{"Type":"forward","ForwardConfig":{"TargetGroups":[
    {"TargetGroupArn":"'$BLUE'","Weight":90},
    {"TargetGroupArn":"'$GREEN'","Weight":10}]}}]'

Rollback decision framework

RequirementPick
Instant rollback, no capacity lossBlue/green with old environment retained
Gradual exposure with metric-based abortCanary/linear (CodeDeploy, Beanstalk traffic splitting)
Cheapest, can tolerate brief errorsRolling
Schema changes involvedBackward-compatible migrations (expand then contract) so both versions work

Stateful and database changes

Use expand/contract migrations: add new columns first (compatible with old code), deploy new code, backfill, then remove old columns in a later release. This makes any rollback strategy safe.

Beanstalk operations

Managed platform updates apply in a maintenance window, with instance replacement or in-place patching. Use saved configurations and environment cloning for blue/green copies. Beanstalk emits events to SNS and EventBridge; configure CloudWatch alarms on the environment health metric.

Customisation and operations

Beanstalk customises instances with .ebextensions (YAML or JSON config files: packages, files, commands, container commands) and, on Amazon Linux 2 and later platforms, .platform hooks (prebuild, predeploy, postdeploy) and Procfile/Buildfile. Use saved configurations and environment manifests to recreate environments consistently, and enhanced health reporting (healthd) for per-instance and request metrics that drive alarms and rolling-deployment health checks.

Keep state out of the environment: decouple the RDS database (an RDS instance inside the environment is deleted with it), store sessions in ElastiCache or DynamoDB, and use S3 for uploads. A worker tier consumes an SQS queue through the daemon. For blue/green on Beanstalk, clone the environment, test it and use Swap Environment URLs; at larger scale, weighted Route 53 records or an ALB-level traffic split across two environments allow canary-style shifting.

Beanstalk managed updates apply platform patches in a maintenance window; choose immutable updates for production so a failed update leaves the original instances untouched. For rollbacks, redeploy a previous application version (stored in S3, subject to a retention limit) or swap back to the old environment.

Real World Example

Scenario: an e-learning provider runs a Beanstalk Java app. They used All-at-once and suffered outages whenever a release crashed on boot. They switch production to Immutable deployments and keep the RDS database outside the environment.

For major upgrades they clone the environment (green), run regression tests on its URL, then lower Route 53 TTL to 60 seconds, send 10% then 50% via weighted records and finally swap. The old environment stays for a day, giving them an immediate path back.

Architecture Diagram

Blue/green with weighted DNS
Architecture
AWS Cloud
Users
Route 53weighted records
Region
Blue environmentv1
Green environmentv2
Shared RDSoutside environments

Route 53 weights move users gradually between two Beanstalk environments.

  1. 1Clone the blue environment into green and deploy the new version.
  2. 2Run regression tests directly against the green environment URL.
  3. 3Add weighted Route 53 records with health checks and a short TTL.
  4. 4Increase green weight in steps while watching alarms.
  5. 5Keep blue for rollback, then decommission it.

Best Practices

  • ✓Use Immutable or Traffic splitting for production Beanstalk environments.
  • ✓Keep databases outside Beanstalk environments that you swap or delete.
  • ✓Lower TTL before DNS-based cutovers and attach health checks to weighted records.
  • ✓Use expand/contract database migrations so versions can run side by side.
  • ✓Prefer ALB weighted target groups when you need immediate, precise shifts.

Common Interview Questions

Exam Tips

DOP-C02 focus

  • "Safest Beanstalk in-place policy" = Immutable; "canary on Beanstalk" = Traffic splitting.
  • "Swap environments" = Swap Environment URLs; database must be external.
  • Instant percentage shift = ALB weighted target groups; DNS weights are subject to TTL.
  • Weighted records need health checks to stop sending to failed endpoints.

Finished this lesson?

0 of 44 lessons done · 0% of the course