Configuring Elastic Beanstalk Applications and Environments
This lesson explains how AWS Elastic Beanstalk provisions the EC2 instances, load balancer, Auto Scaling group and monitoring for a web application from its source bundle, while leaving those resources visible and configurable.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how applications, application versions, environments and saved configurations are related.
- Distinguish the web server and worker environment tiers.
- Explain how .ebextensions files, platform hooks and environment properties configure an environment.
- Identify the tools used to operate environments, such as the EB CLI and enhanced health reporting.
Applications, versions and environments
Elastic Beanstalk organises deployments using four concepts. An application is the top-level container, usually one per software product, such as orders. Each time code is uploaded, it becomes an application version: a labelled source bundle, such as a .zip file, stored in S3. Versions are kept so that any of them can be deployed again; an application version lifecycle policy deletes old ones, because the number of versions per account is limited.
An environment is a running copy of one version, with its own EC2 instances, load balancer and Auto Scaling group and its own URL, such as orders-prod.eu-west-1.elasticbeanstalk.com. One application typically has several environments, such as test and production, running different versions. Each environment uses a platform, which combines an operating system and a runtime, such as Python on Amazon Linux 2023, or Docker for applications packaged as containers. The following table summarises the concepts.
| Concept | Meaning |
|---|---|
| Application | A logical container for versions and environments |
| Application version | A labelled source bundle stored in S3, such as v1.4.0 |
| Environment | A running copy of one version on a platform, such as orders-prod |
| Platform | The runtime and operating system, such as Python 3.12 on Amazon Linux 2023, or Docker |
Elastic Beanstalk itself is free; you pay for the resources it creates.
Elastic Beanstalk building blocks
Environment tiers
Each Elastic Beanstalk environment belongs to one of two tiers, chosen when it is created.
A web server environment runs an application that answers HTTP requests. Elastic Beanstalk normally places the instances in an Auto Scaling group behind an Application Load Balancer; a single-instance environment without a load balancer is also possible for development.
A worker environment runs background jobs. Elastic Beanstalk creates or uses an SQS queue, and a daemon on each instance reads messages from it and sends each one as an HTTP POST request to a local path in the application. If the application responds with 200 OK, the daemon deletes the message; otherwise the message becomes visible again after the visibility timeout and is retried, and repeated failures go to a dead-letter queue. A cron.yaml file defines periodic tasks, which the daemon triggers on a schedule. The following table summarises the two tiers.
| Environment tier | Purpose |
|---|---|
| Web server | Serves HTTP traffic, usually behind an Application Load Balancer |
| Worker | Processes messages from an SQS queue through a local daemon that POSTs each message to the application; can run periodic tasks defined in cron.yaml |
A web environment can hand long-running tasks to a worker environment through the queue, keeping web responses fast.
Configuring an environment
An Elastic Beanstalk environment is configured through files in the source bundle. The following list describes the available mechanisms.
.ebextensions/*.configfiles in the source bundle set option settings, install packages and run commands.container_commandsrun after the application is extracted and before it is deployed; theleader_onlyflag runs a command on one instance only, for one-off tasks such as database migrations.- Platforms based on Amazon Linux 2 and later also support
.platform/hooksscripts and.platform/nginxconfiguration. - Environment properties become environment variables for the application, the usual way to pass database endpoints and settings.
- Settings applied directly to the environment, through the console, CLI or API, override those in configuration files.
# .ebextensions/options.config
option_settings:
aws:elasticbeanstalk:application:environment:
LOG_LEVEL: info
aws:autoscaling:asg:
MinSize: 2
MaxSize: 6
container_commands:
01_migrate:
command: "python manage.py migrate --noinput"
leader_only: trueConfigure the environment
Operations
Elastic Beanstalk provides tools for operating environments. The following list describes them.
- The EB CLI manages environments from a terminal:
eb init,eb create,eb deploy,eb logsandeb health. - Enhanced health reporting explains instance and request problems, such as high latency or 5xx responses.
- Managed platform updates apply patch and minor platform updates during a weekly maintenance window, using an immutable deployment.
- Saved configurations and environment cloning reproduce an environment's settings.
A migration that ran on every instance
A Django application on Elastic Beanstalk runs its database migration in a commands block of an .ebextensions file. With four instances, the migration runs four times at once and fails with lock errors.
| Problem | Fix |
|---|---|
commands run on every instance before the application is extracted | Use container_commands, which run after extraction |
| The migration runs in parallel on all instances | Add leader_only: true so one instance runs it |
| The database password is in the config file | Read it from Secrets Manager; pass only the secret's name as an environment property |
Each deployment now runs the migration exactly once, before the new version receives traffic, and no credentials are stored in the source bundle.
A web application must process long-running jobs from a queue without slowing web requests. Which environment tier handles the jobs?
Show answer
A worker environment. Its daemon reads messages from an SQS queue and posts them to the application, while a web server environment serves the HTTP requests.
Exam Tips
Remember for the exam
- To run a database migration once per deployment, use
container_commandswithleader_only. - Background jobs from an SQS queue and scheduled tasks point to a worker environment with
cron.yaml. - Pass configuration to the application through environment properties.
- To keep the platform patched automatically, enable managed platform updates.
- Settings applied directly to an environment override
.ebextensionsfiles.