Skip to content

Lambda

Chapter 8: AWS Lambda - Serverless Computing

Section titled “Chapter 8: AWS Lambda - Serverless Computing”

Building Event-Driven Serverless Applications

Section titled “Building Event-Driven Serverless Applications”

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources.

Lambda Core Concepts
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS Lambda | |
| +------------------------+ |
| | |
| +-----------+-----------+-----------+-----------+ |
| | | | | | |
| v v v v v |
| +-------+ +-------+ +-------+ +-------+ +-------+ |
| |Triggers| |Function| |Runtime| |Layers | |Config | |
| | | | Code | | | | | | | |
| +-------+ +-------+ +-------+ +-------+ +-------+ |
| |
| Triggers: Event sources that invoke Lambda |
| Function Code: Your application logic |
| Runtime: Execution environment (Node.js, Python, etc.) |
| Layers: Shared code and dependencies |
| Config: Memory, timeout, environment variables |
| |
+------------------------------------------------------------------+

Lambda Execution Environment
+------------------------------------------------------------------+
| |
| +----------------------------------------------------------+ |
| | Execution Environment | |
| | | |
| | +------------------+ +------------------------+ | |
| | | Runtime API | | Your Function Code | | |
| | | | | | | |
| | | - Node.js | | exports.handler = | | |
| | | - Python | | async (event) => { | | |
| | | - Java | | // Your code | | |
| | | - Go | | } | | |
| | | - .NET | | | | |
| | | - Ruby | +------------------------+ | |
| | | - Custom | | |
| | +------------------+ | |
| | | |
| | +------------------+ +------------------------+ | |
| | | /tmp Storage | | Environment Vars | | |
| | | (512 MB - 10GB)| | (Key-Value pairs) | | |
| | +------------------+ +------------------------+ | |
| | | |
| +----------------------------------------------------------+ |
| |
| Lifecycle: |
| 1. INIT phase - Load function, run initialization code |
| 2. INVOKE phase - Process events |
| 3. SHUTDOWN phase - Clean up (on environment shutdown) |
| |
+------------------------------------------------------------------+
Cold Start vs Warm Start
+------------------------------------------------------------------+
| |
| Cold Start |
| +----------------------------------------------------------+ |
| | | |
| | Request arrives | |
| | | | |
| | v | |
| | +----------+ +----------+ +----------+ | |
| | | Download | --> | Initialize| --> | Execute | | |
| | | Code | | Runtime | | Function | | |
| | +----------+ +----------+ +----------+ | |
| | | | | | |
| | v v v | |
| | ~100ms ~200ms ~50ms | |
| | | |
| | Total: ~350ms (can be higher for Java/.NET) | |
| +----------------------------------------------------------+ |
| |
| Warm Start |
| +----------------------------------------------------------+ |
| | | |
| | Request arrives | |
| | | | |
| | v | |
| | +----------+ +----------+ | |
| | | Reuse | --> | Execute | | |
| | | Container| | Function | | |
| | +----------+ +----------+ | |
| | | |
| | Total: ~50ms (much faster!) | |
| +----------------------------------------------------------+ |
| |
| Reducing Cold Starts: |
| +----------------------------------------------------------+ |
| | - Use Provisioned Concurrency | |
| | - Keep functions small | |
| | - Use interpreted languages (Node.js, Python) | |
| | - Minimize dependencies | |
| | - Use SnapStart (Java) | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Lambda Trigger Sources
+------------------------------------------------------------------+
| |
| Push Model (Synchronous) |
| +----------------------------------------------------------+ |
| | | |
| | API Gateway User Services | |
| | +----------+ +----------+ | |
| | | HTTP | | Custom | | |
| | | Request | ------> | SDK | ------> Lambda | |
| | +----------+ +----------+ | |
| | | |
| | - Caller waits for response | |
| | - Error handling by caller | |
| +----------------------------------------------------------+ |
| |
| Push Model (Asynchronous) |
| +----------------------------------------------------------+ |
| | | |
| | S3 SNS EventBridge | |
| | +----------+ +----------+ +----------+ | |
| | | Object | | Message | | Schedule | | |
| | | Upload | -----> | Publish | -----> | Rule | --+--> Lambda
| | +----------+ +----------+ +----------+ | |
| | | |
| | - Lambda retries on failure (0-2 times) | |
| | - Can configure DLQ | |
| +----------------------------------------------------------+ |
| |
| Poll-Based Model |
| +----------------------------------------------------------+ |
| | | |
| | Kinesis DynamoDB SQS | |
| | +----------+ +----------+ +----------+ | |
| | | Stream | | Stream | | Queue | | |
| | +----------+ +----------+ +----------+ | |
| | | | | | |
| | +---------+---------+---------+---------+ | |
| | | | | |
| | v v | |
| | Lambda (polls for records) | |
| | | |
| | - Lambda manages polling | |
| | - Batch size configurable | |
| | - Checkpointing handled | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Common Trigger Configurations
+------------------------------------------------------------------+
| |
| API Gateway Trigger |
| +----------------------------------------------------------+ |
| | | |
| | Client -> API Gateway -> Lambda | |
| | | |
| | Event Structure: | |
| | { | |
| | "httpMethod": "POST", | |
| | "path": "/users", | |
| | "headers": { "Content-Type": "application/json" }, | |
| | "body": "{\"name\": \"John\"}" | |
| | } | |
| +----------------------------------------------------------+ |
| |
| S3 Trigger |
| +----------------------------------------------------------+ |
| | | |
| | S3 Upload -> Lambda | |
| | | |
| | Event Structure: | |
| | { | |
| | "Records": [{ | |
| | "s3": { | |
| | "bucket": { "name": "my-bucket" }, | |
| | "object": { "key": "uploads/file.pdf" } | |
| | } | |
| | }] | |
| | } | |
| +----------------------------------------------------------+ |
| |
| DynamoDB Stream Trigger |
| +----------------------------------------------------------+ |
| | | |
| | DynamoDB -> Stream -> Lambda | |
| | | |
| | Event Structure: | |
| | { | |
| | "Records": [{ | |
| | "eventName": "INSERT", | |
| | "dynamodb": { | |
| | "Keys": { "id": "123" }, | |
| | "NewImage": { "name": "John" } | |
| | } | |
| | }] | |
| | } | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Lambda Configuration Options
+------------------------------------------------------------------+
| |
| Memory Configuration |
| +----------------------------------------------------------+ |
| | | |
| | Memory Range: 128 MB - 10,240 MB (in 1 MB increments) | |
| | | |
| | Memory vCPU Network Performance | |
| | +--------+-------+-------------------+ | |
| | | 128 MB | ~1 | Very Low | | |
| | | 512 MB | ~1 | Low | | |
| | | 1024 MB| ~1 | Moderate | | |
| | | 1769 MB| ~1 | Up to 1 Gbps | | |
| | | 2048 MB| ~2 | Up to 1.5 Gbps | | |
| | | 4096 MB| ~3 | Up to 2.5 Gbps | | |
| | | 10240 MB| ~6 | Up to 10 Gbps | | |
| | +--------+-------+-------------------+ | |
| | | |
| | Note: More memory = more CPU power | |
| +----------------------------------------------------------+ |
| |
| Timeout Configuration |
| +----------------------------------------------------------+ |
| | | |
| | Range: 1 second - 15 minutes | |
| | | |
| | Best Practice: | |
| | - Set timeout slightly above expected execution time | |
| | - Consider retry logic for longer operations | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Lambda Environment Variables
+------------------------------------------------------------------+
| |
| Configuration: |
| +----------------------------------------------------------+ |
| | | |
| | Key Value | |
| | +--------------------+--------------------+ | |
| | | DB_TABLE | users | | |
| | | S3_BUCKET | my-data-bucket | | |
| | | LOG_LEVEL | INFO | | |
| | | API_ENDPOINT | https://api.example| | |
| | +--------------------+--------------------+ | |
| | | |
| | Access in code: | |
| | process.env.DB_TABLE // Node.js | |
| | os.environ['DB_TABLE'] // Python | |
| | System.getenv("DB_TABLE") // Java | |
| +----------------------------------------------------------+ |
| |
| Reserved Environment Variables: |
| +----------------------------------------------------------+ |
| | AWS_REGION - Region (e.g., us-east-1) | |
| | AWS_LAMBDA_FUNCTION_NAME - Function name | |
| | AWS_LAMBDA_FUNCTION_MEMORY_SIZE - Memory in MB | |
| | AWS_LAMBDA_FUNCTION_VERSION - Version | |
| | AWS_EXECUTION_ENV - Runtime identifier | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

