Building Serverless Lambda
Building Serverless Architectures with AWS Lambda
Section titled “Building Serverless Architectures with AWS Lambda”Operational Task Comparison
Section titled “Operational Task Comparison”- 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 ✓
- 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.
AWS Lambda
Section titled “AWS Lambda”Core Features
Section titled “Core Features”- 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
Configuration Options
Section titled “Configuration Options”- 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)
Deployment Options
Section titled “Deployment Options”- .zip File Archives: Traditional deployment package format
- Container Images: Deploy Lambda functions as container images
Lambda Function Location Options
Section titled “Lambda Function Location Options”AWS Lambda Service VPC
Section titled “AWS Lambda Service VPC”- 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
Lambda@Edge
Section titled “Lambda@Edge”- 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.
Connecting Lambda to Your VPC
Section titled “Connecting Lambda to Your VPC”By default, Lambda functions aren’t connected to VPCs in your account. When VPC connection is needed:
Hyperplane ENIs
Section titled “Hyperplane ENIs”- 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
Internet Access Requirements
Section titled “Internet Access Requirements”- NAT Gateway: Route outbound traffic to NAT gateway in public subnet
- Public IP: NAT gateway connects to internet through VPC’s internet gateway
Scaling Considerations
Section titled “Scaling Considerations”- Database Connections: RDS proxy manages connection pools to prevent saturation
- EC2 Scaling: Deploy behind Application Load Balancer in Auto Scaling group
Lambda Processing Types
Section titled “Lambda Processing Types”Synchronous Processing
Section titled “Synchronous Processing”- 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
Asynchronous Processing
Section titled “Asynchronous Processing”- 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
Streaming Processing
Section titled “Streaming Processing”- 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
Lambda Invocation Methods
Section titled “Lambda Invocation Methods”Synchronous Invocation
Section titled “Synchronous Invocation”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
Asynchronous Invocation
Section titled “Asynchronous Invocation”- 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
Event Source Mappings
Section titled “Event Source Mappings”- 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 Handler
Section titled “Lambda Function Handler”import jsondef 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
Handler Components
Section titled “Handler Components”- 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()
Best Practices
Section titled “Best Practices”- 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
Lambda Layers
Section titled “Lambda Layers”Without Layers
Section titled “Without Layers”Each Lambda function packages its own dependencies and custom runtimes, leading to:
- Larger deployment packages
- Code duplication across functions
- Harder maintenance and updates
With Layers
Section titled “With Layers”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
Benefits of Using Layers
Section titled “Benefits of Using Layers”- 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.