Elastic Beanstalk CLI

Elastic Beanstalk CLI cheatsheet — initialize, create, deploy, manage environments. eb init, eb create, eb deploy, eb logs, eb ssh. Full AWS EB CLI reference.

8 min read

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 init
    

    Prompts 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-app
    

    Initializes the EB CLI for an application named my-cool-app using the Python 3.8 platform.

  • Set up SSH access to EC2 instances:

    eb ssh
    

    Connects 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_value
    

    Sets environment variables for your application. These are accessible within your application code.

  • View current environment variables:

    eb printenv
    

    Displays 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 deploy
    

    Zips 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.3
    

    Deploys 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-env
    

    Creates a new environment named my-staging-env for your application.

  • Create an environment with specific options:

    eb create my-prod-env -i t3.micro -db --elb-type application --scale 2
    

    Creates a production environment named my-prod-env using t3.micro instances, with a managed database, an Application Load Balancer, and a desired scaling capacity of 2 instances.

  • List all environments for the current application:

    eb list
    

    Shows a list of all environments associated with the application in the current directory.

  • Terminate an environment:

    eb terminate my-staging-env
    

    Deletes the environment named my-staging-env and all associated resources.

  • Terminate an environment with a specific termination policy:

    eb terminate my-old-env --force --timeout 60
    

    Forcefully terminates the environment my-old-env after a 60-minute grace period.

  • Start an environment:

    eb start
    

    Starts an environment that was previously stopped.

  • Stop an environment:

    eb stop
    

    Stops an environment, preserving its resources but incurring no running instance charges.

  • Describe an environment’s status:

    eb status
    

    Shows the current health and status of the active environment.

  • View environment URLs:

    eb open
    

    Opens the URL of the active environment in your default web browser.

  • Switch the active environment:

    eb use my-staging-env
    

    Sets my-staging-env as the active environment for subsequent eb commands.

Logs and Events

  • Tail recent logs from your environment:

    eb logs
    

    Fetches and displays the latest log files from your environment’s instances.

  • Tail logs with a specific number of lines:

    eb logs --lines 100
    

    Fetches the last 100 lines from the logs.

  • Tail logs for a specific instance:

    eb logs --instance i-0123456789abcdef0
    

    Fetches logs only from the specified EC2 instance ID.

  • View historical logs:

    eb logs --all
    

    Fetches all available historical log files.

  • View recent events:

    eb events
    

    Displays recent events and status updates for your environment.

  • View events with a specific number of events:

    eb events --count 20
    

    Displays the 20 most recent events.

  • View events from a specific time:

    eb events --since 2023-10-27T10:00:00Z
    

    Displays events that occurred since a specific ISO 8601 timestamp.

Application Versions

  • List all application versions:

    eb list-versions
    

    Shows 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.zip
    

    Uploads 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.0
    

    Removes an old application version from Elastic Beanstalk.

Configuration and Platform

  • View current environment configuration:

    eb config
    

    Opens the current environment’s configuration in your default editor. Save changes to apply them.

  • View configuration for a specific environment:

    eb config my-staging-env
    

    Opens the configuration for the my-staging-env environment.

  • View configuration as JSON:

    eb config --format json
    

    Displays the environment configuration in JSON format.

  • List available platforms:

    eb platform list
    

    Shows a list of available Elastic Beanstalk platforms.

  • Show information about a specific platform:

    eb platform info python-3.9
    

    Displays details about the Python 3.9 platform.

Health and Status

  • Check the health of the environment:
    eb health
    
    Returns the current health status of the environment (e.g., Ok, Warning, Degraded, Severe).

Miscellaneous

  • Print the EB CLI version:

    eb --version
    

    Displays the installed version of the EB CLI.

  • Show help for a command:

    eb --help
    eb deploy --help
    

    Displays 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 1
    

    Clones the production-env to create a staging-env with 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.com
    

    Conveniently sets several configuration variables in a single command.

  • Terminating an environment after a confirmation prompt:

    eb terminate my-temp-env
    

    Prompts for confirmation before proceeding with termination.

  • Force terminating an environment without prompt (use with caution):

    eb terminate my-old-env --force
    

    Immediately terminates the environment without asking for confirmation.

Gotchas

  • eb init modifies .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 deploy creates a new version by default: If you only want to apply configuration changes (like environment variables or scaling settings) without uploading new code, use eb deploy --label "config-update" or modify settings via eb 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 like ssm:StartSession.
  • eb terminate is 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 config or eb setenv might involve updating instances, load balancers, and other resources, which can take several minutes. Monitor eb events for 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 list and eb platform info are crucial here.
  • Local file zipping behavior: eb deploy zips the current directory. Ensure you are in the correct directory and that your .ebignore file (similar to .gitignore) correctly excludes unnecessary files (like node_modules, build artifacts, etc.) to keep deployment packages small and clean.
  • Default EB CLI region: If not explicitly set via eb use or command-line arguments, the EB CLI will attempt to use the default region configured in your AWS CLI (~/.aws/config).