AWS Lambda: Integration and Architectural Patterns
Integrate Lambda functions with AWS services
AWS Lambda: Integration and Architectural Patterns
This study guide focuses on the integration of AWS Lambda with other AWS services, covering connectivity, security, error handling, and performance optimization as required for the DVA-C02 exam.
Learning Objectives
After studying this guide, you should be able to:
- Configure Lambda triggers and destinations for event-driven architectures.
- Integrate Lambda with VPCs to access private resources securely.
- Manage sensitive configuration using Environment Variables, Secrets Manager, and Parameter Store.
- Implement robust error handling using Dead Letter Queues (DLQs) and Lambda Destinations.
- Optimize performance by mitigating cold starts with Provisioned Concurrency.
Key Terms & Glossary
- Cold Start: The latency experienced when a Lambda function is invoked for the first time or after being idle, as AWS must initialize the execution environment.
- Provisioned Concurrency: A feature that keeps a specified number of execution environments warm and ready to respond immediately, eliminating cold start latency.
- Dead Letter Queue (DLQ): An SQS queue or SNS topic used to capture events that a Lambda function fails to process after a specific number of retries.
- Lambda Layers: A distribution mechanism for libraries, custom runtimes, and other dependencies that allows you to keep your deployment package small.
- Event Source Mapping: A resource in Lambda that reads from an event source (like Kinesis or DynamoDB Streams) and invokes a Lambda function.
The "Big Idea"
AWS Lambda is the "glue" of the AWS ecosystem. Its primary power lies not just in running code without servers, but in its ability to react instantly to changes across your infrastructure. By moving from a "request-response" (monolithic) mindset to an event-driven mindset, you create applications that are loosely coupled, highly scalable, and cost-efficient.
Formula / Concept Box
| Feature | Configuration Key Point | Use Case |
|---|---|---|
| Memory | 128 MB to 10,240 MB | Scales CPU and Network proportionally with memory. |
| Timeout | 1 sec to 15 mins (900s) | Hard limit on execution time. |
| Environment Vars | Up to 4 KB total | Store non-sensitive config; use Secrets Manager for sensitive data. |
| VPC Config | Subnet IDs + Security Groups | Required to access RDS or ElastiCache in private subnets. |
Hierarchical Outline
- Event-Driven Integration Patterns
- Synchronous: API Gateway, ELB (Lambda returns a response immediately).
- Asynchronous: S3, SNS, EventBridge (Lambda retries 2 times automatically on failure).
- Polling (Stream/Queue): Kinesis, DynamoDB, SQS (Lambda service pulls data).
- Security & Secret Management
- IAM Roles: Execution role provides permissions to the Lambda; Resource-based policy allows services to trigger Lambda.
- Secrets: Use AWS Secrets Manager for rotation; use SSM Parameter Store for hierarchical configuration.
- Networking & VPC
- Lambda runs in an AWS-managed VPC by default (Internet access).
- To access private resources: Assign Lambda to private subnets.
- To access the internet from a VPC-connected Lambda: Use a NAT Gateway.
- Resiliency & Error Handling
- Retries: 0 for Synchronous; 2 for Asynchronous.
- Destinations: Routes execution records (Success/Failure) to SQS, SNS, EventBridge, or another Lambda.
Visual Anchors
Event-Driven Flow (EventBridge)
Lambda VPC Integration Architecture
Definition-Example Pairs
- Event Source Mapping: The link between a stream or queue and the Lambda function.
- Example: A Lambda function polling an SQS Queue. The mapping handles the polling logic and deletes messages after successful processing.
- Lambda Destinations: A feature for asynchronous invocations that routes the outcome of a function.
- Example: If an image processing Lambda fails, the original event is automatically sent to an Amazon SNS topic to alert developers.
- Environment Variables: Key-value pairs used to modify function behavior without changing code.
- Example: Using a variable named
TABLE_NAMEso the same code can interact with aDev_Tablein development and aProd_Tablein production.
- Example: Using a variable named
Worked Examples
Problem: Managing Sensitive Database Credentials
Scenario: Your Lambda function needs to connect to an RDS PostgreSQL database. You must not hardcode the password.
Step-by-Step Solution:
- Store Secret: Save the database URI and password in AWS Secrets Manager.
- IAM Permissions: Grant the Lambda's execution role the
secretsmanager:GetSecretValuepermission. - Code Integration: Use the AWS SDK inside the Lambda handler to call the Secrets Manager API at runtime.
- Optimization: Cache the secret value outside the handler function so it persists across warm starts, reducing API calls and latency.
Problem: Handling High-Traffic Spikes
Scenario: A flash sale causes a sudden burst of 5,000 concurrent requests to an API backed by Lambda.
Step-by-Step Solution:
- Symptoms: Users might experience "Cold Starts" or
429 Too Many Requests(throttling). - Mitigation: Configure Provisioned Concurrency for the specific Lambda Alias used in production.
- Result: AWS pre-initializes the environments, ensuring the 5,000 requests are handled with sub-millisecond initialization latency.
Checkpoint Questions
- Which AWS service is best suited for storing and automatically rotating database credentials used by a Lambda function?
- If a Lambda function is configured to run inside a VPC, does it have internet access by default? If not, what is required?
- What is the difference between a DLQ and a Lambda Destination for failure handling?
- How many times does Lambda retry an asynchronous invocation by default if the function returns an error?
▶Click to see answers
- AWS Secrets Manager (Parameter Store does not support native rotation).
- No. It requires a NAT Gateway in a public subnet and a route in the private subnet's route table pointing to the NAT Gateway.
- DLQ only captures the event payload on failure; Destinations can capture metadata (stack trace, timestamps) and support both success and failure routing.
- Two retries (three executions total).