Skip to content
Pablo Rodriguez

Building Serverless Lambda

Building Serverless Architectures with AWS Lambda

Section titled “Building Serverless Architectures with AWS Lambda”
  • Configure an instance ✓
  • Update operating system ✓
  • Install application platform ✓
  • Build and deploy apps ✓
  • Configure auto scaling and load balancing ✓
  • Continuously secure and monitor instances ✓
  • Monitor and maintain apps ✓

The serverless approach reduces overhead and allows developers to focus on code that makes their business unique, rather than managing underlying infrastructure.

  • Serverless Compute: Run code without provisioning or managing servers
  • High-Availability Infrastructure: AWS handles all compute resource administration
  • Automatic Operations: Server and OS maintenance, capacity provisioning, automatic scaling, and logging
  • Runtime Language: Choose your preferred programming language
  • Memory Allocation: 128MB minimum to 10,240MB maximum (determines CPU and network bandwidth)
  • Timeout Duration: Maximum 15 minutes (AWS hard limit)
  • .zip File Archives: Traditional deployment package format
  • Container Images: Deploy Lambda functions as container images
  • Default Location: Functions run in Lambda service VPC
  • Isolation: Firecracker virtual machines on EC2 instances
  • Microvm Technology: Created in under a second by Firecracker (developed by AWS)
  • Runtime Management: Lambda service manages networking, security, and monitoring
  • Extension: Run functions at Amazon CloudFront regional edge locations
  • Supported Languages: Node.js or Python functions
  • Development Region: Authored in US East (N. Virginia)
  • Global Execution: Run globally at AWS edge locations closer to viewers
  • Use Case: Customize CloudFront content delivery (e.g., jacket color selection based on cookies)

Alternative CloudFront Functions available with no network access and smaller execution time/package size limits.

By default, Lambda functions aren’t connected to VPCs in your account. When VPC connection is needed:

  • Managed Resources: AWS provides Hyperplane elastic network interfaces
  • Connection Method: VPC-to-VPC NAT (V2N) from Lambda VPC to customer VPC
  • Direction: One-way connectivity from Lambda VPC to customer VPC
  • NAT Gateway: Route outbound traffic to NAT gateway in public subnet
  • Public IP: NAT gateway connects to internet through VPC’s internet gateway
Bottleneck Alert
  • Database Connections: RDS proxy manages connection pools to prevent saturation
  • EC2 Scaling: Deploy behind Application Load Balancer in Auto Scaling group
  • Use Cases: Web apps, web services, microservices, machine learning inferences
  • Pattern: Requestor expects response within certain time period
  • Examples:
    • Shopping cart updates via API Gateway
    • Mobile app click processing via Lambda function URLs
  • Use Cases: Scheduled events, queued messages, image/video transformation, AWS service triggers
  • Pattern: Request offloaded for later processing
  • Examples:
    • Package delivery status updates
    • File processing and image identification
  • Use Cases: Continuous data streams requiring batch processing
  • Integration: Built-in capability to batch stream data
  • Examples:
    • DynamoDB Streams for table change aggregation
    • Data analytics and format transformation

API Gateway Integration:

  • Browser client → Amazon API Gateway → AWS Lambda → Response
  • API Gateway handles routing and returns Lambda response to client

Function URL:

  • Browser client → Lambda function URL → Direct Lambda invocation
  • Dedicated HTTPS endpoint with resource-based policies for security
  • CORS configuration support
  • Queue Processing: Lambda places events in queue and returns success response
  • Error Handling: Configure error handling and send records to downstream resources
  • Service Integration: Amazon S3, Amazon SNS invoke functions asynchronously
  • Scheduling: EventBridge for scheduled or recurring processes
  • Polling Services: Lambda polls DynamoDB, Kinesis, SQS, DocumentDB
  • Batch Processing: Multiple events batched together in single payload
  • Size Limits: Configure batch window and payload size (6 MB maximum)
  • Example: DynamoDB order status changes trigger customer notifications
lambda_function.py
import json
def lambda_handler(event, context):
length = event['length']
width = event['width']
area = calculate_area(length, width)
data = {"area": area}
return json.dumps(data)
def calculate_area(length, width):
return length * width
  • Entry Point: lambda_handler method processes events
  • Event Object: JSON document containing input data and invoking service data
  • Context Object: Provides methods and properties about function runtime and invocation
  • Return Value: JSON response using json.dumps()
  • Keep handler function small and put business logic in separate methods
  • Reduces handler function load times
  • Use Amazon Q Developer for on-demand code recommendations

Each Lambda function packages its own dependencies and custom runtimes, leading to:

  • Larger deployment packages
  • Code duplication across functions
  • Harder maintenance and updates

Lambda layers are .zip archives containing supplementary code or data:

  • Library Dependencies: Shared across multiple functions
  • Custom Runtimes: Package alternative runtimes in layers
  • Configuration Files: Centralized configuration management
  • Reduced Package Size: Smaller deployment packages and organized structure
  • Separation of Concerns: Update dependencies independent of function code
  • Code Sharing: Apply single layer to multiple functions in account
  • Development Tools: Enables use of Lambda console code editor for smaller packages

Lambda provides a serverless compute solution that eliminates server management while offering flexible invocation patterns, VPC connectivity options, and code organization through layers.