Jonatan Matajonmatum.com
conceptsnotesexperimentsessays
© 2026 Jonatan Mata. All rights reserved.v2.1.1
Concepts

AWS Lambda

AWS serverless compute service that runs code in response to events without provisioning or managing servers, automatically scaling from zero to thousands of concurrent executions.

evergreen#aws#lambda#serverless#faas#event-driven#cloud

What it is

AWS Lambda is Amazon's Functions as a Service (FaaS) offering that lets you run code without provisioning or managing servers. You upload your function, define what events trigger it, and AWS handles provisioning, automatic scaling, high availability, and monitoring.

Lambda operates on an event-driven execution model: when a trigger occurs (HTTP request, queue message, file uploaded to S3), AWS provisions an execution environment, runs your code, and charges only for the compute time used. This model eliminates the need to maintain idle servers and enables instant scaling from zero to thousands of concurrent executions.

The service supports multiple runtimes (Python, Node.js, Java, Go, .NET, Ruby) and allows deployments with both ZIP files and container images, offering flexibility for different architectures and use cases.

Execution model and lifecycle

Lambda operates with a specific lifecycle that directly impacts performance and costs:

  1. Event trigger: An event triggers the function (API Gateway, S3, SQS, etc.)
  2. Environment initialization: Lambda provisions an execution environment or reuses an existing one
  3. Runtime startup: The runtime initializes and loads the function code
  4. Handler execution: Your function code executes
  5. Environment cleanup: The environment may stay "warm" for subsequent invocations

During the initialization phase, Lambda executes any code outside the handler — ideal for initializing database connections, HTTP clients, or configurations that can be reused across invocations.

Code example: Handler with best practices

import json
import logging
import os
import boto3
from botocore.exceptions import ClientError
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
 
# Initialization outside handler (reused across invocations)
logger = Logger()
tracer = Tracer()
metrics = Metrics()
 
# Reusable DynamoDB client
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['TABLE_NAME'])
 
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
@metrics.log_metrics(capture_cold_start_metric=True)
def lambda_handler(event, context):
    """
    Processes API Gateway events with structured error handling
    and complete observability using Lambda Powertools.
    """
    try:
        # Extract data from event
        body = json.loads(event.get('body', '{}'))
        user_id = body.get('user_id')
        
        if not user_id:
            logger.error("Missing user_id in request body")
            return {
                'statusCode': 400,
                'body': json.dumps({'error': 'user_id is required'})
            }
        
        # Business operation with tracing
        with tracer.subsegment("dynamodb_operation"):
            response = table.get_item(Key={'user_id': user_id})
            
        if 'Item' not in response:
            logger.info(f"User {user_id} not found")
            metrics.add_metric(name="UserNotFound", unit=MetricUnit.Count, value=1)
            return {
                'statusCode': 404,
                'body': json.dumps({'error': 'User not found'})
            }
        
        # Success
        user_data = response['Item']
        logger.info(f"Successfully retrieved user {user_id}")
        metrics.add_metric(name="UserRetrieved", unit=MetricUnit.Count, value=1)
        
        return {
            'statusCode': 200,
            'body': json.dumps(user_data, default=str)
        }
        
    except ClientError as e:
        logger.error(f"DynamoDB error: {e.response['Error']['Message']}")
        metrics.add_metric(name="DynamoDBError", unit=MetricUnit.Count, value=1)
        return {
            'statusCode': 500,
            'body': json.dumps({'error': 'Internal server error'})
        }
    except Exception as e:
        logger.exception("Unexpected error occurred")
        metrics.add_metric(name="UnexpectedError", unit=MetricUnit.Count, value=1)
        return {
            'statusCode': 500,
            'body': json.dumps({'error': 'Internal server error'})
        }

Lambda Layers and dependency management

Lambda Layers allow sharing code, libraries, and configurations across multiple functions:

  • Shared libraries: Common dependencies (boto3, requests, pandas) packaged once
  • Runtime customization: Monitoring extensions, security agents
  • Configuration management: Environment variables, certificates, configuration files
  • Size optimization: Reduce deployment package size for each function

A Layer can contain up to 250 MB (uncompressed) and a function can use up to 5 Layers. Layers are mounted at /opt during execution, allowing the runtime to access libraries automatically.

Container image support

Since 2020, Lambda supports container images as an alternative to ZIP files:

AspectZIP PackageContainer Image
Maximum size250 MB (compressed)10 GB
Deployment timeFast (seconds)Moderate (minutes)
Runtime flexibilityPredefined runtimesFully customizable runtime
ToolingSAM, Serverless FrameworkDocker, ECR
Cold startOptimizedSlightly higher

Container images are ideal for functions with heavy dependencies (ML models, scientific libraries) or when you need full control over the runtime environment.

Cold start mitigation

Cold starts impact initial function latency. Mitigation strategies:

Provisioned Concurrency: Maintains pre-initialized environments

  • Eliminates cold starts completely
  • Cost: pay for reserved capacity (even without usage)
  • Ideal for critical APIs with strict SLAs

SnapStart (Java only):

  • Pre-initializes JVM and caches state
  • Reduces Java cold starts from ~10s to ~200ms
  • No additional cost, but requires deterministic functions

Architectural optimizations:

  • Small, focused functions
  • Minimize dependencies in deployment package
  • Use Layers for shared libraries
  • Implement warming strategies for critical functions

Lambda Powertools and observability