# Python Handler Example
import json
import boto3
# Initialize outside handler for connection reuse
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users')
def lambda_handler(event, context):
"""
Main handler function
Args:
event: Event data from trigger
context: Runtime information (request_id, function_name, etc.)
Returns:
Response object
"""
try:
# Parse input
user_id = event['pathParameters']['id']
# Business logic
response = table.get_item(Key={'id': user_id})
if 'Item' not in response:
return {
'statusCode': 404,
'body': json.dumps({'error': 'User not found'})
}
# Return response
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps(response['Item'])
}
except Exception as e:
# Error handling
print(f"Error: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
// Node.js Handler Example
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
// Initialize outside handler for connection reuse
const USERS_TABLE = process.env.USERS_TABLE;
exports.handler = async (event) => {
try {
// Parse input
const userId = event.pathParameters.id;
// Business logic
const params = {
TableName: USERS_TABLE,
Key: { id: userId }
};
const result = await dynamodb.get(params).promise();
if (!result.Item) {
return {
statusCode: 404,
body: JSON.stringify({ error: 'User not found' })
};
}
// Return response
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(result.Item)
};
} catch (error) {
// Error handling
console.error('Error:', error);
return {
statusCode: 500,
body: JSON.stringify({ error: error.message })
};
}
};

Lambda Layers Structure
+------------------------------------------------------------------+
| |
| Function with Layers |
| +----------------------------------------------------------+ |
| | | |
| | /opt (Layers mount point) | |
| | +--------------------------------------------------+ | |
| | | Layer 1: /opt/nodejs/node_modules/ | | |
| | | (Shared libraries) | | |
| | +--------------------------------------------------+ | |
| | +--------------------------------------------------+ | |
| | | Layer 2: /opt/python/lib/python3.9/site-packages/ | | |
| | | (Python dependencies) | | |
| | +--------------------------------------------------+ | |
| | +--------------------------------------------------+ | |
| | | Layer 3: /opt/bin/ | | |
| | | (Custom binaries) | | |
| | +--------------------------------------------------+ | |
| | | |
| | Function Code: /var/task/ | |
| | | |
| +----------------------------------------------------------+ |
| |
| Layer Contents: |
| +----------------------------------------------------------+ |
| | - Runtime-specific libraries | |
| | - Custom runtimes | |
| | - Shared code across functions | |
| | - Configuration files | |
| | - Native libraries | |
| +----------------------------------------------------------+ |
| |
| Benefits: |
| +----------------------------------------------------------+ |
| | - Reduce deployment package size | |
| | - Share code across functions | |
| | - Separate dependencies from code | |
| | - Version control for dependencies | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Lambda Deployment Options
+------------------------------------------------------------------+
| |
| 1. Zip File Deployment |
| +----------------------------------------------------------+ |
| | | |
| | Size Limits: | |
| | - Direct upload: 50 MB (zipped) | |
| | - S3 upload: 250 MB (zipped) | |
| | - Unzipped: 250 MB | |
| | | |
| | Structure: | |
| | function.zip | |
| | +-- lambda_function.py | |
| | +-- requirements.txt | |
| | +-- utils/ | |
| | +-- helpers.py | |
| +----------------------------------------------------------+ |
| |
| 2. Container Image Deployment |
| +----------------------------------------------------------+ |
| | | |
| | Size Limit: 10 GB | |
| | | |
| | Dockerfile: | |
| | FROM public.ecr.aws/lambda/python:3.9 | |
| | | |
| | COPY requirements.txt . | |
| | RUN pip install -r requirements.txt | |
| | | |
| | COPY lambda_function.py . | |
| | CMD ["lambda_function.handler"] | |
| | | |
| | Benefits: | |
| | - Larger dependencies | |
| | - Custom runtimes | |
| | - Consistent local testing | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Create deployment package
zip -r function.zip lambda_function.py
# Create function
aws lambda create-function \
--function-name my-function \
--runtime python3.9 \
--role arn:aws:iam::123456789012:role/lambda-role \
--handler lambda_function.handler \
--zip-file fileb://function.zip
# Update function code
aws lambda update-function-code \
--function-name my-function \
--zip-file fileb://function.zip
# Update function configuration
aws lambda update-function-configuration \
--function-name my-function \
--timeout 30 \
--memory-size 512 \
--environment Variables={DB_TABLE=users,LOG_LEVEL=INFO}
# Publish version
aws lambda publish-version \
--function-name my-function \
--description "Production version"
# Create alias
aws lambda create-alias \
--function-name my-function \
--name production \
--function-version 1

