Home β€Ί Uncategorized β€Ί AWS Lambda – Serverless Functions, Event-Driven Architecture, Pricing...
Uncategorized

AWS Lambda – Serverless Functions, Event-Driven Architecture, Pricing Model: 20 Interview Questions

AWS Lambda is a serverless compute service that runs code in response to events without provisioning or managing servers. It’s the foundation of modern event-driven architectures, enabling real-time data processing, API backends, and automation workflows with pay-per-execution pricing.

AWS Lambda: Complete Overview

AWS Lambda is a serverless compute service executing code in response to events. You write code, AWS manages infrastructure, scaling, and patching. Ideal for event-driven workloads: S3 uploads, API requests, scheduled tasks, DynamoDB streams. Supports Python, Node.js, Java, Go, C#, and custom runtimes. Maximum execution time: 15 minutes. Maximum memory: 10GB. You pay only for execution time: $0.0000002 per 100ms (4 million free requests/month, 1 million free GB-seconds).

Lambda Architecture and Execution Model

Lambda Execution Flow:

Event Source (S3, API Gateway, DynamoDB, etc)
↓
Lambda Runtime Environment
β”œβ”€β”€ Cold Start (new container ~1-3 sec)
└── Warm Execution (reused container <100ms) ↓ Function Code Execution β”œβ”€β”€ Handler function invoked β”œβ”€β”€ Context and event processed └── Response returned ↓ Destination (S3, DynamoDB, SNS, etc) Pricing Model: - Requests: $0.20 per 1M requests (First 1M free monthly) - Duration: $0.0000166667 per GB-second (First 400k GB-seconds free monthly) - Provisioned Concurrency: $0.015 per GB-hour - Ephemeral Storage: $0.000000138889 per GB-second Example Cost (1M daily invocations, 512MB, 1 sec): - Requests: (1M/30) * $0.20/1M = $0.007/day - Duration: (1M/30) * 1s * 0.512GB * $0.0000166667 = $0.0089/day - Total: ~$0.02/day = $6/month

Event-Driven Architecture Patterns

Lambda excels in event-driven architectures where actions trigger workflows: S3 object upload β†’ trigger thumbnail generation, API request β†’ validate β†’ process β†’ store result, DynamoDB stream β†’ aggregate metrics, CloudWatch alarm β†’ auto-remediate. Decouples producers from consumers. Scales automatically with event volume. No servers to manage means faster time to market.

Cold Starts and Performance Optimization

Cold start: first invocation after timeout (15+ minutes) creates new container (1-3 seconds overhead). Warm execution reuses container (<100ms). Impacts: critical for APIs (users wait), acceptable for batch jobs. Mitigation: provisioned concurrency ($0.015/GB-hour), connection pooling, language choice (Node.js faster than Java). VPC cold starts are slower (network interface attachment adds 1-2 seconds).

Lambda Limitations and Trade-offs

Max execution time: 15 minutes (not suitable for long-running jobs). Max payload size: 6MB synchronous, 256KB async. No local storage persistence (except /tmp, 512MB ephemeral). Memory-bound: more memory = more CPU = faster execution. Not cost-effective for steady-state workloads (EC2 cheaper for 24/7 processes). Requires refactoring for complex dependencies (size limits).

5 Complete Interview Questions with Solutions

Question 1: Design Event-Driven Data Pipeline Using Lambda

Build pipeline: S3 upload triggers image thumbnail generation, stores in DynamoDB, sends SNS notification. Handle errors, retries, and cost optimization.

import boto3
import json
from PIL import Image
from io import BytesIO

s3_client = boto3.client(‘s3’)
dynamodb = boto3.resource(‘dynamodb’)
sns_client = boto3.client(‘sns’)

def lambda_handler(event, context):
try:
# Parse S3 event
bucket = event[‘Records’][0][‘s3’][‘bucket’][‘name’]
key = event[‘Records’][0][‘s3’][‘object’][‘key’]

# Download image
img_data = s3_client.get_object(Bucket=bucket, Key=key)
img = Image.open(BytesIO(img_data[‘Body’].read()))

# Generate thumbnail
img.thumbnail((150, 150))
thumb_buffer = BytesIO()
img.save(thumb_buffer, format=’PNG’)

# Upload thumbnail
thumb_key = f”thumbnails/{key}”
s3_client.put_object(
Bucket=bucket,
Key=thumb_key,
Body=thumb_buffer.getvalue()
)