AWS Lambda Powertools provides utilities for structured logging, metrics, and tracing:

  • Structured logging: JSON-formatted logs with automatic correlation IDs
  • Distributed tracing: Native integration with AWS X-Ray
  • Custom metrics: Business metrics sent to CloudWatch
  • Idempotency: Automatic prevention of duplicate processing
  • Event source data classes: Typed parsing of events from different services

Powertools is available for Python, TypeScript, Java, and .NET, following the same APIs and patterns across all runtimes.

Cost model and optimization

Lambda charges for three main components:

Requests: $0.20 per million requests Duration: $0.0000166667 per GB-second of execution Provisioned Concurrency: $0.0000041667 per GB-second of reserved capacity

Cost optimization strategies

  1. Memory tuning: More memory = more CPU = faster execution

    • Use AWS Lambda Power Tuning to find the optimal point
    • Example: 512 MB may be cheaper than 256 MB if it reduces duration >50%
  2. Architecture patterns:

    • Batch processing to reduce number of invocations
    • Async processing with SQS for non-critical loads
    • Caching with ElastiCache for frequently accessed data
  3. Free tier: 1M free requests + 400,000 GB-seconds monthly

Lambda@Edge vs CloudFront Functions

For logic executed at CloudFront's edge:

FeatureLambda@EdgeCloudFront Functions
RuntimeNode.js, PythonJavaScript (ES5)
Maximum duration30s1ms
Maximum memory3008 MB2 MB
Triggers4 CloudFront events2 CloudFront events
CostMore expensiveCheaper
Use casesA/B testing, authURL rewrites, headers

IAM integration and security

Each Lambda function requires an execution role that defines which AWS services it can access. Security principles:

  • Least privilege: Grant only the minimum necessary permissions
  • Resource-based policies: Control which services can invoke the function
  • VPC integration: Run functions within VPCs for private resource access
  • Environment variables encryption: Use AWS KMS for sensitive data
  • Secrets management: Integration with AWS Secrets Manager for credentials

Why it matters

Lambda represents a fundamental shift in the compute model: from "servers running applications" to "functions responding to events." For event-driven architectures, this model offers significant advantages: automatic scaling, pay-per-actual-use, and elimination of infrastructure management.

However, Lambda is not a universal solution. Duration limitations (15 minutes), stateless model, and cold starts make it unsuitable for long-running applications or those with extremely low latency requirements. The decision between Lambda and traditional containers should consider traffic patterns, performance requirements, and operational complexity.

References

  • AWS Lambda Developer Guide — AWS, 2024. Complete official documentation.
  • Lambda Powertools Documentation — AWS, 2024. Utilities for observability and best practices.
  • AWS Lambda Pricing Calculator — AWS, 2024. Official cost calculator.
  • Lambda Power Tuning — Alex Casalboni, 2024. Open source tool for memory/cost optimization.
  • Serverless Architectures with AWS Lambda — AWS, 2023. Whitepaper on architectural patterns.
  • Lambda Container Image Support — AWS, 2020. Announcement and container image guide.

Related content

  • Serverless

    Cloud computing model where the provider manages infrastructure automatically, allowing code execution without provisioning or managing servers, paying only for actual usage.

  • AWS IAM

    AWS identity and access management service controlling who can do what in your account, with granular policies based on the principle of least privilege.

  • Event-Driven Architecture

    Architectural pattern where components communicate through asynchronous events, enabling decoupled, scalable, and reactive systems.

  • From Prototype to Production: A Serverless Second Brain on AWS

    Architecture design for scaling a personal second brain to a production system with AWS serverless — from the current prototype to specialized use cases in legal, research, and community building.

  • Terraform AWS Serverless Modules

    Collection of 13 Terraform modules published on the Terraform Registry for deploying serverless architectures on AWS, with 12 examples covering basic ECS to full-stack CRUD with DynamoDB and AgentCore with MCP.

  • Serverless Second Brain

    Production-ready serverless backend for a personal knowledge graph — DynamoDB, Lambda, Bedrock, MCP, Step Functions. The implementation of the architecture described in the 'From Prototype to Production' essay.

  • PR Auto-Approver

    Serverless GitHub App that auto-approves pull requests after CI passes, with optional AI code review via Amazon Bedrock. Five repositories: TypeScript/Probot app, AWS Terraform module (Lambda + API Gateway + Secrets Manager + SQS DLQ), GitHub Terraform module (webhooks), deployment infra, and test repo.

  • AWS Step Functions

    AWS serverless orchestration service that coordinates multiple services into visual workflows using Amazon States Language (ASL), with built-in error handling, retries, and parallel execution.

  • AWS SQS

    AWS fully managed message queue service that decouples distributed application components, guaranteeing message delivery with unlimited scalability.

  • AWS SNS

    AWS pub/sub messaging service that distributes messages to multiple subscribers simultaneously, enabling fan-out patterns and notifications at scale.

  • AWS SAM

    AWS open-source framework for building serverless applications with simplified CloudFormation syntax, CLI for local development, and integrated deployment.

  • AWS EventBridge

    AWS serverless event bus connecting applications using events, enabling decoupled event-driven architectures with rule-based routing.

  • AWS DynamoDB

    AWS serverless NoSQL database with single-digit millisecond latency at any scale, ideal for applications requiring high performance and automatic scalability.

  • AWS API Gateway

    AWS managed service for creating, publishing, and managing REST, HTTP, and WebSocket APIs that act as entry points to Lambda functions and other backend services.

Concepts