โ† Back to Blog

Deploy Python Functions to AWS Lambda in 5 Minutes

Python Aws Lambda Deployment ยท 296 words

Tags: python, aws, serverless, deployment

Why Lambda?

AWS Lambda lets you run code without servers. Pay only for compute time. Perfect for APIs, data processing, and automation scripts.

Quick Deployment with SAM


# Install AWS SAM CLI
pip install aws-sam-cli

# Initialize a new SAM project
sam init --runtime python3.12 --name my-function

# Build and deploy
sam build
sam deploy --guided

Minimal Lambda Function


import json
import os
from datetime import datetime

def handler(event, context):
    """Process API Gateway events."""
    
    # Parse request
    path = event.get('path', '/')
    method = event.get('httpMethod', 'GET')
    body = json.loads(event.get('body', '{}')) if event.get('body') else {}
    
    # Route handling
    if path == '/health':
        response = {
            'status': 'healthy',
            'timestamp': datetime.utcnow().isoformat(),
            'region': os.environ.get('AWS_REGION', 'unknown'),
        }
    elif path == '/process' and method == 'POST':
        # Example: process data
        result = process_data(body)
        response = {'result': result}
    else:
        response = {'error': 'Not found'}
    
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*',
        },
        'body': json.dumps(response)
    }


def process_data(data):
    """Example data processing."""
    return {
        'processed': True,
        'items': len(data.get('items', [])),
        'timestamp': datetime.utcnow().isoformat()
    }

Environment Variables


# template.yaml
Globals:
  Function:
    Timeout: 30
    Runtime: python3.12
    MemorySize: 256
    Environment:
      Variables:
        TABLE_NAME: !Ref DataTable
        AWS_REGION: !Ref AWS::Region

Best Practices

1. Cold starts: Keep functions warm with CloudWatch Events

2. Memory: More memory = faster execution = lower cost

3. Layers: Share dependencies across functions

4. Errors: Always return proper error responses

5. Logging: Use CloudWatch structured logging

Cost Optimization

Lambda charges per request and per GB-second:

Related Products

Need serverless templates and deployment scripts? We have production-ready Lambda packages at our store.

[Browse the collection โ†’](https://petroleum-board-hawaii-lol.trycloudflare.com)

๐Ÿ›’ Ready to deploy?

Browse 120+ Python tools with crypto payments and instant delivery.

Browse Products โ†’