Deploy a Python Flask App to AWS Lambda (Serverless)

How to deploy a Python Flask web application to AWS Lambda using Serverless architecture with AWS API Gateway and Mangum for event bridging.

Traditional web hosting requires provisioning and managing virtual private servers (VPS), configuring Nginx, and handling continuous system updates. Serverless computing eliminates server management overhead entirely, allowing developers to deploy individual functions or complete web APIs that scale automatically from zero to millions of requests while you only pay for exact compute time consumed.

However, AWS Lambda expects function handlers triggered by specific events (like API Gateway payloads or S3 events), whereas frameworks like Flask expect standard WSGI or ASGI web requests. This is where adapters like **Mangum** bridge the gap. This comprehensive guide walks you through deploying a Python Flask application to AWS Lambda using Serverless architecture.

1. Prerequisites and Tooling Setup

Before deploying, ensure you have the necessary cloud development tools installed on your local machine:

  • An active AWS Account with administrator or programmatic deployment permissions.
  • AWS CLI installed and configured with your credentials (`aws configure`).
  • Node.js and npm installed (required to run the Serverless Framework CLI).
  • Python 3.10+ and pip installed locally.

Install the Serverless Framework CLI globally on your system:

C
npm install -g serverless

2. Creating the Flask Application and Mangum Adapter

Create a new directory for your project, set up a virtual environment, and install Flask along with `mangum`.

C
# Create project directory
mkdir flask-lambda-app
cd flask-lambda-app

# Set up virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install flask mangum

Create an `app.py` file. Here, we wrap our Flask application instance inside Mangum so that API Gateway HTTP events are translated into standard WSGI requests that Flask can process.

C
from flask import Flask, jsonify
from mangum import Mangum

app = Flask(__name__)

@app.route("/", methods=["GET"])
def home():
    return jsonify({
        "message": "Hello from Flask running on AWS Lambda!",
        "status": "success"
    })

@app.route("/health", methods=["GET"])
def health_check():
    return jsonify({"status": "healthy"})

# Wrap Flask app with Mangum for AWS Lambda compatibility
handler = Mangum(app)

3. Configuring Serverless Framework (`serverless.yml`)

Create a `serverless.yml` configuration file in your root directory. This file defines your cloud infrastructure stack, runtime environment, and API Gateway event triggers.

C
service: flask-lambda-api
frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.10
  stage: prod
  region: us-east-1

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: false

functions:
  api:
    handler: app.handler
    events:
      - httpApi: 'ANY /'
      - httpApi: 'ANY /{proxy+}'

Install the python-requirements plugin to ensure your Python dependencies are packaged automatically with your deployment artifact:

C
serverless plugin install -n serverless-python-requirements

4. Deploying to AWS and Testing Your Endpoint

With your application code and configuration ready, deploy your serverless stack directly to AWS using a single CLI command:

C
sls deploy

Once the deployment completes successfully, the Serverless Framework will output your API Gateway endpoint URL in the terminal. You can test your live serverless application using `curl` or your browser:

C
curl https://<api-id>.execute-api.us-east-1.amazonaws.com/

Conclusion

Deploying a Python Flask application to AWS Lambda using Serverless and Mangum provides a robust, zero-maintenance backend architecture that scales infinitely with zero idle server costs. By combining Flask's expressive routing with AWS API Gateway, you can launch production-ready microservices in minutes.