Skip to main content

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​

TriggerDescriptionExample
API GatewayHTTP RequestREST API
S3File uploadImage processing
DynamoDBData changeSync, notification
SQSQueue messageAsync processing
CloudWatch EventsSchedulingCron job
SNSNotificationFan-out

4. Lambda Limits​

LimitValueNotes
Max timeout15 minutesPer execution
Memory128 MB - 10 GBProportional CPU
Package50 MB (zip) / 250 MB (unzipped)Code + dependencies
Concurrency1000 (default)Per account/region
/tmp storage512 MB - 10 GBTemporary

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
  1. Lambda Concepts
  2. Function Creation
  3. Validation Quiz - 30 questions to validate your knowledge
  4. Lab: Lambda

Next week: MODULE 12 - Lambda Troubleshooting