# Store metadata in DynamoDB
table = dynamodb.Table(‘ImageMetadata’)
table.put_item(Item={
‘image_id’: key,
‘thumbnail_url’: f”s3://{bucket}/{thumb_key}”,
‘timestamp’: context.aws_request_id
})

# Send SNS notification
sns_client.publish(
TopicArn=’arn:aws:sns:us-east-1:123456789:image-processed’,
Message=f’Thumbnail generated for {key}’
)

return {‘statusCode’: 200, ‘body’: ‘Success’}

except Exception as e:
print(f”Error: {str(e)}”)
# SNS alert on failure
sns_client.publish(
TopicArn=’arn:aws:sns:us-east-1:123456789:error-alerts’,
Message=f’Image processing failed: {str(e)}’
)
raise

Configuration:
Runtime: Python 3.11
Memory: 512 MB
Timeout: 30 seconds
Ephemeral storage: 512 MB
Environment: PILLOW_LAYER_ARN=arn:aws:lambda:…

Cost Analysis (1M images/month):
– Requests: (1M) * $0.20/1M = $0.20
– Duration: (1M * 3s * 512MB / 1024) * $0.0000166667 = $24.00
– Total: ~$25/month

Optimizations:
– Use Lambda layers for dependencies (avoid repackaging)
– Connection pooling for DynamoDB
– Batch operations where possible

Explanation: Event-driven pipeline eliminates polling. S3 trigger automatically invokes Lambda. Error handling prevents silent failures. Cost ~$25/month for 1M monthly invocationsβ€”extremely cost-effective.

AWS – Serverless Architect

Question 2: Optimize Lambda for Cold Starts

API receives 100 requests/hour with 2-minute gaps between requests. Cold starts kill user experience. Design solution: provisioned concurrency vs connection pooling vs language choice.

Option 1: Provisioned Concurrency
aws lambda put-provisioned-concurrency-config \
–function-name my-function \
–provisioned-concurrent-executions 10 \
–qualifier LIVE

Cost: 10 concurrent * 0.512 GB * 730 hours/month * $0.015 = $56/month
Benefit: Eliminates cold starts

Option 2: Connection Pooling
import psycopg2
from psycopg2 import pool

# Create pool outside handler (reused across invocations)
connection_pool = pool.SimpleConnectionPool(
1, 5,
“dbname=mydb user=postgres password=secret host=rds.aws.com”
)

def lambda_handler(event, context):
conn = connection_pool.getconn()
try:
# Use connection
cursor = conn.cursor()
cursor.execute(“SELECT * FROM users”)
finally:
connection_pool.putconn(conn)

Cost: $0 (included in Lambda execution)
Benefit: Faster initialization, reuses DB connections

Option 3: Language Choice
Cold Start Times:
– Python: ~600ms
– Node.js: ~400ms
– Go: ~100ms (fastest)
– Java: ~1200ms (slowest)

Recommendation for 100 req/hour:
– Use connection pooling (free cost benefit)
– Node.js or Go (400-100ms overhead acceptable)
– Provisioned concurrency only if <100ms required Cost-Benefit Trade-off: - No optimization: 100 req/hour * 400ms = 40s/hour overhead - Connection pooling: saves database init (~200ms) = $0 cost - Provisioned concurrency: eliminates all (~100%) but $56/month

Explanation: Cold starts matter for user-facing APIs. Connection pooling is free optimization for frequent requests. Provisioned concurrency expensive ($56/month) unless SLA requires <100ms latency.

Google Cloud – Performance Engineer

Question 3: Lambda Concurrency and Scaling

Function hitting reserved concurrency limits during traffic spikes. 1,000 concurrent requests with 1,000 reserved concurrency. Requests throttled. Design auto-scaling and burst capacity.

Current Configuration:
Reserved Concurrency: 1,000
Unreserved (burst): 1,000 (AWS account default)
Total available: 2,000

Scenario: 3,000 concurrent requests arrive
– First 2,000 execute normally
– Next 1,000 throttled (HTTP 429)

Solution 1: Increase Reserved Concurrency
aws lambda put-function-concurrency \
–function-name my-function \
–reserved-concurrent-executions 3000

Cost: Provisioned concurrency for 3000 concurrent
Monthly: 3000 * 0.512 GB * 730 hours * $0.015 = $16,848

