Last modified: Jan 01, 2026 By Alexander Williams

Deploy Python Apps on AWS Lambda Guide

AWS Lambda is a serverless compute service.

It runs your code in response to events.

You only pay for the compute time you consume.

This makes it cost-effective and scalable.

Python is a popular language for Lambda functions.

Its simplicity and rich library ecosystem are key.

This guide walks you through the deployment process.

Understanding AWS Lambda and Python

Lambda executes your code without provisioning servers.

It automatically scales from a few requests to thousands.

You upload your code and Lambda handles everything else.

Python is a supported runtime.

You can use versions like Python 3.9, 3.10, or 3.11.

The handler function is the entry point for your code.

Lambda calls this handler when the function is invoked.

Prerequisites and Initial Setup

First, you need an AWS account.

Install the AWS CLI and configure it with your credentials.

You should have Python and pip installed locally.

Basic knowledge of Python is required.

Familiarity with virtual environments is helpful.

You can manage AWS resources using the Getting Started with AWS SDK for Python Boto3 guide.

Writing Your Lambda Function

Create a simple Python script.

Define a function that accepts two arguments.

The lambda_handler is the standard name.

It takes an event and a context object.

The event contains data from the invoker.

The context provides runtime information.

Here is a basic example.


# lambda_function.py
import json

def lambda_handler(event, context):
    """
    A simple AWS Lambda handler function.
    Returns a greeting message.
    """
    # Extract a name from the event, default to 'World'
    name = event.get('name', 'World')

    # Construct the response body
    body = {
        'message': f'Hello, {name}!',
        'input_event': event
    }

    # Return a proper Lambda response
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(body)
    }

Packaging Dependencies for Deployment

Real applications need third-party packages.

Lambda's execution environment may not have them.

You must include them in your deployment package.

Create a virtual environment in your project folder.

Install required packages using pip.

Package them with your function code.

For complex packages, refer to Install Python Package in AWS Lambda.


# In your project directory
mkdir my_lambda_function
cd my_lambda_function
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install requests  # Example package
deactivate

Next, create the deployment zip file.


# Package the function and dependencies
cd my_lambda_function
cp lambda_function.py .
cp -r venv/lib/python3.9/site-packages/* .  # Adjust Python version
zip -r9 deployment_package.zip .

Deploying to AWS Lambda

You can deploy via the AWS Console, CLI, or SAM.

Using the AWS CLI is efficient for automation.

First, create an IAM role for your Lambda function.

This role grants necessary permissions.

Then use the create-function command.


# Create the Lambda function
aws lambda create-function \
    --function-name my-python-function \
    --runtime python3.9 \
    --role arn:aws:iam::123456789012:role/lambda-execution-role \
    --handler lambda_function.lambda_handler \
    --zip-file fileb://deployment_package.zip

If the function already exists, update it.


# Update the function code
aws lambda update-function-code \
    --function-name my-python-function \
    --zip-file fileb://deployment_package.zip

Testing and Invoking Your Function

Test your function after deployment.

Use the AWS Console test feature or the CLI.

The invoke command calls your function.


# Invoke the function with a test event
aws lambda invoke \
    --function-name my-python-function \
    --payload '{"name": "Alice"}' \
    response.json

# View the output
cat response.json

# Expected output in response.json
{
    "statusCode": 200,
    "headers": {"Content-Type": "application/json"},
    "body": "{\"message\": \"Hello, Alice!\", \"input_event\": {\"name\": \"Alice\"}}"
}

Best Practices and Advanced Tips

Keep your deployment packages small.

Use Lambda Layers for common dependencies.

Set appropriate memory and timeout settings.

Implement proper logging using the print function.

Logs are automatically sent to Amazon CloudWatch.

For web frameworks like FastAPI, use an adapter.

Check Deploy FastAPI to AWS Lambda with Mangum for details.

Always use environment variables for configuration.

Never store secrets in your code.

Conclusion

Deploying Python on AWS Lambda is straightforward.

You write your function, package it, and deploy.

The serverless model offers great scalability.

It also reduces operational overhead.

Start with simple functions.

Gradually explore more complex use cases.

Use the linked guides for deeper dives.

Happy coding with AWS Lambda and Python!