MODULE 11 - Serverless: AWS Lambda
Overview
This module introduces serverless with AWS Lambda. It's a different paradigm where you have no servers to manage - you just write code that runs on demand.
Summative Assessment
This module includes Assessment 3 (25%) covering Lambda and validation.
LocalStack Supports Lambda!
Good news: LocalStack emulates Lambda well. You will be able to truly practice this module!
Learning Objectives
- Understand the serverless paradigm
- Compare Lambda vs virtual machines
- Create and deploy Lambda functions
- Configure minimal IAM permissions
- Analyze CloudWatch logs
- Test and validate functions
Weekly Content
1. Serverless vs VM
┌─────────────────────────────────────────────────────────────┐
│ SERVERLESS vs VM │
├─────────────────────────────────────────────────────────────┤
│ │
│ EC2 (VM) LAMBDA (Serverless) │
│ ──────── ──────────────────── │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Instance │ │ Function │ │
│ │ (24/7) │ │ (on demand) │ │
│ │ │ │ │ │
│ │ OS + Runtime │ │ Code only │ │
│ │ + Application │ │ │ │
│ │ │ │ Triggered by │ │
│ │ Always UP │ │ event │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ You manage: You manage: │
│ • OS • Code │
│ • Patching • Configuration │
│ • Scaling │
│ • Availability AWS manages the rest! │
│ │
│ Billing: Billing: │
│ • Per hour • Per execution │
│ • Even if idle • $0 if no execution │
│ │
└─────────────────────────────────────────────────────────────┘
2. Anatomy of a Lambda Function
# Python Example
def handler(event, context):
"""
Handler = Entry point of the function
event = Input data (JSON)
context = Runtime info (remaining timeout, memory...)
"""
# Your logic here
name = event.get('name', 'World')
message = f"Hello, {name}!"
# Return (format depends on the trigger)
return {
'statusCode': 200,
'body': message
}
3. Triggers
| Trigger | Description | Example |
|---|---|---|
| API Gateway | HTTP Request | REST API |
| S3 | File upload | Image processing |
| DynamoDB | Data change | Sync, notification |
| SQS | Queue message | Async processing |
| CloudWatch Events | Scheduling | Cron job |
| SNS | Notification | Fan-out |
4. Lambda Limits
| Limit | Value | Notes |
|---|---|---|
| Max timeout | 15 minutes | Per execution |
| Memory | 128 MB - 10 GB | Proportional CPU |
| Package | 50 MB (zip) / 250 MB (unzipped) | Code + dependencies |
| Concurrency | 1000 (default) | Per account/region |
| /tmp storage | 512 MB - 10 GB | Temporary |
Practical Lab with LocalStack
Create a Lambda Function
# 1. Create the function file
cat > lambda_function.py << 'EOF'
def handler(event, context):
name = event.get('name', 'World')
return {
'statusCode': 200,
'body': f'Hello, {name}!'
}
EOF
# 2. Zip it
zip function.zip lambda_function.py
# 3. Create the function on LocalStack
awslocal lambda create-function \
--function-name hello-function \
--runtime python3.9 \
--handler lambda_function.handler \
--zip-file fileb://function.zip \
--role arn:aws:iam::000000000000:role/lambda-role
# 4. Invoke the function
awslocal lambda invoke \
--function-name hello-function \
--payload '{"name": "AWS Student"}' \
output.txt
# 5. View the result
cat output.txt
Test with API Gateway (LocalStack)
# Create a REST API
awslocal apigateway create-rest-api \
--name 'Hello API'
# List the APIs
awslocal apigateway get-rest-apis
Assessment 3 (25%)
Format
- Practical Lab LocalStack (60%)
- Theoretical questions (40%)
Covered Content
- Creation of Lambda functions
- Invocation and tests
- Log reading
- Serverless use cases
- Comparison with EC2
Deliverable
Documented Lambda Function + tests:
- Commented source code
- Invocation screenshots
- Log analysis
- Improvement proposals
Navigation
- Lambda Concepts
- Function Creation
- Validation Quiz - 30 questions to validate your knowledge
- Lab: Lambda
Next week: MODULE 12 - Lambda Troubleshooting