Solution 2: Implement Burst and Queue
# Use SQS for buffering
– API Gateway β†’ SQS queue β†’ Lambda
– Lambda processes at max concurrency (1,000)
– Excess requests wait in queue
– Process time: queue depth / (concurrency * throughput)

Cost: SQS ~$0.40 per 1M requests
Benefit: No throttling, users don’t experience errors

Solution 3: Auto-Scale Reserved Concurrency
aws lambda update-function-concurrency \
–function-name my-function \
–reserved-concurrent-executions 2000

Monitor CloudWatch: Throttles metric
If throttles > threshold, increase reserved concurrency

Recommended: Option 2 (SQS buffering)
– Handles burst without limiting throughput
– Costs ~$0.40/month (vs $16k for provisioned concurrency)
– Users experience queue delay (acceptable vs throttling)

Explanation: Reserved concurrency prevents other functions from stealing capacity. SQS buffering handles bursts cost-effectively. Provisioned concurrency ($16k/month) only for latency-critical workloads.

Databricks – Scaling Specialist

Question 4: Lambda Integration with VPC

Lambda needs access to RDS database in VPC. Design VPC integration, handle cold start penalty, and maintain security with least privilege.

VPC Configuration:
aws lambda update-function-configuration \
–function-name my-function \
–vpc-config SubnetIds=subnet-12345,subnet-67890 \
–security-group-ids sg-12345

Security Group Rules (Lambda β†’ RDS):
– Protocol: TCP
– Port: 5432 (PostgreSQL)
– Source: Lambda security group
– Direction: Egress

RDS Security Group:
– Protocol: TCP
– Port: 5432
– Source: Lambda security group
– Direction: Ingress

VPC Cold Start Penalty:
– Without VPC: 100ms cold start
– With VPC: 1-2 seconds cold start (ENI attachment)
– Mitigation: Provisioned concurrency

Code Example with Connection Pooling:
import psycopg2
from psycopg2 import pool
import os

# Initialize pool (outside handler, persists across invocations)
db_pool = None

def get_db_pool():
global db_pool
if db_pool is None:
db_pool = pool.SimpleConnectionPool(
1, 5,
dbname=os.environ[‘DB_NAME’],
user=os.environ[‘DB_USER’],
password=os.environ[‘DB_PASS’],
host=os.environ[‘DB_HOST’]
)
return db_pool

def lambda_handler(event, context):
conn = get_db_pool().getconn()
try:
cursor = conn.cursor()
cursor.execute(“SELECT * FROM users WHERE id = %s”, (event[‘user_id’],))
result = cursor.fetchone()
return {‘statusCode’: 200, ‘body’: result}
finally:
get_db_pool().putconn(conn)

Cost Impact:
– Without VPC: $0.008/invocation
– With VPC + provisioned concurrency: $0.008 + $0.015/GB-hour

Explanation: VPC Lambda has 1-2 second cold start penalty (ENI attachment). Connection pooling reuses DB connections, saving 200-500ms per invocation. Provisioned concurrency eliminates cold starts ($56/month for 10 concurrent).

Azure – VPC Integration Specialist

Question 5: Lambda Cost Optimization

Portfolio of 100 Lambda functions with varying workloads. Optimize total spend: right-size memory, identify inefficient functions, use reserved capacity. Target: 30% cost reduction.

Cost Optimization Strategy:

1. Right-size Memory Allocation:
Memory determines CPU (linear), cost (linear), execution time (inverse)

Function A: 512MB, 5 sec execution = 2.56 GB-sec cost
Cost: 2.56 * $0.0000166667 = $0.00004

Try 1024MB: 2.5 sec execution = 2.56 GB-sec (same!)
Cost: 2.56 * $0.0000166667 = $0.00004 (same, but faster)

Try 256MB: 10 sec execution = 2.56 GB-sec (same!)
Cost: 2.56 * $0.0000166667 = $0.00004 (same, but slower)

Insight: Execution time dominates. Higher memory speeds up execution,
often without increasing total GB-seconds cost.

2. Identify Inefficient Functions:
– List all functions with CloudWatch logs
– Sort by: Duration * Memory * Invocations
– Focus on top 10% by cost

CloudWatch Insights Query:
fields @duration, @memoryUsed, @maxMemoryUsed
| stats avg(@duration) as avgTime,
max(@maxMemoryUsed) as maxMem,
pct(@duration, 99) as p99Time
| filter pct(@maxMemoryUsed/@memorySize, 100) > 0.8