Lambda Performance Optimization
+------------------------------------------------------------------+
| |
| 1. Minimize Cold Starts |
| +----------------------------------------------------------+ |
| | - Use Provisioned Concurrency for critical functions | |
| | - Keep deployment package small | |
| | - Initialize outside handler | |
| | - Use SnapStart for Java (if available in region) | |
| +----------------------------------------------------------+ |
| |
| 2. Optimize Memory |
| +----------------------------------------------------------+ |
| | - Test different memory settings | |
| | - Higher memory = more CPU | |
| | - Use CloudWatch Lambda Insights | |
| | - Balance cost vs performance | |
| +----------------------------------------------------------+ |
| |
| 3. Connection Reuse |
| +----------------------------------------------------------+ |
| | - Initialize SDK clients outside handler | |
| | - Use connection pooling | |
| | - Keep connections alive | |
| +----------------------------------------------------------+ |
| |
| 4. Efficient Code |
| +----------------------------------------------------------+ |
| | - Avoid heavy initialization | |
| | - Use lazy loading | |
| | - Minimize dependencies | |
| | - Use Layers for shared code | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Lambda Security Checklist
+------------------------------------------------------------------+
| |
| 1. IAM Roles |
| +----------------------------------------------------------+ |
| | [ ] Use least privilege permissions | |
| | [ ] Separate roles for different functions | |
| | [ ] Avoid using '*' in resource policies | |
| +----------------------------------------------------------+ |
| |
| 2. Environment Variables |
| +----------------------------------------------------------+ |
| | [ ] Use KMS encryption for secrets | |
| | [ ] Use AWS Secrets Manager for sensitive data | |
| | [ ] Don't log sensitive information | |
| +----------------------------------------------------------+ |
| |
| 3. VPC Configuration |
| +----------------------------------------------------------+ |
| | [ ] Use VPC for RDS/ElastiCache access | |
| | [ ] Use VPC endpoints for AWS services | |
| | [ ] Configure security groups properly | |
| +----------------------------------------------------------+ |
| |
| 4. Code Security |
| +----------------------------------------------------------+ |
| | [ ] Validate input data | |
| | [ ] Handle errors properly | |
| | [ ] Use parameterized queries | |
| | [ ] Keep dependencies updated | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

ResourceLimit
Memory128 MB - 10,240 MB
Timeout15 minutes
Deployment package (zip)50 MB (direct), 250 MB (S3)
Container image10 GB
Environment variables4 KB
Layers5 layers, 250 MB total
Concurrent executions1,000 (default, adjustable)
/tmp storage512 MB - 10,240 MB

Exam Tip

  1. Execution Model: Cold starts vs warm starts, provisioned concurrency
  2. Triggers: Push vs poll model, synchronous vs asynchronous
  3. Memory: More memory = more CPU power
  4. Timeout: Maximum 15 minutes
  5. Layers: Share code and dependencies across functions
  6. VPC: Required for accessing RDS, ElastiCache in VPC
  7. Reserved Concurrency: Guarantees capacity for critical functions
  8. Event Sources: Know which use push vs poll model

Chapter 9: AWS Elastic Container Services (ECS/EKS)


Last Updated: February 2026