AWS Elastic Beanstalk (EB) — Practical Guide for Deploying Python APIs (FastAPI included)

From Qiki
Jump to navigation Jump to search

AWS Elastic Beanstalk (EB) — Practical Guide for Deploying Python APIs (FastAPI included)

What Elastic Beanstalk is

AWS Elastic Beanstalk is a managed service that helps you deploy, run, and scale applications on AWS by handling a lot of the “glue work” (provisioning, configuration, health monitoring, deployments) while still letting you customize underlying AWS resources when needed. (AWS Documentation)

Typical workflow: create an application, upload an application version (your code as a source bundle), and Elastic Beanstalk launches an environment with the AWS resources required to run it. (AWS Documentation)



Core concepts (EB vocabulary)

  • Application: A logical container for EB components (environments + versions).
  • Environment: The running infrastructure for one version of your app (e.g., dev, staging, prod). (AWS Documentation)
  • Application version: A specific uploaded build of your code; EB creates one whenever you upload/deploy source code. (AWS Documentation)
  • Platform: The runtime stack (e.g., Python on Amazon Linux) that EB provisions/maintains. (AWS Documentation)



Environment types (how your app runs)

Elastic Beanstalk lets you choose an environment style based on your needs:

  • Single-instance: simplest, lowest cost for dev/test.
  • Load-balanced, scalable: uses a load balancer + Auto Scaling for production workloads. (AWS Documentation)



Pricing (what you pay for)

Elastic Beanstalk has no additional service charge—you pay for the AWS resources it provisions (EC2, load balancer, S3, etc.). (Amazon Web Services, Inc.)



How deployments work in EB

Source bundle (your deployable artifact)

When deploying via the console (and often in automation), you upload your app as a source bundle (typically a .zip) with a specific structure and requirements described in the EB docs. (AWS Documentation)

Deployment policies

EB supports different deployment behaviors (rolling, etc.), and each deployment gets a deployment ID (useful for troubleshooting when updates fail). (AWS Documentation)

Blue/green deployments (near-zero downtime)

A common safe approach is:

  1. clone or create a new environment (green),
  2. deploy the new version there,
  3. test it,
  4. swap the environment URLs/CNAMEs to shift traffic quickly. (AWS Documentation)



Installing and using the EB CLI (recommended for developers)

The EB CLI provides interactive commands to create/update/monitor EB environments from your terminal. (AWS Documentation)

Useful docs:



Configuration & customization (the EB way)

1) Environment variables (Environment properties)

You can define environment properties (environment variables) to pass config like database URLs, API keys, debug flags, etc. (AWS Documentation) You can set them in the console under your environment’s configuration screens. (AWS Documentation)

2) .ebextensions configuration files (advanced customization)

You can place YAML/JSON .config files under a folder named .ebextensions/ to customize your environment and even underlying AWS resources. (AWS Documentation)

You can also use option_settings in these files to modify EB configuration and define variables that become environment variables for your app. (AWS Documentation)

3) Procfile (Python web process command)

On the EB Python platform, you can use a Procfile to configure the command that starts your web server (the EB docs describe this for WSGI servers). The default listening port is 8000. (AWS Documentation)

FastAPI is ASGI, not WSGI. A common pattern is to run Gunicorn with an ASGI worker (Uvicorn worker) from a Procfile—this usually works well on EB’s Python platform because EB is really just starting the process you specify.

4) Managed platform updates (keep runtimes patched)

EB regularly releases platform updates (fixes, security updates, features). You can enable managed platform updates to upgrade automatically during a scheduled maintenance window. (AWS Documentation)



Observability: health + logs

Enhanced health

Enhanced health reporting gives a better picture of environment health and helps identify issues that could cause unavailability. In many newer environments it’s enabled by default, and newer platform versions include a health agent. (AWS Documentation)

Logs

You can retrieve logs via the console or EB CLI, and you can also publish logs to S3 or stream to CloudWatch Logs. (AWS Documentation)



Step-by-step example: Deploy a FastAPI app to Elastic Beanstalk (Python platform + Procfile)

This walkthrough uses:

  • FastAPI for the API
  • Gunicorn + Uvicorn worker to serve ASGI
  • EB CLI to create and deploy the environment

0) Prerequisites

  • AWS account and credentials configured locally (via standard AWS auth methods)
  • Python 3.x locally
  • EB CLI installed and working (EB CLI setup guide) (AWS Documentation)



1) Create a minimal FastAPI project

Folder structure

fastapi-eb-demo/
  main.py
  requirements.txt
  Procfile

main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
def health():
    return {"status": "ok"}

@app.get("/hello")
def hello(name: str = "world"):
    return {"message": f"Hello, {name}!"}

requirements.txt

fastapi==0.115.0
uvicorn[standard]==0.30.6
gunicorn==22.0.0

Procfile

Elastic Beanstalk’s Python platform expects your web process to listen on port 8000 by default. (AWS Documentation)

web: gunicorn -k uvicorn.workers.UvicornWorker --bind :8000 --workers 2 main:app

Notes:

  • main:app means “import app from main.py”.
  • Increase --workers as needed (start small).

2) Test locally (recommended)

python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -r requirements.txt
uvicorn main:app --reload --port 8000

Visit:



3) Initialize Elastic Beanstalk in the project

From inside the project directory:

eb init

eb init sets defaults for your project directory and creates an Elastic Beanstalk application in your account; you typically create the environment afterward with eb create. (AWS Documentation)

During prompts:

  • Choose a region
  • Choose the Python platform
  • (Optional) Set up SSH keypair if you want to SSH into instances



4) Create an environment

For a first deployment, a single-instance environment is fine; for production you’ll usually want load-balanced/scalable. (AWS Documentation)

eb create fastapi-eb-demo-dev

(EB CLI overview mentions creating environments with a single command like eb create ....) (AWS Documentation)



5) Deploy your FastAPI app

eb deploy

eb deploy deploys code changes to your environment’s instances. (AWS Documentation)

When it finishes:

eb open

Try:

  • /health
  • /hello?name=beanstalk



6) Set environment variables (optional but common)

Use environment properties (environment variables) for configuration values. (AWS Documentation)

Option A: Console

Go to your environment → Configuration and update runtime environment variables in the settings UI. (AWS Documentation)

Option B: .ebextensions (version-controlled)

Create .ebextensions/01-env.config:

option_settings:
  aws:elasticbeanstalk:application:environment:
    APP_ENV: "dev"
    LOG_LEVEL: "info"

option_settings can modify EB configuration and define variables retrievable as environment variables. (AWS Documentation) .ebextensions files live in a .ebextensions/ directory and are deployed with your source bundle. (AWS Documentation)

Deploy again:

eb deploy

7) View health and logs (debugging basics)

Health

Enhanced health can help identify problems that could make your app unavailable. (AWS Documentation)

You can view health in the console, and also:

eb health

Logs

eb logs

eb logs retrieves logs (tail logs by default unless you request full logs). (AWS Documentation) You can also retrieve/download logs in the console and publish to S3 or stream to CloudWatch Logs. (AWS Documentation)



Production checklist (practical recommendations)



Quick reference (handy EB CLI commands)



If you tell me whether you want single-instance (cheap/dev) or load-balanced/scalable (prod), and whether you’ll use RDS (Postgres/MySQL), I can add a ready-to-copy .ebextensions set for common API needs (timeouts, log streaming, instance sizing, etc.).