Skip to main content

AWS Lambda Concepts

What is Lambda?​

AWS Lambda is a serverless compute service that runs your code in response to events, without you having to manage servers.

┌─────────────────────────────────────────────────────────────┐
│ LAMBDA vs EC2 │
├─────────────────────────────────────────────────────────────┤
│ │
│ EC2 (Server) Lambda (Serverless) │
│ ───────────── ────────────────── │
│ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Server │ │ Function │ │
│ │ 24/7 │ │ on-demand │ │
│ │ active │ │ │ │
│ └─────────────┘ └─────────────┘ │
│ │
│ • You manage the server • AWS manages infra │
│ • Paid even when idle • Paid per execution │
│ • Manual/ASG scaling • Unlimited auto scaling │
│ • Patching required • Zero maintenance │
│ • Long running OK • Max 15 minutes │
│ │
│ Monthly cost (example): │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ EC2 t3.medium 24/7 = ~$30/month │ │
│ │ Lambda 1M requests = ~$0.20/month │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

Execution Model​

┌─────────────────────────────────────────────────────────────┐
│ LAMBDA EXECUTION MODEL │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. TRIGGER EVENT │
│ ───────────────────── │
│ • API Gateway request │
│ • S3 file upload │
│ • SQS message │
│ • CloudWatch event │
│ • DynamoDB modification │
│ │
│ 2. INVOCATION │
│ ────────── │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────────────────┐ │
│ │ Trigger │───►│ AWS │───►│ Execution │ │
│ │ (event) │ │ Lambda │ │ Environment │ │
│ └─────────┘ │ Service │ │ │ │
│ └─────────┘ │ ┌───────────────┐ │ │
│ │ │ Your code │ │ │
│ │ │ (handler) │ │ │
│ │ └───────────────┘ │ │
│ └─────────────────────┘ │
│ │
│ 3. LIFECYCLE │
│ ──────────── │
│ │
│ Cold Start Warm Terminated │
│ ┌────────┐ ┌────────┐ ┌────────┐ │
│ │ Init │────────►│ Invoke │──(idle)───►│ Shut │ │
│ │ ~100ms │ │ ~10ms │ │ down │ │
│ └────────┘ └────────┘ └────────┘ │
│ │ │
│ ▼ │
│ Reused if new request │
│ arrives quickly │
│ │
└─────────────────────────────────────────────────────────────┘

Lambda Configuration​

Key Parameters​

ParameterDescriptionValues
RuntimeExecution languagePython, Node.js, Java, Go, .NET, Ruby
HandlerEntry pointfile.function
MemoryAllocated RAM128 MB - 10,240 MB
TimeoutMax duration1 sec - 15 min
EnvironmentEnv variablesKey=Value
RoleIAM permissionsRole ARN

Memory and CPU​

┌─────────────────────────────────────────────────────────────┐
│ MEMORY = CPU │
├─────────────────────────────────────────────────────────────┤
│ │
│ In Lambda, memory also determines CPU! │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Memory CPU Use case │ │
│ │ ────── ─── ────────── │ │
│ │ 128 MB ~0.08 vCPU Simple, light API │ │
│ │ 512 MB ~0.33 vCPU Standard │ │
│ │ 1024 MB ~0.58 vCPU Medium processing │ │
│ │ 1769 MB 1 vCPU CPU-intensive │ │
│ │ 3008 MB ~1.75 vCPU Heavy processing │ │
│ │ 10240 MB 6 vCPU Max performance │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ 💡 More memory = more CPU = faster execution │
│ Sometimes, more memory costs LESS! │
│ │
│ Example: │
│ • 128MB × 10s = 1.25 GB-second │
│ • 1024MB × 1.5s = 1.5 GB-second (faster, ~same $) │
│ │
└─────────────────────────────────────────────────────────────┘

Triggers​

Event Sources​

