What it is
The Elastic Beanstalk Command Line Interface (EB CLI) is a tool for deploying and managing applications on AWS Elastic Beanstalk from your terminal.
Installation
Linux (Debian/Ubuntu):
sudo apt update
sudo apt install python3-pip
pip3 install awsebcli --upgrade
Linux (Fedora/CentOS/RHEL):
sudo yum install python3-pip
pip3 install awsebcli --upgrade
macOS:
brew install awsebcli
Or, if you don’t have Homebrew:
pip3 install awsebcli --upgrade
Windows:
pip install awsebcli --upgrade
Ensure Python 3 and pip are installed and in your PATH.
Core Concepts
- Application: A logical collection of Elastic Beanstalk resources (environments, versions, configurations).
- Environment: A specific running instance of your application. An application can have multiple environments (e.g., development, staging, production).
- Version: A specific snapshot of your application’s code and configuration. Each time you deploy, a new version is created.
- Configuration: Settings that define how your environment runs (e.g., instance type, scaling, database settings).
Commands / Usage
Initialization and Setup
-
Initialize EB CLI in a project directory:
eb initPrompts you to select a region, an application name (or create a new one), and platform/language for your application.
-
Configure EB CLI for a specific environment:
eb init -p python-3.8 my-cool-appInitializes the EB CLI for an application named
my-cool-appusing the Python 3.8 platform. -
Set up SSH access to EC2 instances:
eb sshConnects to your environment’s primary EC2 instance. Requires an SSH key pair to be configured in your environment.
-
Configure environment settings:
eb setenv VAR_NAME=value ANOTHER_VAR=another_valueSets environment variables for your application. These are accessible within your application code.
-
View current environment variables:
eb printenvDisplays the environment variables currently configured for your application.
-
Unset environment variables:
eb setenv VAR_NAME=Removes an environment variable.
Deployment
-
Deploy the current directory to your environment:
eb deployZips the contents of the current directory and uploads it as a new application version, then deploys it to the active environment.
-
Deploy a specific version label:
eb deploy --version-label v1.2.3Deploys a previously uploaded application version with the specified label.
-
Deploy with a custom message:
eb deploy --message "Fixed critical bug in checkout"Deploys the application and associates a descriptive message with the new version.
-
Deploy without creating a new version (useful for configuration changes):
eb deploy --no-verify-ssl --timeout 30 --label "config-update-$(date +%s)"Deploys the application, skipping SSL verification, setting a timeout of 30 minutes, and assigning a unique label based on the current timestamp.
Environment Management
-
Create a new environment:
eb create my-staging-envCreates a new environment named
my-staging-envfor your application. -
Create an environment with specific options:
eb create my-prod-env -i t3.micro -db --elb-type application --scale 2Creates a production environment named
my-prod-envusingt3.microinstances, with a managed database, an Application Load Balancer, and a desired scaling capacity of 2 instances. -
List all environments for the current application:
eb listShows a list of all environments associated with the application in the current directory.
-
Terminate an environment:
eb terminate my-staging-envDeletes the environment named
my-staging-envand all associated resources. -
Terminate an environment with a specific termination policy:
eb terminate my-old-env --force --timeout 60Forcefully terminates the environment
my-old-envafter a 60-minute grace period. -
Start an environment:
eb startStarts an environment that was previously stopped.
-
Stop an environment:
eb stopStops an environment, preserving its resources but incurring no running instance charges.
-
Describe an environment’s status:
eb statusShows the current health and status of the active environment.
-
View environment URLs:
eb openOpens the URL of the active environment in your default web browser.
-
Switch the active environment:
eb use my-staging-envSets
my-staging-envas the active environment for subsequentebcommands.
Logs and Events
-
Tail recent logs from your environment:
eb logsFetches and displays the latest log files from your environment’s instances.
-
Tail logs with a specific number of lines:
eb logs --lines 100Fetches the last 100 lines from the logs.
-
Tail logs for a specific instance:
eb logs --instance i-0123456789abcdef0Fetches logs only from the specified EC2 instance ID.
-
View historical logs:
eb logs --allFetches all available historical log files.
-
View recent events:
eb eventsDisplays recent events and status updates for your environment.
-
View events with a specific number of events:
eb events --count 20Displays the 20 most recent events.
-
View events from a specific time:
eb events --since 2023-10-27T10:00:00ZDisplays events that occurred since a specific ISO 8601 timestamp.
Application Versions
-
List all application versions:
eb list-versionsShows all uploaded application versions for the current application.
-
Upload a specific directory as an application version:
eb create-version --version-label v1.0.1 --source my-app-v1.0.1.zipUploads a specific zip file as a new application version with the label
v1.0.1. -
Delete an application version:
eb delete-version --version-label v1.0.0Removes an old application version from Elastic Beanstalk.
Configuration and Platform
-
View current environment configuration:
eb configOpens the current environment’s configuration in your default editor. Save changes to apply them.
-
View configuration for a specific environment:
eb config my-staging-envOpens the configuration for the
my-staging-envenvironment. -
View configuration as JSON:
eb config --format jsonDisplays the environment configuration in JSON format.
-
List available platforms:
eb platform listShows a list of available Elastic Beanstalk platforms.
-
Show information about a specific platform:
eb platform info python-3.9Displays details about the Python 3.9 platform.
Health and Status
- Check the health of the environment:
Returns the current health status of the environment (e.g., Ok, Warning, Degraded, Severe).eb health
Miscellaneous
-
Print the EB CLI version:
eb --versionDisplays the installed version of the EB CLI.
-
Show help for a command:
eb --help eb deploy --helpDisplays general help or help for a specific subcommand.
Common Patterns
-
Deploying a new version with a timestamped label:
eb deploy --version-label "deploy-$(date +%Y%m%d%H%M%S)"Ensures unique version labels for each deployment, useful for tracking and rollbacks.
-
Setting up a new staging environment from a production configuration:
eb use production-env eb clone staging-env --instance_type t2.medium --scale 1Clones the
production-envto create astaging-envwith specified instance type and scale. -
Automating deployments in CI/CD pipelines:
# Assuming EB_APP_NAME, EB_ENV_NAME, and AWS credentials are set git archive --format=zip -o app.zip HEAD eb deploy --message "CI build ${GIT_COMMIT}"Creates a zip archive of the current Git HEAD and deploys it with a commit message.
-
Viewing logs and immediately deploying if issues arise:
eb logs --lines 50 # Review logs... if okay: eb deploy --message "Deployment after log review"A manual workflow for checking logs before confirming a deployment.
-
Setting multiple environment variables at once:
eb setenv DATABASE_URL=postgres://user:pass@host:port/db API_KEY=abcdef12345 REDIS_HOST=redis.example.comConveniently sets several configuration variables in a single command.
-
Terminating an environment after a confirmation prompt:
eb terminate my-temp-envPrompts for confirmation before proceeding with termination.
-
Force terminating an environment without prompt (use with caution):
eb terminate my-old-env --forceImmediately terminates the environment without asking for confirmation.
Gotchas
eb initmodifies.elasticbeanstalk/config.yml: This file stores your application and environment configurations. Be mindful when committing it to version control, especially if it contains sensitive information (though credentials should not be stored here).eb deploycreates a new version by default: If you only want to apply configuration changes (like environment variables or scaling settings) without uploading new code, useeb deploy --label "config-update"or modify settings viaeb config.- SSH keys are managed by AWS: When using
eb ssh, the EB CLI uses AWS Systems Manager Session Manager or EC2 Instance Connect, which requires appropriate IAM permissions and security group rules. Ensure your IAM user/role has permissions likessm:StartSession. eb terminateis permanent: Terminating an environment deletes all associated resources (EC2 instances, load balancers, etc.) and cannot be undone. Always double-check the environment name.- Configuration changes can take time: Applying changes via
eb configoreb setenvmight involve updating instances, load balancers, and other resources, which can take several minutes. Monitoreb eventsfor progress. - Platform updates require new deployments: If you need to update the underlying platform (e.g., from Python 3.8 to 3.9), you typically need to create a new environment or deploy a new application version targeting the updated platform.
eb platform listandeb platform infoare crucial here. - Local file zipping behavior:
eb deployzips the current directory. Ensure you are in the correct directory and that your.ebignorefile (similar to.gitignore) correctly excludes unnecessary files (likenode_modules, build artifacts, etc.) to keep deployment packages small and clean. - Default EB CLI region: If not explicitly set via
eb useor command-line arguments, the EB CLI will attempt to use the default region configured in your AWS CLI (~/.aws/config).