3. Consolidate Small Functions:
– 50 functions with <100 invocations/month - Consolidate into 5 multi-purpose functions - Savings: Request costs (first 1M free, then $0.20/1M) 4. Batch Invocations: - Instead of 1 invocation/second: - Batch 100 events, invoke 1x/100 seconds - Reduces invocation count 100x - Savings: $0.20 per 1M requests Cost Breakdown (Current): - 100M monthly invocations - Average memory: 512MB - Average duration: 3 seconds - Total GB-seconds: 100M * (512/1024) * 3 = 150M GB-sec - Cost: 100M * $0.20/1M + 150M * $0.0000166667 = $20 + $2,500 = $2,520/month Optimized Configuration: 1. Right-size memory: Reduce avg duration by 20% = $2,500 * 0.8 = $2,000 2. Consolidate functions: Save 40% on requests = $20 * 0.6 = $12 3. Batch invocations: Reduce count 50% = $12 * 0.5 = $6 Total optimized: $2,006/month Savings: $2,520 - $2,006 = $514 (20% reduction) Additional: Reserved Capacity (if predictable) - Save 35% on compute costs - Cost: 150M GB-sec * $0.0000166667 * 0.35 = $87.50/month for reservation - Savings: $2,500 * 0.35 - $87.50 = $787.50 - $87.50 = $700/month - Total optimized: $2,006 - $700 = $1,306/month - Total savings: $2,520 - $1,306 = $1,214 (48% reduction!)

Explanation: Right-sizing memory reduces execution time, often with same or lower GB-seconds cost. Consolidating functions and batching invocations save on request charges. Reserved capacity saves 35% but requires predictable workload.

AWS – Cost Optimization Lead

15 Practice Questions (Test Yourself)

Question 6: Design Lambda function to process 1TB CSV file in S3. 15-minute timeout limit. How would you partition data, use Step Functions, or S3 Select for partial reads?

AWS – Data Processing Architect

Question 7: Implement Lambda layer for shared dependencies (Pandas, NumPy). How would you manage versioning, test compatibility, and minimize cold start impact?

Google Cloud – Dependency Management Specialist

Question 8: Design Lambda with DynamoDB streams for real-time aggregations. Handle late-arriving data, deduplication, and state management in event-driven system.

Databricks – Stream Processing Engineer

Question 9: Implement Lambda@Edge for real-time data transformation at CloudFront. How would you cache responses and handle cache invalidation?

AWS – Edge Computing Specialist

Question 10: Design asynchronous Lambda invocation with SQS/SNS. How would you handle dead-letter queues, retries, and error notifications?

Google Cloud – Async Architecture Specialist

Question 11: Lambda authorization with API Gateway custom authorizer. Implement JWT validation, caching, and performance optimization for 10K req/sec.

AWS – Security Engineer

Question 12: Implement Lambda with RDS Proxy for connection pooling. Compare direct RDS connection vs Proxy for concurrent requests and cost.

Databricks – Database Connection Specialist

Question 13: Design Lambda environment variables and Secrets Manager integration. How would you rotate secrets, audit access, and maintain compliance?

AWS – Secrets Management Lead

Question 14: Troubleshoot Lambda timeout issues with 30-minute processing requirement. Would you use Step Functions, Glue, or EC2? Cost comparison.

Google Cloud – Compute Specialist

Question 15: Implement Lambda with X-Ray tracing for distributed debugging. How would you trace cross-service calls and identify performance bottlenecks?

AWS – Observability Specialist

Question 16: Design Lambda function URLs for direct HTTPS access. When would you use URLs vs API Gateway? Performance and cost trade-offs.

Databricks – API Architecture Lead

Question 17: Implement Lambda with CloudWatch Logs Insights for real-time monitoring. Create custom metrics and alarms for anomaly detection.

AWS – Monitoring Expert

Question 18: Design Lambda for multi-region deployment. Handle cross-region failover, data consistency, and user latency optimization.

Google Cloud – Disaster Recovery Architect

Question 19: Implement Lambda canary deployment using traffic shifting. How would you gradually roll out new versions and rollback on errors?

AWS – DevOps Specialist

Question 20: Design Lambda with container images for complex dependencies. How would you optimize image size, manage ECR costs, and handle versioning?

Databricks – Container Specialist

Master AWS Lambda

Get our comprehensive Data Analytics: Ace All The Interview Concepts course.

Scroll to Top