┌─────────────────────────────────────────────────────────────┐
│ LAMBDA TRIGGERS │
├─────────────────────────────────────────────────────────────┤
│ │
│ SYNCHRONOUS (wait for response) │
│ ───────────────────────────────── │
│ • API Gateway → REST/HTTP APIs │
│ • ALB → Load Balancer │
│ • CloudFront → Lambda@Edge │
│ • Cognito → Auth triggers │
│ │
│ ASYNCHRONOUS (fire-and-forget) │
│ ───────────────────────────── │
│ • S3 → Upload, delete │
│ • SNS → Messages │ │
│ • CloudWatch Events → Cron, events │
│ • SES → Emails │
│ │
│ POLLING (Lambda polls the source) │
│ ─────────────────────────────── │
│ • SQS → Messages queue │
│ • DynamoDB Streams → Changes DB │
│ • Kinesis → Streaming data │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ S3 ──────┐ │ │
│ │ │ │ │
│ │ API ──────┼──────► Lambda ──────► DynamoDB │ │
│ │ │ │ │
│ │ SQS ──────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

Code Structure​

Python Handler​

import json

def lambda_handler(event, context):
"""
event : Trigger event data
context : Execution information
"""

# Read event data
print(f"Event: {json.dumps(event)}")

# Context information
print(f"Function: {context.function_name}")
print(f"Request ID: {context.aws_request_id}")
print(f"Time remaining: {context.get_remaining_time_in_millis()}ms")

# Your logic here
name = event.get('name', 'World')
message = f"Hello, {name}!"

# Return the response
return {
'statusCode': 200,
'body': json.dumps({
'message': message
})
}

Node.js Handler​

exports.handler = async (event, context) => {
// Log the event
console.log('Event:', JSON.stringify(event, null, 2));

// Your logic
const name = event.name || 'World';
const message = `Hello, ${name}!`;

// Return the response
return {
statusCode: 200,
body: JSON.stringify({
message: message
})
};
};

IAM Permissions​

┌─────────────────────────────────────────────────────────────┐
│ LAMBDA PERMISSIONS │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. EXECUTION ROLE (what Lambda can do) │
│ ────────────────────────────────────── │
│ IAM role attached to the function │
│ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Typical permissions: │ │
│ │ │ │
│ │ • logs:CreateLogGroup │ │
│ │ • logs:CreateLogStream │ │
│ │ • logs:PutLogEvents │ │
│ │ • s3:GetObject (if S3 access) │ │
│ │ • dynamodb:PutItem (if DDB access) │ │
│ │ • sqs:ReceiveMessage (if SQS trigger) │ │
│ │ │ │
│ └─────────────────────────────────────────────────┘ │
│ │
│ 2. RESOURCE-BASED POLICY (who can invoke Lambda) │
│ ───────────────────────────────────────────────── │
│ Allows other services to call the function │
│ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Example: S3 can invoke this function │ │
│ │ │ │
│ │ Principal: s3.amazonaws.com │ │
│ │ Action: lambda:InvokeFunction │ │
│ │ Condition: bucket = my-bucket │ │
│ │ │ │
│ └─────────────────────────────────────────────────┘ │
│ │
│ Principle of least privilege: │
│ ✅ Minimal necessary permissions │
│ ❌ Never AdministratorAccess on Lambda │
│ │
└─────────────────────────────────────────────────────────────┘

Pricing​

┌─────────────────────────────────────────────────────────────┐
│ LAMBDA PRICING │
├─────────────────────────────────────────────────────────────┤
│ │
│ You pay for: │
│ 1. Number of requests │
│ 2. Execution duration (GB-seconds) │
│ │
│ FREE (Permanent Free Tier): │
│ • 1 million requests/month │
│ • 400,000 GB-seconds/month │
│ │
│ BEYOND FREE TIER: │
│ • $0.20 per million requests │
│ • $0.0000166667 per GB-second │
│ │
│ Example calculation: │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Function: 512 MB, 200ms average, 5M requests/month │ │
│ │ │ │
│ │ GB-seconds = 5M × 0.512 GB × 0.2s = 512,000 │ │
│ │ │ │
│ │ Request cost: (5M - 1M) × $0.20/M = $0.80 │ │
│ │ Duration cost: (512K - 400K) × $0.0000166667 │ │
│ │ = 112K × $0.0000166667 = $1.87 │ │
│ │ │ │
│ │ TOTAL: $2.67/month │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ 💡 Very cost-effective for variable workloads! │
│ │
└─────────────────────────────────────────────────────────────┘

Summary​

ConceptDescription
ServerlessNo server to manage
Event-drivenTriggered by events
HandlerCode entry point
Cold StartInitialization time
Memory = CPUMore RAM = more CPU
15 min maxDuration limit

Next section: Creating Functions