AWS Lambda & Serverless Python: OneHub Interview Masterclass
Canonical File Note: This is the original OneHub-style AWS Lambda guide (254 Q&A, deep conceptual focus). A newer structured guide covering Lambda in the 100/120/80 Core/Intermediate/Advanced format is at
../15-aws-core-services/01-aws-lambda.md. Both files contain different questions β use both for comprehensive preparation.
Q1: How does the AWS Lambda execution environment work, and what happens during a "Cold Start"?β
Technical Answer: When a Lambda function is invoked, AWS allocates an execution environment. If no idle environment exists, a "Cold Start" occurs: AWS downloads your code, starts a new microVM (using Firecracker), initializes the runtime (Python interpreter), and runs your global/initialization code. Subsequent requests use a "Warm" environment, skipping initialization until the environment is reaped (usually after 5-15 mins of inactivity). Code/Syntax Justification:
import boto3
# GLOBAL SCOPE: Runs during Cold Start only.
# Reuse connections to save time on warm starts.
s3 = boto3.client('s3')
def lambda_handler(event, context):
# HANDLER SCOPE: Runs on every invocation.
return {"status": "success"}
π Behind the Question: Testing your understanding of latency, resource lifecycle, and cost optimization. β The Red Flags: Putting heavy DB connection logic inside the handler; ignoring Provisioned Concurrency for latency-sensitive APIs.
Q2: What is the significance of the context object in a Python Lambda handler?β
Technical Answer: The context object provides methods and properties that provide information about the invocation, function, and execution environment. Key attributes include get_remaining_time_in_millis(), function_name, memory_limit_in_mb, and aws_request_id.
Code/Syntax Justification:
def lambda_handler(event, context):
if context.get_remaining_time_in_millis() < 100:
print("Stopping early to avoid timeout!")
return context.aws_request_id
π Behind the Question: Checking if you can handle timeouts gracefully and leverage metadata for logging/tracing. β The Red Flags: Not knowing how to check remaining time; hardcoding timeouts in logic instead of querying the context.
Q3: Explain the 'Event Loop' behavior in a Python Lambda function.β
Technical Answer: Unlike a persistent server, Lambda is single-threaded per execution environment. While you can use asyncio, the environment is "frozen" as soon as the handler returns. Any background threads or pending event loop tasks will pause and only resume if the environment is reused for a subsequent invocation.
Code/Syntax Justification:
import asyncio
async def background_task():
await asyncio.sleep(10) # DANGER: Might never finish
print("Done")
def lambda_handler(event, context):
loop = asyncio.get_event_loop()
loop.create_task(background_task())
return "Done" # Environment freezes HERE.
π Behind the Question: Deep-dive into the "freeze/thaw" cycle of serverless environments.
β The Red Flags: Expecting asyncio.create_task to finish after the handler returns without await or loop.run_until_complete.
Q4: How do you handle secrets (API keys, DB credentials) in AWS Lambda?β
Technical Answer: Never hardcode secrets. Use AWS Systems Manager (SSM) Parameter Store or AWS Secrets Manager. For performance, fetch secrets in the global scope (outside the handler) and implement a simple caching/TTL mechanism to avoid hitting the API on every invocation. Code/Syntax Justification:
import boto3
import os
# Better: Fetch once globally
ssm = boto3.client('ssm')
DB_PASSWORD = None
def get_secret():
global DB_PASSWORD
if not DB_PASSWORD:
resp = ssm.get_parameter(Name='/prod/db/pass', WithDecryption=True)
DB_PASSWORD = resp['Parameter']['Value']
return DB_PASSWORD
π Behind the Question: Security awareness and understanding of API rate limits/costs. β The Red Flags: Using Environment Variables for sensitive data (they are visible in the console); calling Secrets Manager inside the handler loop.
Q5: What is the difference between an 'Event Source Mapping' and a 'Direct Invocation'?β
Technical Answer: Direct Invocations (Sync/Async) happen when a caller (like CLI or SDK) triggers the Lambda. Event Source Mapping (ESM) is an AWS resource that reads from a stream or queue (SQS, Kinesis, DynamoDB Streams) and invokes the Lambda with batches of records. Code/Syntax Justification:
// SQS Event Structure (ESM)
{
"Records": [{ "body": "...", "receiptHandle": "..." }]
}
π Behind the Question: Testing knowledge of Lambda's integration patterns and polling logic. β The Red Flags: Confusing SQS (polled by Lambda service) with SNS (pushes to Lambda).
Q6: How does VPC networking affect Lambda performance and connectivity?β
Technical Answer: Lambda functions by default run in a service-managed VPC with internet access. If you need to access private resources (RDS, ElastiCache) in your VPC, you must provide VPC config (Subnets, Security Groups). AWS now uses Hyperplane ENIs to minimize the "VPC Cold Start" penalty, but you lose default internet access unless you route through a NAT Gateway.
Code/Syntax Justification:
No specific code, but requires IAM policy AWSLambdaVPCAccessExecutionRole.
π Behind the Question: Network architecture skills and troubleshooting "Lambda can't reach the internet" issues.
β The Red Flags: Thinking VPC attachment still adds minutes to cold starts (it's now seconds); forgetting a NAT Gateway for external API calls from within a VPC.
Q7: Explain 'Reserved Concurrency' vs 'Provisioned Concurrency'.β
Technical Answer: Reserved Concurrency acts as a ceiling (limit) and a guarantee (pool) for a specific function, ensuring it doesn't get throttled by other functions and vice versa. Provisioned Concurrency keeps a specified number of execution environments initialized ("warm") to eliminate cold starts. Code/Syntax Justification: Configured via SAM/Terraform:
MyFunction:
Type: AWS::Serverless::Function
Properties:
ReservedConcurrentExecutions: 10
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 5
π Behind the Question: Scalability management and cost/performance trade-offs. β The Red Flags: Using Provisioned Concurrency for functions that are rarely called (it's expensive); not setting Reserved Concurrency for functions that hit downstream DB limits.
Q8: How do you implement Error Handling and Retries for Asynchronous Lambda invocations?β
Technical Answer: For Async invocations (e.g., SNS, S3 Events), Lambda automatically retries twice by default. You should configure a Dead Letter Queue (DLQ) or Lambda Destinations (onFailure) to capture events that fail all retries. Code/Syntax Justification:
def lambda_handler(event, context):
try:
process(event)
except Exception as e:
# If this is Async, Lambda service will retry this whole function
raise e
π Behind the Question: Reliability engineering in distributed systems. β The Red Flags: Catching all exceptions and returning 200 (this prevents the Lambda service from retrying async events).
Q9: What are Lambda Layers and when should you use them?β
Technical Answer: Layers are ZIP archives containing libraries, custom runtimes, or config. They allow you to share code across multiple functions, keeping your deployment package small. However, they can make local testing harder and lead to "dependency hell" if not versioned strictly.
Code/Syntax Justification:
Folder structure for Python: python/lib/python3.x/site-packages/
π Behind the Question: Code modularity and deployment optimization.
β The Red Flags: Putting frequently changing application logic in a layer; ignoring the 250MB unzipped limit.
Q10: Explain the 'Lambda Power Tuning' concept.β
Technical Answer: Lambda memory allocation (128MB to 10GB) linearly scales CPU and Network bandwidth. "Power Tuning" is the process of testing different memory settings to find the "sweet spot" where performance is highest and cost is lowestβoften, increasing memory reduces cost because the function finishes significantly faster.
Code/Syntax Justification:
Memory settings are in template.yaml: MemorySize: 1024
π Behind the Question: Cost optimization and performance benchmarking.
β The Red Flags: Always using 128MB to "save money"; not realizing that 1792MB is the equivalent of 1 full vCPU.
Section 2: IAM Roles & Permissionsβ
Q11: How do you configure IAM roles for Lambda functions?β
Technical Answer: Every Lambda function executes under an IAM execution role. The role grants least-privilege permissions β only the specific AWS services and resources the function needs. Attach managed policies for common access (e.g., AWSLambdaBasicExecutionRole for CloudWatch Logs) and inline policies for custom resources.
Code/Syntax Justification:
# SAM template.yaml
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Role: !GetAtt MyFunctionRole.Arn
MyFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: DynamoDBAccess
PolicyDocument:
Statement:
- Effect: Allow
Action: ["dynamodb:GetItem", "dynamodb:PutItem"]
Resource: !GetAtt MyTable.Arn
π Behind the Question: Security posture β IAM misconfiguration is the #1 serverless security issue.
β The Red Flags: Attaching AdministratorAccess to a Lambda role "to make it work."
Q12: How do you grant cross-account access to a Lambda function?β
Technical Answer: Use resource-based policies on the Lambda function (who can invoke it) combined with the execution role (what it can access). For cross-account DynamoDB, the function's execution role must trust the target account, and the target account's resource policy must allow access. Code/Syntax Justification:
# Allow account B to invoke function in account A
aws lambda add-permission \
--function-name my-function \
--statement-id cross-account-invoke \
--action lambda:InvokeFunction \
--principal 123456789012 \ # Account B
--source-arn arn:aws:iam::123456789012:role/invoker-role
# Assume role in account B from Lambda execution role
import boto3
sts = boto3.client('sts')
credentials = sts.assume_role(
RoleArn='arn:aws:iam::123456789012:role/cross-account-role',
RoleSessionName='lambda-session'
)['Credentials']
π Behind the Question: Multi-account AWS architecture patterns. β The Red Flags: Embedding access keys in Lambda environment variables for cross-account access β use role assumption instead.
Q13: What is the difference between Lambda execution role and resource-based policy?β
Technical Answer: The execution role defines what the Lambda function can DO (outbound permissions β which AWS services it can call). The resource-based policy defines who can INVOKE the function (inbound permissions β which services, accounts, or principals can trigger it). Code/Syntax Justification:
Execution Role (outbound): Lambda β S3, DynamoDB, SQS, SNS
Resource Policy (inbound): API Gateway, SNS, S3, EventBridge β Lambda
# Resource-based policy example (allow S3 to invoke)
aws lambda add-permission \
--function-name my-function \
--statement-id s3-trigger \
--action lambda:InvokeFunction \
--principal s3.amazonaws.com \
--source-arn arn:aws:s3:::my-bucket \
--source-account 111122223333
π Behind the Question: IAM model fluency β two distinct permission planes. β The Red Flags: Confusing the two β trying to grant S3 access by modifying the resource-based policy instead of the execution role.
Q14: How do you use AWS Secrets Manager with Lambda securely?β
Technical Answer: Store secrets in Secrets Manager and retrieve them at Lambda cold start (not per invocation). Cache the secret in the function's global scope to avoid per-invocation API calls. Use the Lambda extension for Secrets Manager to cache automatically with TTL. Code/Syntax Justification:
import boto3
import json
# Module-level cache β retrieved once per container lifetime
_secrets_cache = {}
def get_secret(secret_name: str) -> dict:
if secret_name in _secrets_cache:
return _secrets_cache[secret_name]
client = boto3.client('secretsmanager')
response = client.get_secret_value(SecretId=secret_name)
secret = json.loads(response['SecretString'])
_secrets_cache[secret_name] = secret
return secret
# Usage
def handler(event, context):
db_creds = get_secret('prod/myapp/db')
conn = connect_db(db_creds['username'], db_creds['password'])
π Behind the Question: Secrets management without environment variable exposure.
β The Red Flags: Calling get_secret_value on every invocation β adds 20-50ms latency and unnecessary Secrets Manager API costs.
Q15: How do you implement least-privilege IAM for Lambda using SAM policy templates?β
Technical Answer: AWS SAM provides pre-built policy templates that expand to least-privilege IAM policies. Use DynamoDBCrudPolicy, S3ReadPolicy, SQSPollerPolicy, etc. instead of broad managed policies.
Code/Syntax Justification:
# SAM policy templates
Resources:
ProcessOrderFunction:
Type: AWS::Serverless::Function
Properties:
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
- S3ReadPolicy:
BucketName: !Ref AssetsBucket
- SQSSendMessagePolicy:
QueueName: !GetAtt NotificationQueue.QueueName
- CloudWatchPutMetricPolicy: {}
# Expands to minimal IAM β no overpermissioning
π Behind the Question: Security automation and IaC best practices.
β The Red Flags: Writing Action: "*" in Lambda IAM policies β violates least privilege, fails SOC 2 audits.
Section 3: SQS + Lambda Integrationβ
Q16: How does SQS batch processing work with Lambda?β
Technical Answer: SQS triggers Lambda with a batch of messages (up to 10,000 with batch windows). Lambda processes each message; for partial failures, use ReportBatchItemFailures β return only the failed messageIds so SQS only re-queues those messages, not the entire batch.
Code/Syntax Justification:
def handler(event, context):
failed_ids = []
for record in event['Records']:
message_id = record['messageId']
try:
body = json.loads(record['body'])
process_order(body)
except Exception as e:
logger.error(f"Failed to process {message_id}: {e}")
failed_ids.append({'itemIdentifier': message_id})
# Return failed IDs β only those are re-queued
return {'batchItemFailures': failed_ids}
# SAM configuration
Globals:
Function:
EventInvokeConfig:
MaximumRetryAttempts: 2
Events:
SQSTrigger:
Type: SQS
Properties:
Queue: !GetAtt MyQueue.Arn
BatchSize: 100
FunctionResponseTypes:
- ReportBatchItemFailures
π Behind the Question: Efficient SQS batch processing without re-processing successful messages. β The Red Flags: Raising an exception on one failure β entire batch re-queued, causing 99 successful messages to be processed again.
Q17: How do you tune SQS batch window and batch size for Lambda?β
Technical Answer: BatchSize (1β10,000) controls max messages per invocation. MaximumBatchingWindowInSeconds (0β300) tells Lambda to wait up to N seconds to fill a batch before invoking. Use batch windows to reduce Lambda invocation costs when messages trickle in.
Code/Syntax Justification:
Events:
SQSTrigger:
Type: SQS
Properties:
Queue: !GetAtt MyQueue.Arn
BatchSize: 1000 # Up to 1000 messages per invocation
MaximumBatchingWindowInSeconds: 30 # Wait 30s to accumulate a full batch
FunctionResponseTypes:
- ReportBatchItemFailures
π Behind the Question: Cost optimization and throughput maximization.
β The Red Flags: BatchSize: 1 (default was 10) β 1,000 invocations instead of 10 for the same 1,000 messages.
Q18: How do you implement dead letter queues (DLQ) for SQS-triggered Lambda?β
Technical Answer: Configure a DLQ on the SQS source queue (not on the Lambda function) for SQS-triggered Lambdas. Messages that fail after maxReceiveCount retries are moved to the DLQ. Set up CloudWatch alerts on DLQ depth.
Code/Syntax Justification:
Resources:
MainQueue:
Type: AWS::SQS::Queue
Properties:
RedrivePolicy:
deadLetterTargetArn: !GetAtt DLQueue.Arn
maxReceiveCount: 3 # Move to DLQ after 3 failures
DLQueue:
Type: AWS::SQS::Queue
Properties:
MessageRetentionPeriod: 1209600 # 14 days
DLQAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
MetricName: ApproximateNumberOfMessagesVisible
Namespace: AWS/SQS
Dimensions:
- Name: QueueName
Value: !GetAtt DLQueue.QueueName
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions: [!Ref AlertTopic]
π Behind the Question: Production reliability β poisoned messages need a safe landing zone. β The Red Flags: No DLQ β failed messages loop indefinitely, consuming Lambda capacity.
Q19: How do you control SQS-Lambda concurrency to avoid overwhelming a downstream database?β
Technical Answer: Set a Lambda reserved concurrency limit equal to your database connection pool size. This ensures Lambda never opens more concurrent DB connections than the pool supports. For RDS, use RDS Proxy as a connection pooler between Lambda and the database. Code/Syntax Justification:
# SAM: limit concurrency to protect DB
ProcessOrderFunction:
Type: AWS::Serverless::Function
Properties:
ReservedConcurrentExecutions: 10 # Max 10 parallel executions = max 10 DB connections
# Even better: use RDS Proxy
DBProxy:
Type: AWS::RDS::DBProxy
Properties:
DBProxyName: my-lambda-proxy
EngineFamily: MYSQL
RoleArn: !GetAtt ProxyRole.Arn
Auth:
- AuthScheme: SECRETS
SecretArn: !Ref DBSecret
VpcSubnetIds: !Ref PrivateSubnets
π Behind the Question: Database connection management in serverless architectures. β The Red Flags: Unlimited Lambda concurrency hammering RDS with 3,000 connections β database crashes.
Q20: How do you implement SQS FIFO queues with Lambda for ordered processing?β
Technical Answer: SQS FIFO queues guarantee ordering within a MessageGroupId. Lambda processes one message group at a time (sequential within group, parallel across groups). Use one MessageGroupId per entity (e.g., per user or per order) that needs ordered processing.
Code/Syntax Justification:
# Enqueue with FIFO
sqs.send_message(
QueueUrl=FIFO_QUEUE_URL,
MessageBody=json.dumps(event),
MessageGroupId=f"user-{user_id}", # Order guaranteed per user
MessageDeduplicationId=str(uuid.uuid4()), # Required for FIFO
)
# Lambda processes groups sequentially β no code change needed
def handler(event, context):
for record in event['Records']:
# Messages within same MessageGroupId arrive in order
process(json.loads(record['body']))
π Behind the Question: Event ordering guarantees in serverless systems. β The Red Flags: Using standard SQS when order matters β messages arrive out of order and state machine breaks.
Section 4: Kinesis + Lambda Integrationβ
Q21: How does Kinesis Data Streams integrate with Lambda?β
Technical Answer: Lambda polls Kinesis shards via an event source mapping. It processes records shard-by-shard, in order, with checkpointing via sequence numbers. One Lambda invocation per shard per polling cycle. Failed batches block the shard until resolved or the retention period expires. Code/Syntax Justification:
def handler(event, context):
for record in event['Records']:
# Kinesis records are base64-encoded
payload = base64.b64decode(record['kinesis']['data'])
data = json.loads(payload)
shard_id = record['eventID'].split(':')[0]
sequence = record['kinesis']['sequenceNumber']
process_stream_event(data)
# SAM configuration
KinesisTrigger:
Type: Kinesis
Properties:
Stream: !GetAtt MyStream.Arn
StartingPosition: LATEST
BatchSize: 100
BisectBatchOnFunctionError: true # Split batch to find poison pill
DestinationConfig:
OnFailure:
Destination: !GetAtt FailureSQS.Arn
π Behind the Question: Real-time stream processing architecture.
β The Red Flags: Ignoring BisectBatchOnFunctionError β one poison pill blocks an entire shard indefinitely.
Q22: What is Kinesis checkpointing and how does Lambda handle it?β
Technical Answer: Kinesis uses sequence numbers as checkpoints β the last successfully processed position. Lambda's event source mapping manages checkpoints automatically: it advances the checkpoint only when the Lambda function returns successfully. On failure, it retries from the last checkpoint, guaranteeing at-least-once processing. Code/Syntax Justification:
def handler(event, context):
failed_batch_items = []
for record in event['Records']:
try:
process(json.loads(base64.b64decode(record['kinesis']['data'])))
except Exception as e:
failed_batch_items.append({
'itemIdentifier': record['kinesis']['sequenceNumber']
})
# Return failed sequence numbers for partial batch failure
return {'batchItemFailures': failed_batch_items}
π Behind the Question: Stream processing delivery semantics.
β The Red Flags: Not using ReportBatchItemFailures for Kinesis β entire batch retried on any single record failure.
Q23: How do you scale Kinesis + Lambda for high-throughput streams?β
Technical Answer: Scale by increasing Kinesis shard count (each shard = 1 MB/s in, 2 MB/s out, 1 Lambda invocation in parallel). Use enhanced fan-out (RegisterStreamConsumer) to give Lambda its own dedicated 2 MB/s per shard read throughput, avoiding sharing with other consumers.
Code/Syntax Justification:
# Add shards to Kinesis stream
aws kinesis update-shard-count \
--stream-name my-stream \
--target-shard-count 10 \
--scaling-type UNIFORM_SCALING
# Enhanced fan-out registration
aws kinesis register-stream-consumer \
--stream-arn arn:aws:kinesis:us-east-1:123456789012:stream/my-stream \
--consumer-name lambda-consumer
# SAM enhanced fan-out
KinesisTrigger:
Type: Kinesis
Properties:
Stream: !GetAtt MyStream.Arn
StartingPosition: LATEST
ParallelizationFactor: 10 # 10 concurrent invocations per shard
π Behind the Question: Kinesis capacity planning and throughput optimization. β The Red Flags: One shard for 10 MB/s of data β guaranteed throttling.
Q24: How do you handle Kinesis iterator expiration in Lambda?β
Technical Answer: Kinesis iterators expire after 5 minutes of inactivity or 24 hours (7 days with enhanced retention). Lambda's event source mapping handles iterator renewal automatically. The risk is a backed-up shard blocking Lambda from advancing past a problematic record β use BisectBatchOnFunctionError and MaximumRetryAttempts.
Code/Syntax Justification:
KinesisTrigger:
Type: Kinesis
Properties:
Stream: !GetAtt MyStream.Arn
StartingPosition: LATEST
BisectBatchOnFunctionError: true # Binary search for bad record
MaximumRetryAttempts: 3 # Don't retry forever
MaximumRecordAgeInSeconds: 3600 # Skip records older than 1 hour
DestinationConfig:
OnFailure:
Destination: !GetAtt FailureSQS.Arn # Dead letter for failed records
π Behind the Question: Operational resilience in stream processing. β The Red Flags: Infinite retries on a malformed record β shard blocked, stream falls further and further behind.
Section 5: Step Functions Integrationβ
Q25: What is AWS Step Functions and when should you use it over Celery Canvas?β
Technical Answer: Step Functions is a fully managed serverless workflow orchestrator for Lambda functions. Use it over Celery Canvas when: you need visual workflow tracing, built-in error handling at each step, wait states (pause for days), human approval steps, or when each step is a separate Lambda function. Celery Canvas is better for Python-internal task orchestration within a single service. Code/Syntax Justification:
{
"Comment": "Order processing workflow",
"StartAt": "ValidateOrder",
"States": {
"ValidateOrder": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123:function:validate-order",
"Next": "ChargePayment",
"Catch": [{ "ErrorEquals": ["ValidationError"], "Next": "NotifyFailure" }]
},
"ChargePayment": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123:function:charge-payment",
"Next": "ShipOrder",
"Retry": [
{
"ErrorEquals": ["PaymentTimeout"],
"MaxAttempts": 3,
"IntervalSeconds": 5
}
]
},
"ShipOrder": { "Type": "Task", "Resource": "...", "End": true },
"NotifyFailure": { "Type": "Task", "Resource": "...", "End": true }
}
}
π Behind the Question: Workflow orchestration tool selection. β The Red Flags: Building complex multi-step workflows with raw Lambda calling Lambda directly β no visibility, no retry, no error handling.
Q26: What is the difference between Step Functions Standard and Express workflows?β
Technical Answer: Standard Workflows: max 1 year duration, at-least-once execution, full execution history, suitable for long-running business processes. Express Workflows: max 5 minutes, at-least-once (async) or at-most-once (sync), higher throughput (100k/s), lower cost for high-frequency short workflows. Code/Syntax Justification:
Standard Workflow:
- Duration: up to 1 year
- Execution: at-least-once
- Price: $0.025 per 1,000 state transitions
- Use: order fulfillment, document approval
Express Workflow (Async):
- Duration: up to 5 minutes
- Execution: at-least-once
- Price: $1.00 per 1M executions + duration
- Use: IoT data processing, streaming transformations
Express Workflow (Sync):
- Duration: up to 5 minutes
- Execution: at-most-once
- Use: API Gateway β sync response from multi-step workflow
π Behind the Question: Cost optimization and architectural fit. β The Red Flags: Using Standard Workflows for 10,000 IoT events/sec β prohibitive cost.
Q27: How do you implement the Map state in Step Functions for parallel processing?β
Technical Answer: The Map state iterates over an array, running the same workflow branch for each item β analogous to Celery's group. You can run iterations in parallel (up to MaxConcurrency limit) and aggregate results.
Code/Syntax Justification:
{
"ProcessItems": {
"Type": "Map",
"ItemsPath": "$.items",
"MaxConcurrency": 10,
"Iterator": {
"StartAt": "ProcessSingleItem",
"States": {
"ProcessSingleItem": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:function:process-item",
"End": true
}
}
},
"Next": "AggregateResults"
}
}
π Behind the Question: Parallel processing patterns in Step Functions. β The Red Flags: Using a loop with sequential invocations when parallel Map would reduce total time from 100s to 10s.
Q28: How do you pass data between Step Functions states efficiently?β
Technical Answer: Step Functions pass the entire state as JSON between states, limited to 256 KB. For large payloads: store data in S3 and pass the S3 key between states. Use InputPath, ResultPath, and OutputPath to filter what data flows between steps.
Code/Syntax Justification:
{
"ProcessLargeFile": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:function:process-file",
"Parameters": {
"s3_key.$": "$.s3_key", // Pass only the S3 key, not file contents
"user_id.$": "$.user_id"
},
"ResultPath": "$.processing_result", // Merge result into state, not replace
"OutputPath": "$.processing_result", // Only pass result to next state
"Next": "NotifyUser"
}
}
π Behind the Question: Step Functions data flow management. β The Red Flags: Trying to pass 10 MB files as JSON state β hits 256 KB limit and fails.
Q29: How do you implement human approval workflows with Step Functions?β
Technical Answer: Use a Task state with a callback token (waitForTaskToken). The Lambda sends the token to an email/Slack, pauses the workflow, and resumes when a human calls SendTaskSuccess or SendTaskFailure with the token β the workflow can wait up to 1 year.
Code/Syntax Justification:
{
"WaitForApproval": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke.waitForTaskToken",
"Parameters": {
"FunctionName": "send-approval-email",
"Payload": {
"taskToken.$": "$$.Task.Token",
"requestDetails.$": "$"
}
},
"HeartbeatSeconds": 86400,
"Next": "ProcessApproval"
}
}
# Approval endpoint (called when human clicks Approve/Reject)
def approve_handler(task_token: str, approved: bool):
if approved:
sfn.send_task_success(taskToken=task_token, output=json.dumps({'approved': True}))
else:
sfn.send_task_failure(taskToken=task_token, error='Rejected', cause='Manual rejection')
π Behind the Question: Human-in-the-loop workflow patterns. β The Red Flags: Polling a database from Lambda every minute to check for approval β wasteful and not serverless.
Q30: How do you implement distributed sagas with Step Functions?β
Technical Answer: Model each saga step as a Task state with a corresponding compensating Lambda. Use Catch blocks to trigger compensation on failure, running compensating transactions in reverse order to maintain consistency.
Code/Syntax Justification:
{
"ReserveInventory": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:function:reserve-inventory",
"Next": "ChargePayment",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "CompensateInventory"
}
]
},
"ChargePayment": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:function:charge-payment",
"Next": "ShipOrder",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "CompensatePaymentAndInventory"
}
]
},
"CompensatePaymentAndInventory": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "RefundPayment",
"States": {
"RefundPayment": { "Type": "Task", "Resource": "...", "End": true }
}
},
{
"StartAt": "ReleaseInventory",
"States": {
"ReleaseInventory": { "Type": "Task", "Resource": "...", "End": true }
}
}
],
"End": true
}
}
π Behind the Question: Distributed transaction management in serverless architectures. β The Red Flags: Trying to use a single DB transaction across multiple Lambda functions β not possible without a distributed coordinator.
Section 6: Lambda Performance Optimizationβ
Q31: How do you optimize Lambda cold starts for Python?β
Technical Answer: Cold starts are the container initialization cost. Reduce them by: (1) keeping deployment packages small (use Lambda layers for large deps), (2) using arm64 architecture (faster init, cheaper), (3) enabling Lambda SnapStart (for Java, not Python), (4) using provisioned concurrency for latency-critical paths, (5) moving heavy imports outside the handler.
Code/Syntax Justification:
# BAD: imports inside handler (re-imported every invocation)
def handler(event, context):
import boto3 # Slow
import pandas # Very slow
return process()
# GOOD: module-level imports (execute once per container lifetime)
import boto3
import pandas # Loaded once during cold start
s3 = boto3.client('s3') # Connection reused across invocations
def handler(event, context):
return process()
π Behind the Question: Latency optimization for user-facing Lambda functions. β The Red Flags: A 5-second cold start for a user-facing API endpoint due to a 100 MB deployment package.
Q32: What is Lambda Provisioned Concurrency and when should you use it?β
Technical Answer: Provisioned Concurrency pre-warms N Lambda instances β they're initialized and ready to handle requests instantly, eliminating cold starts. Cost: you pay for provisioned concurrency even when idle. Use for: SLA-critical endpoints, user-facing APIs with p99 latency requirements, or predictable traffic patterns. Code/Syntax Justification:
# SAM: provisioned concurrency on alias
MyFunctionAlias:
Type: AWS::Lambda::Alias
Properties:
FunctionName: !Ref MyFunction
FunctionVersion: !GetAtt MyFunction.Version
Name: production
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 10
# Auto-scaling provisioned concurrency
MyFunctionScaling:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
ServiceNamespace: lambda
ResourceId: !Sub function:${MyFunction}:production
ScalableDimension: lambda:function:ProvisionedConcurrency
MinCapacity: 5
MaxCapacity: 50
π Behind the Question: Balancing cold start elimination against cost. β The Red Flags: Using provisioned concurrency for a batch job that runs once a day β enormous unnecessary cost.
Q33: How do you optimize Lambda memory and CPU allocation?β
Technical Answer: Lambda allocates CPU proportionally to memory. At 1,769 MB you get exactly 1 vCPU. Above that, you get fractional additional vCPUs. Use AWS Lambda Power Tuning to empirically find the memory setting that minimizes cost Γ duration. More memory often reduces cost by completing faster. Code/Syntax Justification:
# Lambda Power Tuning tool (open source)
# Runs your function at 10+ memory settings and reports cost/performance
# Rule of thumb:
# 128 MB β ~12% of 1 vCPU (sluggish for CPU tasks)
# 512 MB β ~29% of 1 vCPU
# 1769 MB β exactly 1 vCPU (inflection point)
# 3008 MB β ~1.7 vCPU
# 10240 MB β ~6 vCPU
# For a CPU-intensive function:
# 128 MB: 10s Γ $0.000000208 = $0.00000208 per invocation
# 3008 MB: 0.5s Γ $0.00000487 = $0.00000244 per invocation (faster AND cheaper!)
π Behind the Question: Cost and performance optimization of Lambda functions. β The Red Flags: Always using 128 MB "to save money" β can be more expensive AND slower.
Q34: How do you implement Lambda response streaming?β
Technical Answer: Lambda response streaming (available for Node.js 18+ and Python 3.12+) allows the function to send chunks of the response as they're generated, instead of buffering the entire response. Ideal for AI-generated text streaming, large file downloads, or any progressive rendering use case. Code/Syntax Justification:
import json
# Python 3.12 response streaming
def handler(event, context):
# streaming_body is a file-like object
def generate_chunks():
for chunk in generate_llm_response(event['prompt']):
yield chunk.encode('utf-8')
return {
'statusCode': 200,
'headers': {'Content-Type': 'text/plain'},
'body': generate_chunks(),
'isBase64Encoded': False,
}
# AWS CLI: invoke with streaming
aws lambda invoke-with-response-stream \
--function-name my-streaming-function \
--payload '{"prompt": "Hello"}' \
output.txt
π Behind the Question: Modern Lambda features for real-time AI applications. β The Red Flags: Buffering a 100 MB file in Lambda memory before returning β hits 6 MB response limit.
Q35: How do you implement Lambda function URLs as a lightweight API?β
Technical Answer: Lambda Function URLs provide a dedicated HTTPS endpoint for a Lambda function without API Gateway. Supports AWS_IAM or NONE auth, CORS configuration, and response streaming. Ideal for simple webhooks, single-function APIs, and cost-sensitive use cases (no API Gateway cost).
Code/Syntax Justification:
# SAM: Lambda Function URL
MyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionUrlConfig:
AuthType: NONE # Public endpoint
Cors:
AllowOrigins: ["https://myapp.com"]
AllowMethods: ["GET", "POST"]
AllowHeaders: ["Content-Type", "Authorization"]
# Output the URL
Outputs:
FunctionUrl:
Value: !GetAtt MyFunctionUrl.FunctionUrl
π Behind the Question: Cost-effective API alternatives to API Gateway. β The Red Flags: Adding API Gateway for a single-function webhook β unnecessary $3.50/million requests overhead.
Section 7: Lambda Observabilityβ
Q36: How do you implement structured logging in Lambda?β
Technical Answer: Use Python's logging module with JSON formatting (via python-json-logger). Include request_id, function_name, user_id, and correlation IDs in every log line. CloudWatch Logs Insights can query structured JSON logs efficiently.
Code/Syntax Justification:
import logging
import json
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class LambdaLogger:
def __init__(self, context):
self.base = {
'request_id': context.aws_request_id,
'function': context.function_name,
'version': context.function_version,
}
def info(self, msg, **kwargs):
logger.info(json.dumps({**self.base, 'message': msg, **kwargs}))
def error(self, msg, **kwargs):
logger.error(json.dumps({**self.base, 'message': msg, 'level': 'ERROR', **kwargs}))
def handler(event, context):
log = LambdaLogger(context)
log.info("Processing request", user_id=event.get('user_id'))
# Query in CloudWatch Logs Insights:
# fields @timestamp, message, user_id | filter level = "ERROR" | sort @timestamp desc
π Behind the Question: Production debugging without SSH access.
β The Red Flags: print("Something happened") β unqueryable, no context, useless at scale.
Q37: How do you implement distributed tracing for Lambda with AWS X-Ray?β
Technical Answer: Enable X-Ray active tracing on Lambda. Use the aws_xray_sdk to instrument downstream calls (DynamoDB, S3, HTTP). X-Ray creates a service map showing latency between Lambda β DynamoDB β external APIs, making bottlenecks instantly visible.
Code/Syntax Justification:
from aws_xray_sdk.core import xray_recorder, patch_all
patch_all() # Auto-instrument boto3, requests, etc.
@xray_recorder.capture('process_order')
def process_order(order_id: str):
with xray_recorder.in_subsegment('validate'):
validate(order_id)
with xray_recorder.in_subsegment('save_to_dynamo'):
save(order_id)
def handler(event, context):
xray_recorder.begin_segment('my-function')
try:
process_order(event['order_id'])
finally:
xray_recorder.end_segment()
# SAM: enable X-Ray
Globals:
Function:
Tracing: Active
π Behind the Question: Distributed tracing in serverless architectures. β The Red Flags: No tracing β a 500ms API call is slow and you have no idea if it's Lambda init, DynamoDB, or an external API.
Q38: How do you use CloudWatch Lambda Insights for performance monitoring?β
Technical Answer: Lambda Insights is a monitoring solution that collects system-level metrics (CPU utilization, memory used, init duration, number of cold starts) from a Lambda extension layer. It provides pre-built dashboards and integrates with CloudWatch alarms. Code/Syntax Justification:
# SAM: enable Lambda Insights
Globals:
Function:
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:38
Policies:
- CloudWatchLambdaInsightsExecutionRolePolicy
# Key metrics available:
# init_duration β cold start time
# used_memory_max β peak memory usage
# cpu_total_time β CPU time consumed
# cold_start β 1 if cold start, 0 if warm
π Behind the Question: Memory and performance profiling for Lambda functions. β The Red Flags: Guessing memory allocation without data β over-provisioning costs money, under-provisioning causes OOM.
Q39: How do you implement custom CloudWatch metrics from Lambda?β
Technical Answer: Use boto3 to publish custom metrics via put_metric_data. For efficiency, use the Embedded Metrics Format (EMF) β log JSON with _aws.CloudWatchMetrics metadata, and CloudWatch automatically extracts metrics without API calls.
Code/Syntax Justification:
import json
# Embedded Metrics Format (EMF) β zero API call overhead
def emit_metric(metric_name: str, value: float, unit: str = 'Count'):
print(json.dumps({
"_aws": {
"Timestamp": int(time.time() * 1000),
"CloudWatchMetrics": [{
"Namespace": "MyApp",
"Dimensions": [["Environment", "FunctionName"]],
"Metrics": [{"Name": metric_name, "Unit": unit}]
}]
},
"Environment": "production",
"FunctionName": context.function_name,
metric_name: value
}))
def handler(event, context):
start = time.time()
result = process(event)
emit_metric("ProcessingDuration", (time.time() - start) * 1000, "Milliseconds")
emit_metric("OrdersProcessed", len(event.get('orders', [])), "Count")
π Behind the Question: Business-level observability beyond system metrics.
β The Red Flags: Calling cloudwatch.put_metric_data() per invocation β adds latency and CloudWatch API costs.
Q40: How do you set up Lambda dead letter queues for async invocations?β
Technical Answer: For asynchronous Lambda invocations (SNS, S3 events, EventBridge), configure a DLQ (SQS or SNS) on the function itself. If Lambda fails after 2 retries (default), the event is sent to the DLQ. For synchronous invocations (API Gateway), DLQ is not applicable β the caller handles errors. Code/Syntax Justification:
# SAM: Lambda DLQ for async failures
MyFunction:
Type: AWS::Serverless::Function
Properties:
DeadLetterQueue:
Type: SQS
TargetArn: !GetAtt MyDLQ.Arn
EventInvokeConfig:
MaximumRetryAttempts: 2 # Retry twice before DLQ
MaximumEventAgeInSeconds: 3600 # Discard events older than 1h
DestinationConfig:
OnSuccess:
Destination: !GetAtt SuccessQueue.Arn
OnFailure:
Destination: !GetAtt MyDLQ.Arn
π Behind the Question: Async invocation reliability and failure handling. β The Red Flags: No DLQ on async Lambda β S3 trigger failures silently drop events forever.
Section 8: Lambda Architecture Patternsβ
Q41: What is the Lambda Powertools library and how does it improve development?β
Technical Answer: AWS Lambda Powertools for Python is an opinionated set of utilities: structured logging (with correlation IDs), tracing (X-Ray integration), metrics (EMF), idempotency (with DynamoDB), batch processing (SQS/Kinesis), and more. It eliminates boilerplate and applies best practices automatically. Code/Syntax Justification:
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger(service="order-service")
tracer = Tracer(service="order-service")
metrics = Metrics(namespace="MyApp", service="order-service")
@logger.inject_lambda_context(correlation_id_path="requestContext.requestId")
@tracer.capture_lambda_handler
@metrics.log_metrics(capture_cold_start_metric=True)
def handler(event: dict, context: LambdaContext):
metrics.add_metric(name="OrdersProcessed", unit=MetricUnit.Count, value=1)
logger.info("Processing order", extra={"order_id": event["order_id"]})
return process_order(event)
π Behind the Question: Production-grade Lambda development without reinventing the wheel. β The Red Flags: Manually implementing correlation IDs, EMF metrics, and X-Ray tracing when Powertools does it in 3 decorators.
Q42: How do you implement the Lambda idempotency pattern with Powertools?β
Technical Answer: The Powertools idempotency utility stores a hash of the event in DynamoDB with a TTL. If the same event arrives again (within TTL), it returns the cached result without re-executing the function β guaranteeing exactly-once business logic despite at-least-once delivery. Code/Syntax Justification:
from aws_lambda_powertools.utilities.idempotency import (
idempotent, DynamoDBPersistenceLayer, IdempotencyConfig
)
persistence = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
config = IdempotencyConfig(
event_key_jmespath="body.order_id", # Deduplicate by order_id
expires_after_seconds=3600,
)
@idempotent(config=config, persistence_store=persistence)
def handler(event, context):
order_id = json.loads(event['body'])['order_id']
charge_customer(order_id) # Safe β only executes once per order_id
return {"statusCode": 200, "body": "Payment processed"}
π Behind the Question: Exactly-once semantics for critical business operations. β The Red Flags: Charging a customer twice because SQS retried the Lambda after a timeout.
Q43: How do you implement Lambda blue/green deployments?β
Technical Answer: Use Lambda aliases and weighted traffic shifting. Point the production alias at v1 (100%). Deploy v2, then shift traffic gradually (10% β 50% β 100%) using alias routing configuration. CloudWatch alarms auto-rollback if error rates spike.
Code/Syntax Justification:
# SAM deployment preference
MyFunction:
Type: AWS::Serverless::Function
Properties:
AutoPublishAlias: production
DeploymentPreference:
Type: Canary10Percent5Minutes # 10% for 5 min, then 100%
Alarms:
- !Ref FunctionErrorRateAlarm
Hooks:
PreTraffic: !Ref PreTrafficHook # Run integration tests before shift
PostTraffic: !Ref PostTrafficHook # Validate after shift
# Traffic shift types:
# Canary10Percent5Minutes
# Linear10PercentEvery1Minute
# AllAtOnce (no gradual shift)
π Behind the Question: Safe deployments for production Lambda functions.
β The Red Flags: Deploying directly to production with AllAtOnce and no rollback strategy β one bad deploy takes down all traffic.
Q44: How do you handle VPC networking for Lambda functions?β
Technical Answer: Lambda in a VPC gets ENIs (Elastic Network Interfaces) in your private subnets, enabling access to RDS, ElastiCache, and private resources. Tradeoffs: slightly longer cold starts (ENI attachment, though AWS now uses Hyperplane ENIs which are much faster), and internet access requires a NAT Gateway. Code/Syntax Justification:
# SAM: VPC configuration
MyFunction:
Type: AWS::Serverless::Function
Properties:
VpcConfig:
SecurityGroupIds:
- !Ref LambdaSecurityGroup
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
# Security group: allow outbound to RDS, deny all inbound
LambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Lambda security group
VpcId: !Ref VPC
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 5432
ToPort: 5432
DestinationSecurityGroupId: !Ref RDSSecurityGroup
π Behind the Question: Secure private resource access from Lambda. β The Red Flags: Putting RDS in a public subnet so Lambda can reach it without VPC β database exposed to internet.
Q45: What are Lambda Extensions and how are they used?β
Technical Answer: Lambda Extensions run as separate processes alongside the function, sharing the execution environment. Used for: telemetry collection (Datadog, New Relic agents), secrets caching (AWS Parameters and Secrets Lambda Extension), security scanning, and custom logging pipelines β without modifying function code. Code/Syntax Justification:
# SAM: add extensions as layers
MyFunction:
Type: AWS::Serverless::Function
Properties:
Layers:
# Datadog extension for metrics/tracing
- !Sub arn:aws:lambda:${AWS::Region}:464622532012:layer:Datadog-Extension:51
# AWS Parameters and Secrets extension for caching
- !Sub arn:aws:lambda:${AWS::Region}:177933569100:layer:AWS-Parameters-and-Secrets-Lambda-Extension:11
Environment:
Variables:
DD_API_KEY_SECRET_ARN: !Ref DatadogAPIKey
PARAMETERS_SECRETS_EXTENSION_CACHE_ENABLED: true
PARAMETERS_SECRETS_EXTENSION_CACHE_SIZE: 1000
π Behind the Question: Lambda ecosystem and third-party integration patterns. β The Red Flags: Making a Secrets Manager API call on every invocation when the extension would cache it for the container's lifetime.
Section 9: Cost Optimizationβ
Q46: How do you calculate and optimize Lambda costs?β
Technical Answer: Lambda pricing: (1) per-request: $0.20/million; (2) duration: GB-seconds Γ $0.0000166667. Optimize by: right-sizing memory, minimizing cold starts (reduce init work), using ARM/Graviton2 (20% cheaper), batching SQS messages, using S3 event filtering to avoid unnecessary invocations. Code/Syntax Justification:
# Cost calculation example
# 1 million invocations Γ 512 MB Γ 200ms average duration
# GB-seconds = 1,000,000 Γ (512/1024) Γ 0.2 = 100,000
# Duration cost = 100,000 Γ $0.0000166667 = $1.67
# Request cost = 1,000,000 Γ $0.0000002 = $0.20
# Total = $1.87/month
# ARM64 savings (20% cheaper duration cost)
MemorySize: 512
Architectures: [arm64] # Save 20% on duration, 20% faster performance
π Behind the Question: FinOps awareness and cost-conscious architecture. β The Red Flags: 10,240 MB allocation for a function that reads 100 bytes from DynamoDB β 80x overprovisioned.
Q47: How do you use Lambda Savings Plans for cost reduction?β
Technical Answer: AWS Compute Savings Plans offer up to 17% discount on Lambda costs in exchange for a 1 or 3-year commitment to a minimum spend ($/hour). They apply automatically across Lambda regions and apply to both x86 and arm64. Best for stable, predictable Lambda workloads. Code/Syntax Justification:
# Savings Plan math:
# On-Demand Lambda: $1,000/month
# 1-year Savings Plan commitment: $850/month (15% discount)
# 3-year Savings Plan commitment: $830/month (17% discount)
# Purchase via AWS Cost Explorer:
# AWS Console β Cost Explorer β Savings Plans β Purchase Savings Plan
# Type: Compute Savings Plan (covers Lambda, Fargate, EC2)
π Behind the Question: Enterprise cost optimization and commitment discounts. β The Red Flags: Paying on-demand rates for a Lambda function that processes 100M invocations/month.
Q48: How do you avoid Lambda timeout-related cost waste?β
Technical Answer: Set function Timeout to slightly above the p99 execution time (not the maximum possible). Over-generous timeouts waste money when functions hang waiting for an unresponsive downstream β they consume GB-seconds until timeout. Use X-Ray to measure actual p99 duration.
Code/Syntax Justification:
# BAD: 15-minute timeout on a function that takes 200ms normally
MyFunction:
Timeout: 900 # If it hangs, burns 900 seconds Γ memory cost
# GOOD: set timeout to 2x p99 execution time
MyFunction:
Timeout: 5 # p99 = 2s, timeout at 5s β early fail, no waste
# Set downstream timeouts shorter than Lambda timeout
def handler(event, context):
remaining_ms = context.get_remaining_time_in_millis()
requests.get(url, timeout=(remaining_ms - 500) / 1000) # Always leave 500ms buffer
π Behind the Question: Cost efficiency and failure fast patterns. β The Red Flags: 15-minute timeout on a 200ms function β a hung invocation wastes $0.000015 instead of $0.0000003.
Section 10: Testing Lambda Functionsβ
Q49: How do you unit test Lambda handlers with mocked AWS services?β
Technical Answer: Use moto to mock AWS services (DynamoDB, S3, SQS) in unit tests. Pass synthetic API Gateway events constructed with aws-lambda-powertools event factories or raw dicts. No real AWS credentials or deployed resources needed.
Code/Syntax Justification:
import pytest
import boto3
from moto import mock_dynamodb
from myapp.handler import handler
@mock_dynamodb
def test_handler_creates_order():
# Setup mock DynamoDB
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.create_table(
TableName='orders',
KeySchema=[{'AttributeName': 'order_id', 'KeyType': 'HASH'}],
AttributeDefinitions=[{'AttributeName': 'order_id', 'AttributeType': 'S'}],
BillingMode='PAY_PER_REQUEST',
)
# Synthetic API Gateway event
event = {
'body': json.dumps({'order_id': 'ord-123', 'total': 99.99}),
'requestContext': {'requestId': 'test-req-id'},
}
result = handler(event, MockContext())
assert result['statusCode'] == 200
item = table.get_item(Key={'order_id': 'ord-123'})['Item']
assert item['total'] == Decimal('99.99')
π Behind the Question: Testing serverless functions without real AWS services. β The Red Flags: Integration tests that hit real AWS β slow, costs money, fails in CI without credentials.
Q50: How do you load test Lambda functions effectively?β
Technical Answer: Use Artillery, Locust, or AWS's own load testing solution. For async invocations, use SQS or SNS to flood the function. Key metrics to capture: cold start rate, p50/p99/p99.9 latency, error rate, throttle count (429s), and concurrent execution count. Code/Syntax Justification:
# artillery.yml - load test Lambda via API Gateway
config:
target: https://api.example.com
phases:
- duration: 60
arrivalRate: 10 # 10 users/sec for 1 minute
- duration: 120
arrivalRate: 100 # Ramp to 100 users/sec
- duration: 60
arrivalRate: 500 # Spike test
scenarios:
- flow:
- post:
url: /process-order
json:
order_id: "{{ $randomString() }}"
π Behind the Question: Capacity validation and performance testing of serverless functions. β The Red Flags: Never load testing before launch β discovering Lambda concurrency limits under production traffic.
Section 11: API Gateway + Lambda Patternsβ
Q51: What is the difference between API Gateway REST API and HTTP API?β
Technical Answer: REST API is the original, feature-rich option with usage plans, API keys, request validation, WAF integration, and caching. HTTP API is newer, cheaper (~70% less), faster (~40% lower latency), and supports JWT authorizers natively. Use HTTP API unless you need REST-only features. Code/Syntax Justification:
# SAM HTTP API (simpler, cheaper)
MyApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: prod
CorsConfiguration:
AllowOrigins: ["https://myapp.com"]
AllowMethods: ["GET", "POST"]
# SAM REST API (more features)
MyRestApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
ApiKeyRequired: true
UsagePlan:
Throttle:
BurstLimit: 100
RateLimit: 50
π Behind the Question: Cost-conscious API design. β The Red Flags: Using REST API for a simple CRUD backend when HTTP API would save 70% on gateway costs.
Q52: How do you implement request validation in API Gateway?β
Technical Answer: REST API supports request validators that check body, query parameters, and headers against JSON Schema models before invoking Lambda. This rejects malformed requests at the gateway level β Lambda is never invoked, saving cost and reducing attack surface. Code/Syntax Justification:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
Models:
CreateOrderModel:
type: object
required: [product_id, quantity]
properties:
product_id: { type: string, minLength: 1 }
quantity: { type: integer, minimum: 1, maximum: 100 }
CreateOrder:
Type: AWS::Serverless::Function
Properties:
Events:
Post:
Type: Api
Properties:
Path: /orders
Method: post
RequestModel:
Model: CreateOrderModel
Required: true
ValidateBody: true
π Behind the Question: Defense-in-depth and cost efficiency. β The Red Flags: Validating every request inside Lambda β paying for invocations that should have been rejected at the gateway.
Q53: How do you implement Lambda authorizers for custom auth logic?β
Technical Answer: Lambda authorizers (formerly custom authorizers) run a Lambda function before the API handler to authenticate/authorize requests. Two types: Token-based (validates a bearer token) and Request-based (accesses full request context including headers, query params, IP). Returns an IAM policy that API Gateway caches. Code/Syntax Justification:
def authorizer_handler(event, context):
token = event.get('authorizationToken', '').replace('Bearer ', '')
try:
claims = verify_jwt(token)
return generate_policy(claims['sub'], 'Allow', event['methodArn'], claims)
except Exception:
return generate_policy('unauthorized', 'Deny', event['methodArn'])
def generate_policy(principal_id, effect, resource, context=None):
return {
'principalId': principal_id,
'policyDocument': {
'Version': '2012-10-17',
'Statement': [{
'Action': 'execute-api:Invoke',
'Effect': effect,
'Resource': resource,
}]
},
'context': context or {}, # Passed to downstream Lambda
}
π Behind the Question: Custom authentication integration with API Gateway. β The Red Flags: No caching on the authorizer β re-validates JWT on every single request, adding latency and Lambda cost.
Q54: How do you handle API Gateway throttling and rate limiting?β
Technical Answer: API Gateway provides built-in throttling at account, stage, route, and method levels. Configure ThrottlingBurstLimit (max concurrent requests) and ThrottlingRateLimit (sustained requests/sec). Usage plans with API keys allow per-customer rate limits.
Code/Syntax Justification:
# Per-stage throttling
MyApi:
Type: AWS::Serverless::Api
Properties:
MethodSettings:
- HttpMethod: "*"
ResourcePath: "/*"
ThrottlingBurstLimit: 200
ThrottlingRateLimit: 100
# Per-customer usage plan
UsagePlan:
Type: AWS::ApiGateway::UsagePlan
Properties:
Throttle:
BurstLimit: 50
RateLimit: 25
Quota:
Limit: 10000
Period: MONTH
π Behind the Question: API protection and fair usage enforcement. β The Red Flags: No throttling β one abusive client exhausts your account-level Lambda concurrency, taking down all functions.
Q55: How do you implement WebSocket APIs with API Gateway and Lambda?β
Technical Answer: API Gateway WebSocket API maintains persistent connections. Lambda handles three routes: $connect (auth + save connectionId), $disconnect (cleanup), and $default (message handling). Use DynamoDB to store active connectionIds and push messages via ApiGatewayManagementApi.post_to_connection().
Code/Syntax Justification:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('WebSocketConnections')
def connect_handler(event, context):
connection_id = event['requestContext']['connectionId']
table.put_item(Item={'connectionId': connection_id})
return {'statusCode': 200}
def message_handler(event, context):
sender_id = event['requestContext']['connectionId']
body = json.loads(event['body'])
domain = event['requestContext']['domainName']
stage = event['requestContext']['stage']
apigw = boto3.client('apigatewaymanagementapi',
endpoint_url=f'https://{domain}/{stage}')
connections = table.scan()['Items']
for conn in connections:
if conn['connectionId'] != sender_id:
apigw.post_to_connection(
ConnectionId=conn['connectionId'],
Data=json.dumps(body).encode()
)
return {'statusCode': 200}
π Behind the Question: Real-time communication in serverless architectures. β The Red Flags: Polling an API endpoint every second for updates instead of using WebSockets β 86,400 unnecessary Lambda invocations per user per day.
Q56: How do you implement binary file uploads via API Gateway and Lambda?β
Technical Answer: API Gateway can handle binary content (images, PDFs) when configured with binary media types. For large files (>10MB), bypass API Gateway entirely using S3 pre-signed URLs β Lambda generates the URL, client uploads directly to S3. Code/Syntax Justification:
# Pre-signed URL approach (recommended for files >6MB)
import boto3
s3 = boto3.client('s3')
def generate_upload_url(event, context):
file_key = f"uploads/{uuid.uuid4()}/{event['queryStringParameters']['filename']}"
presigned_url = s3.generate_presigned_url(
'put_object',
Params={
'Bucket': 'my-uploads-bucket',
'Key': file_key,
'ContentType': event['queryStringParameters'].get('content_type', 'application/octet-stream'),
},
ExpiresIn=300, # 5 minutes
)
return {
'statusCode': 200,
'body': json.dumps({'upload_url': presigned_url, 'file_key': file_key}),
}
# Client-side: PUT directly to S3 using the pre-signed URL
# fetch(presignedUrl, { method: 'PUT', body: file, headers: { 'Content-Type': contentType } })
π Behind the Question: Large file handling in serverless architectures. β The Red Flags: Streaming a 500MB file through Lambda β hits the 6MB request/response payload limit.
Q57: How do you implement API versioning with Lambda and API Gateway?β
Technical Answer: Three strategies: (1) URL path: /v1/users, /v2/users β clearest, most common. (2) Header: Accept: application/vnd.myapi.v2+json. (3) Query param: /users?version=2. Use Lambda aliases or separate functions per version.
Code/Syntax Justification:
# SAM: URL path versioning with separate functions
V1GetUsers:
Type: AWS::Serverless::Function
Properties:
Handler: v1.users.get_handler
Events:
Api:
Type: HttpApi
Properties:
Path: /v1/users
Method: GET
V2GetUsers:
Type: AWS::Serverless::Function
Properties:
Handler: v2.users.get_handler
Events:
Api:
Type: HttpApi
Properties:
Path: /v2/users
Method: GET
# Or use Lambda alias-based versioning
# /users β Lambda alias "production" β version 5 (v1 behavior)
# /v2/users β Lambda alias "v2" β version 8 (v2 behavior)
π Behind the Question: API lifecycle management and backward compatibility. β The Red Flags: Breaking API changes without versioning β every client integration breaks simultaneously.
Q58: How do you implement CORS correctly with API Gateway?β
Technical Answer: For HTTP API, set CorsConfiguration on the API resource. For REST API, add CORS headers to both the Lambda response AND configure an OPTIONS mock integration. Lambda must always return Access-Control-Allow-Origin in its response headers.
Code/Syntax Justification:
# Lambda response with CORS headers
def handler(event, context):
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': 'https://myapp.com',
'Access-Control-Allow-Headers': 'Content-Type,Authorization',
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE',
'Access-Control-Allow-Credentials': 'true',
},
'body': json.dumps({'data': 'result'}),
}
# SAM HTTP API CORS config
MyApi:
Type: AWS::Serverless::HttpApi
Properties:
CorsConfiguration:
AllowOrigins: ["https://myapp.com"]
AllowMethods: ["GET", "POST", "PUT", "DELETE"]
AllowHeaders: ["Content-Type", "Authorization"]
AllowCredentials: true
MaxAge: 3600
π Behind the Question: Browser security model and API accessibility.
β The Red Flags: Access-Control-Allow-Origin: * with Allow-Credentials: true β security violation, browsers reject this combination.
Q59: How do you implement request/response transformation in API Gateway?β
Technical Answer: REST API supports Velocity Template Language (VTL) mapping templates to transform request/response payloads between the client format and Lambda format. HTTP API uses payload format version 2.0 which simplifies the Lambda event structure. Code/Syntax Justification:
# HTTP API payload format v2.0 (simpler Lambda event)
MyFunction:
Type: AWS::Serverless::Function
Properties:
Events:
Api:
Type: HttpApi
Properties:
PayloadFormatVersion: "2.0"
# v2.0 event structure (cleaner than v1.0):
# {
# "requestContext": {"http": {"method": "GET", "path": "/users"}},
# "queryStringParameters": {"page": "1"},
# "body": "...",
# "isBase64Encoded": false
# }
π Behind the Question: API Gateway integration patterns. β The Red Flags: Complex VTL templates that nobody can maintain β use Lambda proxy integration and handle transformation in code.
Q60: How do you implement a multi-stage API Gateway deployment?β
Technical Answer: Use stages (dev, staging, prod) with stage variables to parameterize Lambda function aliases, DynamoDB table names, and environment-specific configuration. Each stage gets its own URL and can have independent throttling, logging, and caching settings. Code/Syntax Justification:
# SAM: stage-specific configuration
Globals:
Function:
Environment:
Variables:
TABLE_NAME: !Sub "${AWS::StackName}-orders"
STAGE: !Ref Stage
Parameters:
Stage:
Type: String
AllowedValues: [dev, staging, prod]
Default: dev
Conditions:
IsProd: !Equals [!Ref Stage, prod]
Resources:
MyApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: !Ref Stage
MyFunction:
Type: AWS::Serverless::Function
Properties:
ProvisionedConcurrencyConfig:
!If [
IsProd,
{ ProvisionedConcurrentExecutions: 10 },
!Ref "AWS::NoValue",
]
π Behind the Question: Environment management for serverless APIs. β The Red Flags: Single stage for everything β dev testing accidentally hits production DynamoDB tables.
Section 12: DynamoDB + Lambda Patternsβ
Q61: How do you design a DynamoDB table for Lambda-based applications?β
Technical Answer: Design for access patterns first, not entity relationships. Use single-table design with composite keys (PK, SK), GSIs for alternate access patterns, and sparse indexes. DynamoDB is the natural database for Lambda β no connection pooling needed, instant scaling.
Code/Syntax Justification:
# Single-table design for e-commerce
# PK=USER#123, SK=PROFILE β User profile
# PK=USER#123, SK=ORDER#2024-001 β User's order
# PK=ORDER#2024-001, SK=ITEM#1 β Order item
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('MyApp')
# Get user profile
profile = table.get_item(Key={'PK': 'USER#123', 'SK': 'PROFILE'})
# Get all orders for a user
orders = table.query(
KeyConditionExpression='PK = :pk AND begins_with(SK, :sk)',
ExpressionAttributeValues={':pk': 'USER#123', ':sk': 'ORDER#'}
)
π Behind the Question: NoSQL data modeling for serverless. β The Red Flags: Creating 10 DynamoDB tables for a simple app β one well-designed table handles all access patterns.
Q62: How do you implement DynamoDB transactions with Lambda?β
Technical Answer: TransactWriteItems provides ACID transactions across up to 100 items in multiple tables. Use for operations that must succeed or fail atomically: order creation + inventory decrement, user creation + team membership.
Code/Syntax Justification:
dynamodb = boto3.client('dynamodb')
def create_order_transactionally(user_id, order_id, product_id, quantity):
dynamodb.transact_write_items(
TransactItems=[
{
'Put': {
'TableName': 'MyApp',
'Item': {
'PK': {'S': f'ORDER#{order_id}'},
'SK': {'S': 'DETAILS'},
'user_id': {'S': user_id},
'product_id': {'S': product_id},
'quantity': {'N': str(quantity)},
},
}
},
{
'Update': {
'TableName': 'MyApp',
'Key': {'PK': {'S': f'PRODUCT#{product_id}'}, 'SK': {'S': 'INVENTORY'}},
'UpdateExpression': 'SET stock = stock - :qty',
'ConditionExpression': 'stock >= :qty',
'ExpressionAttributeValues': {':qty': {'N': str(quantity)}},
}
},
]
)
π Behind the Question: Atomic operations in NoSQL databases.
β The Red Flags: Two separate put_item and update_item calls β if the second fails, inventory is decremented without a valid order.
Q63: How do you implement DynamoDB Streams with Lambda for event-driven processing?β
Technical Answer: DynamoDB Streams captures table changes (INSERT, MODIFY, REMOVE) as an ordered stream. Lambda polls the stream and processes changes β enabling event sourcing, materialized views, cross-region replication, and real-time analytics. Code/Syntax Justification:
def stream_handler(event, context):
for record in event['Records']:
event_name = record['eventName'] # INSERT, MODIFY, REMOVE
new_image = record['dynamodb'].get('NewImage', {})
old_image = record['dynamodb'].get('OldImage', {})
if event_name == 'INSERT':
index_to_elasticsearch(new_image)
elif event_name == 'MODIFY':
if status_changed(old_image, new_image):
send_notification(new_image)
elif event_name == 'REMOVE':
remove_from_search_index(old_image)
# SAM: DynamoDB Stream trigger
OrdersTable:
Type: AWS::DynamoDB::Table
Properties:
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
StreamProcessor:
Type: AWS::Serverless::Function
Properties:
Events:
Stream:
Type: DynamoDB
Properties:
Stream: !GetAtt OrdersTable.StreamArn
StartingPosition: TRIM_HORIZON
BatchSize: 100
BisectBatchOnFunctionError: true
π Behind the Question: Event-driven architectures with DynamoDB.
β The Red Flags: Polling DynamoDB with scan() every minute for changes β expensive, slow, misses real-time events.
Q64: How do you implement TTL-based data expiration in DynamoDB?β
Technical Answer: DynamoDB TTL automatically deletes items after a specified Unix timestamp. Use for session tokens, temporary data, cache entries, and GDPR-compliant data retention. Deletions are free (no WCU cost) but happen within ~48 hours of expiry (not instant). Code/Syntax Justification:
import time
# Set TTL when creating item
table.put_item(Item={
'PK': f'SESSION#{session_id}',
'SK': 'DATA',
'user_id': user_id,
'data': session_data,
'ttl': int(time.time()) + 3600, # Expires in 1 hour
})
# DynamoDB table TTL configuration
# aws dynamodb update-time-to-live \
# --table-name MyApp \
# --time-to-live-specification Enabled=true,AttributeName=ttl
π Behind the Question: Automatic data lifecycle management. β The Red Flags: Running a daily Lambda to scan and delete expired items β expensive, TTL does this for free.
Q65: How do you handle DynamoDB pagination in Lambda?β
Technical Answer: DynamoDB limits query/scan results to 1MB. If there's more data, the response includes LastEvaluatedKey. Pass it as ExclusiveStartKey in the next request to continue. For API responses, return the key as a cursor token to the client.
Code/Syntax Justification:
def get_paginated_orders(user_id: str, cursor: str = None, limit: int = 20):
params = {
'KeyConditionExpression': 'PK = :pk AND begins_with(SK, :sk)',
'ExpressionAttributeValues': {':pk': f'USER#{user_id}', ':sk': 'ORDER#'},
'Limit': limit,
'ScanIndexForward': False, # Newest first
}
if cursor:
params['ExclusiveStartKey'] = json.loads(base64.b64decode(cursor))
response = table.query(**params)
next_cursor = None
if 'LastEvaluatedKey' in response:
next_cursor = base64.b64encode(json.dumps(response['LastEvaluatedKey']).encode()).decode()
return {
'items': response['Items'],
'next_cursor': next_cursor,
}
π Behind the Question: Efficient data retrieval patterns. β The Red Flags: Loading all items with a scan and paginating in Python β scans entire table, O(n) cost.
Section 13: EventBridge + Lambdaβ
Q66: What is Amazon EventBridge and how does it integrate with Lambda?β
Technical Answer: EventBridge is a serverless event bus that routes events from AWS services, SaaS integrations, and custom applications to Lambda targets based on rules. It decouples producers from consumers β producers publish events to a bus, rules filter and route to targets. Code/Syntax Justification:
# SAM: EventBridge rule triggers Lambda
OrderEventRule:
Type: AWS::Events::Rule
Properties:
EventBusName: default
EventPattern:
source: ['myapp.orders']
detail-type: ['OrderCreated']
detail:
total: [{"numeric": [">=", 1000]}] # Only high-value orders
Targets:
- Arn: !GetAtt NotifyVIPTeamFunction.Arn
Id: notify-vip
# Publishing custom event
import boto3
events = boto3.client('events')
events.put_events(Entries=[{
'Source': 'myapp.orders',
'DetailType': 'OrderCreated',
'Detail': json.dumps({'order_id': 'ord-123', 'total': 1500}),
'EventBusName': 'default',
}])
π Behind the Question: Event-driven serverless architecture. β The Red Flags: Directly invoking Lambda functions from other Lambda functions β creates tight coupling, no retry logic, no filtering.
Q67: How do you implement scheduled Lambda functions with EventBridge?β
Technical Answer: EventBridge Scheduler (or CloudWatch Events schedule expression) triggers Lambda on a fixed schedule. Supports rate() expressions (every N minutes/hours) and cron() expressions. EventBridge Scheduler adds one-time schedules, flexible time windows, and timezone support.
Code/Syntax Justification:
# SAM: Scheduled Lambda
CleanupFunction:
Type: AWS::Serverless::Function
Properties:
Events:
EveryHour:
Type: Schedule
Properties:
Schedule: rate(1 hour)
Description: Cleanup expired sessions
DailyReport:
Type: Schedule
Properties:
Schedule: cron(0 8 * * ? *) # 8 AM UTC daily
Description: Generate daily report
# EventBridge Scheduler (one-time future execution)
scheduler = boto3.client('scheduler')
scheduler.create_schedule(
Name=f'reminder-{user_id}',
ScheduleExpression=f'at({future_time.isoformat()})',
Target={'Arn': LAMBDA_ARN, 'Input': json.dumps({'user_id': user_id})},
FlexibleTimeWindow={'Mode': 'OFF'},
)
π Behind the Question: Cron job replacement in serverless. β The Red Flags: Running an EC2 instance 24/7 just for a cron job that executes 5 seconds per hour.
Q68: How do you implement event replay and dead letter handling in EventBridge?β
Technical Answer: EventBridge Archives store events for replay. When a target fails, EventBridge retries with exponential backoff for up to 24 hours. Configure a DLQ (SQS) on the rule target for events that exhaust retries. Use event replay to reprocess failed events after fixing the bug. Code/Syntax Justification:
# Archive for event replay
OrderEventArchive:
Type: AWS::Events::Archive
Properties:
SourceArn: !GetAtt OrderEventBus.Arn
EventPattern:
source: ["myapp.orders"]
RetentionDays: 30
# Rule target with DLQ and retry
OrderEventRule:
Type: AWS::Events::Rule
Properties:
Targets:
- Arn: !GetAtt ProcessOrderFunction.Arn
Id: process-order
DeadLetterConfig:
Arn: !GetAtt OrderDLQ.Arn
RetryPolicy:
MaximumRetryAttempts: 3
MaximumEventAgeInSeconds: 3600
# Replay archived events
aws events start-replay \
--replay-name "fix-order-processing" \
--event-source-arn arn:aws:events:...:archive/OrderEventArchive \
--event-start-time 2026-04-20T00:00:00Z \
--event-end-time 2026-04-21T00:00:00Z \
--destination Arn=arn:aws:events:...:event-bus/OrderEventBus
π Behind the Question: Event-driven system resilience and recovery. β The Red Flags: Failed events silently dropped β no DLQ, no archive, no ability to replay.
Q69: How do you implement cross-account event routing with EventBridge?β
Technical Answer: EventBridge supports cross-account event routing by adding a resource-based policy to the target account's event bus, allowing the source account to put events. Use aws.partner events for SaaS integrations or custom event buses for multi-account architectures.
Code/Syntax Justification:
# Target account: allow source account to put events
TargetEventBusPolicy:
Type: AWS::Events::EventBusPolicy
Properties:
EventBusName: !Ref SharedEventBus
StatementId: AllowCrossAccountPut
Action: events:PutEvents
Principal: "111122223333" # Source account ID
# Source account: forward events to target account
CrossAccountRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source: ["myapp.orders"]
Targets:
- Arn: !Sub "arn:aws:events:${AWS::Region}:444455556666:event-bus/SharedEventBus"
Id: cross-account-forward
RoleArn: !GetAtt CrossAccountRole.Arn
π Behind the Question: Multi-account AWS architecture patterns. β The Red Flags: Sharing IAM credentials between accounts for event routing instead of using EventBridge's native cross-account support.
Q70: How do you implement content-based filtering in EventBridge rules?β
Technical Answer: EventBridge supports content-based filtering with pattern matching: exact values, prefix matching, numeric comparisons, IP address ranges, exists checks, and anything-but exclusions. This reduces Lambda invocations by filtering at the bus level.
Code/Syntax Justification:
{
"source": ["myapp.orders"],
"detail-type": ["OrderCreated"],
"detail": {
"status": ["confirmed", "pending"],
"total": [{ "numeric": [">=", 100, "<=", 10000] }],
"region": [{ "anything-but": ["us-gov-west-1"] }],
"customer": {
"tier": [{ "prefix": "enterprise" }],
"verified": [true],
"discount_code": [{ "exists": true }]
}
}
}
π Behind the Question: Efficient event routing without unnecessary Lambda invocations. β The Red Flags: Routing all events to Lambda and filtering inside the function β paying for 10,000 invocations when only 100 match.
Section 14: Lambda Security Advancedβ
Q71: How do you implement environment variable encryption for Lambda?β
Technical Answer: Lambda encrypts environment variables at rest with AWS KMS by default. For sensitive values, use a customer-managed KMS key and encrypt values before setting them. The function decrypts at runtime using boto3.client('kms').decrypt(). Better yet, use Secrets Manager or SSM Parameter Store.
Code/Syntax Justification:
import boto3
import base64
kms = boto3.client('kms')
# Decrypt KMS-encrypted env var at cold start (cache result)
_decrypted_cache = {}
def get_decrypted_env(key: str) -> str:
if key not in _decrypted_cache:
encrypted = base64.b64decode(os.environ[key])
_decrypted_cache[key] = kms.decrypt(CiphertextBlob=encrypted)['Plaintext'].decode()
return _decrypted_cache[key]
def handler(event, context):
db_password = get_decrypted_env('DB_PASSWORD_ENCRYPTED')
# Use db_password...
π Behind the Question: Secrets management in serverless. β The Red Flags: Plaintext database passwords in environment variables visible in the Lambda console.
Q72: How do you implement WAF protection for API Gateway + Lambda?β
Technical Answer: AWS WAF (Web Application Firewall) attaches to API Gateway REST APIs (not HTTP APIs directly) to filter requests based on IP reputation, rate limiting, SQL injection patterns, XSS patterns, and custom rules. Use managed rule groups for OWASP Top 10 coverage. Code/Syntax Justification:
WebACL:
Type: AWS::WAFv2::WebACL
Properties:
Scope: REGIONAL
DefaultAction: { Allow: {} }
Rules:
- Name: RateLimit
Priority: 1
Action: { Block: {} }
Statement:
RateBasedStatement:
Limit: 1000 # 1000 requests per 5 minutes per IP
AggregateKeyType: IP
- Name: AWSManagedRulesCommonRuleSet
Priority: 2
OverrideAction: { None: {} }
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesCommonRuleSet
- Name: SQLInjection
Priority: 3
Action: { Block: {} }
Statement:
SqliMatchStatement:
FieldToMatch: { Body: {} }
TextTransformations:
- Priority: 0
Type: URL_DECODE
π Behind the Question: Defense-in-depth for serverless APIs. β The Red Flags: No WAF β API Gateway directly exposed to SQL injection, credential stuffing, and DDoS attacks.
Q73: How do you implement Lambda function signing for code integrity?β
Technical Answer: AWS Signer creates a code signing configuration that verifies Lambda deployment packages haven't been tampered with. Attach the signing config to a Lambda function β only packages signed by trusted publishers can be deployed. Prevents supply chain attacks. Code/Syntax Justification:
# Code signing configuration
CodeSigningConfig:
Type: AWS::Lambda::CodeSigningConfig
Properties:
AllowedPublishers:
SigningProfileVersionArns:
- !GetAtt MySigningProfile.SigningProfileVersionArn
CodeSigningPolicies:
UntrustedArtifactOnDeployment: Enforce # Block unsigned code
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeSigningConfigArn: !Ref CodeSigningConfig
# Sign deployment package
aws signer start-signing-job \
--source s3://my-bucket/function.zip \
--destination s3://my-bucket/signed/ \
--profile-name my-signing-profile
π Behind the Question: Supply chain security for serverless. β The Red Flags: No code signing β a compromised CI pipeline can deploy malicious Lambda code.
Q74: How do you implement VPC endpoints for Lambda to access AWS services without internet?β
Technical Answer: Lambda in a VPC needs a NAT Gateway for internet access (including AWS API endpoints). VPC endpoints (Interface or Gateway) provide private connectivity to AWS services without a NAT Gateway β cheaper and more secure. Code/Syntax Justification:
# Gateway endpoint for S3 and DynamoDB (free)
S3Endpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
VpcId: !Ref VPC
ServiceName: !Sub "com.amazonaws.${AWS::Region}.s3"
VpcEndpointType: Gateway
RouteTableIds: [!Ref PrivateRouteTable]
DynamoDBEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
VpcId: !Ref VPC
ServiceName: !Sub "com.amazonaws.${AWS::Region}.dynamodb"
VpcEndpointType: Gateway
RouteTableIds: [!Ref PrivateRouteTable]
# Interface endpoint for SQS (charged per hour + data)
SQSEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
VpcId: !Ref VPC
ServiceName: !Sub "com.amazonaws.${AWS::Region}.sqs"
VpcEndpointType: Interface
PrivateDnsEnabled: true
SubnetIds: [!Ref PrivateSubnet1, !Ref PrivateSubnet2]
π Behind the Question: Network architecture optimization. β The Red Flags: Paying $32/month for a NAT Gateway just so Lambda can call DynamoDB β a free VPC endpoint does the same thing.
Q75: How do you implement runtime security scanning for Lambda?β
Technical Answer: Use AWS Inspector for Lambda to scan function code and dependencies for known vulnerabilities (CVEs). Enable Amazon GuardDuty for Lambda runtime threat detection β it monitors network activity, DNS queries, and suspicious API calls from Lambda functions. Code/Syntax Justification:
# Enable Inspector for Lambda scanning
# AWS Console β Inspector β Account management β Enable Lambda scanning
# GuardDuty Lambda protection
# AWS Console β GuardDuty β Settings β Lambda Protection β Enable
# Automated remediation with EventBridge
InspectorFindingRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source: ["aws.inspector2"]
detail-type: ["Inspector2 Finding"]
detail:
severity: ["CRITICAL", "HIGH"]
resourceType: ["AWS_LAMBDA_FUNCTION"]
Targets:
- Arn: !GetAtt RemediationFunction.Arn
Id: auto-remediate
π Behind the Question: Runtime security posture management.
β The Red Flags: No vulnerability scanning β a Lambda function with requests==2.25.0 has known CVEs, exploitable in production.
Section 15: Advanced Lambda Patternsβ
Q76: How do you implement the strangler fig pattern with Lambda to migrate from a monolith?β
Technical Answer: Gradually route traffic from the monolith to Lambda functions feature by feature. Use API Gateway as the facade β route some paths to Lambda, others to the existing monolith via HTTP proxy integration. Over time, more paths move to Lambda until the monolith is fully decomposed. Code/Syntax Justification:
# API Gateway routes: some to Lambda, some to monolith
Routes:
# New: handled by Lambda
NewUsersRoute:
Type: AWS::Serverless::Function
Properties:
Events:
Api:
Type: HttpApi
Properties:
Path: /api/v2/users
Method: GET
# Legacy: proxied to monolith
LegacyProxy:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref MyApi
RouteKey: "ANY /api/v1/{proxy+}"
Target: !Sub "integrations/${MonolithIntegration}"
MonolithIntegration:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref MyApi
IntegrationType: HTTP_PROXY
IntegrationUri: "https://monolith.internal/{proxy}"
π Behind the Question: Migration strategy from monolith to serverless. β The Red Flags: Big-bang rewrite from monolith to Lambda β high risk, months of zero delivery.
Q77: How do you implement Lambda@Edge for request manipulation at CDN level?β
Technical Answer: Lambda@Edge runs Lambda functions at CloudFront edge locations, intercepting requests/responses at four points: viewer request, origin request, origin response, viewer response. Use for: A/B testing, URL rewrites, auth at the edge, header manipulation, and dynamic content. Code/Syntax Justification:
# Lambda@Edge: viewer request β redirect based on country
def handler(event, context):
request = event['Records'][0]['cf']['request']
country = request['headers'].get('cloudfront-viewer-country', [{}])[0].get('value', 'US')
if country == 'DE':
return {
'status': '302',
'statusDescription': 'Found',
'headers': {'location': [{'key': 'Location', 'value': 'https://de.myapp.com' + request['uri']}]},
}
return request # Continue to origin
π Behind the Question: Edge computing patterns. β The Red Flags: Running geo-routing logic in a centralized Lambda when Lambda@Edge handles it at sub-10ms latency worldwide.
Q78: How do you implement Lambda container images?β
Technical Answer: Lambda supports deploying functions as container images (up to 10 GB) instead of ZIP archives. Use AWS-provided base images or bring your own. Benefits: use familiar Docker tooling, include large dependencies (ML models, native libraries), consistent local testing. Code/Syntax Justification:
# Dockerfile for Lambda
FROM public.ecr.aws/lambda/python:3.12
# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy function code
COPY app/ ${LAMBDA_TASK_ROOT}/
# Set the handler
CMD ["app.handler"]
# SAM: container image function
MyFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/my-function:latest"
MemorySize: 3008
Timeout: 300
Metadata:
DockerTag: latest
DockerContext: ./src
Dockerfile: Dockerfile
π Behind the Question: Large dependency management in Lambda. β The Red Flags: Trying to fit a 2 GB ML model into a 250 MB ZIP deployment package β use container images instead.
Q79: How do you implement Lambda SnapStart for faster cold starts?β
Technical Answer: Lambda SnapStart (Java-only as of 2024, Python support coming) takes a snapshot of the initialized function memory after the init phase and caches it. Cold starts restore from the snapshot instead of re-initializing β reducing cold start from ~5s to ~200ms for Java functions. Code/Syntax Justification:
# SAM: Enable SnapStart (Java)
MyJavaFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: java21
SnapStart:
ApplyOn: PublishedVersions
AutoPublishAlias: live
# For Python: Use Provisioned Concurrency instead
# Or minimize init work: lazy-load heavy dependencies
π Behind the Question: Cold start optimization techniques. β The Red Flags: Python Lambda importing TensorFlow, Pandas, and NumPy at module level without considering cold start impact (~3-5 seconds).
Q80: How do you implement recursive Lambda invocations safely?β
Technical Answer: Recursive patterns (Lambda invokes itself for pagination, tree traversal) risk infinite loops and runaway costs. Safeguards: set a max recursion depth counter, use reserved concurrency as a circuit breaker, and enable recursive loop detection (AWS default since 2023). Code/Syntax Justification:
MAX_RECURSION_DEPTH = 10
def handler(event, context):
depth = event.get('recursion_depth', 0)
if depth >= MAX_RECURSION_DEPTH:
logger.error(f"Max recursion depth {MAX_RECURSION_DEPTH} reached, stopping")
return {'status': 'max_depth_reached'}
remaining_items = process_batch(event['items'][:100])
if remaining_items:
lambda_client.invoke(
FunctionName=context.function_name,
InvocationType='Event',
Payload=json.dumps({
'items': remaining_items,
'recursion_depth': depth + 1,
}),
)
return {'status': 'batch_processed', 'depth': depth}
# Set reserved concurrency as circuit breaker
# If recursion goes wild, max 10 concurrent executions limits blast radius
π Behind the Question: Safety patterns for self-invoking functions. β The Red Flags: No recursion limit β a bug causes infinite invocations, $10,000 AWS bill before anyone notices.
Section 16: S3 + Lambda Event Processingβ
Q81: How do you implement S3 event-driven Lambda processing?β
Technical Answer: S3 sends events (ObjectCreated, ObjectRemoved, etc.) to Lambda when objects are uploaded or deleted. Configure event notifications with prefix/suffix filters to trigger only for specific file types or paths. Code/Syntax Justification:
ProcessUploadFunction:
Type: AWS::Serverless::Function
Properties:
Events:
S3Upload:
Type: S3
Properties:
Bucket: !Ref UploadsBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: prefix
Value: uploads/
- Name: suffix
Value: .csv
def handler(event, context):
for record in event['Records']: bucket = record['s3']['bucket']['name']
key = urllib.parse.unquote_plus(record['s3']['object']['key'])
size = record['s3']['object']['size']
logger.info(f"Processing {key} ({size} bytes) from {bucket}")
obj = s3.get_object(Bucket=bucket, Key=key)
process_csv(obj['Body'].read().decode('utf-8'))
π Behind the Question: Event-driven file processing.
β The Red Flags: Polling S3 with list_objects_v2() every minute β wasteful, slow, and misses files uploaded between polls.
Q82: How do you handle large S3 files in Lambda (>512MB)?β
Technical Answer: Lambda has limited /tmp storage (512MB default, extendable to 10GB). For files larger than memory: stream processing with StreamingBody, use S3 Select to query only needed rows/columns, or use EFS (Elastic File System) mounted to Lambda for large temp storage.
Code/Syntax Justification:
# Stream processing β never load entire file into memory
def handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# S3 Select: query CSV without downloading
result = s3.select_object_content(
Bucket=bucket, Key=key,
ExpressionType='SQL',
Expression="SELECT * FROM s3object s WHERE s.status = 'active'",
InputSerialization={'CSV': {'FileHeaderInfo': 'USE'}},
OutputSerialization={'JSON': {}},
)
for event_stream in result['Payload']:
if 'Records' in event_stream:
records = json.loads(event_stream['Records']['Payload'].decode())
process(records)
# Lambda with EFS for large temp storage
MyFunction:
Type: AWS::Serverless::Function
Properties:
EphemeralStorage:
Size: 10240 # 10 GB /tmp
FileSystemConfigs:
- Arn: !GetAtt EFSAccessPoint.Arn
LocalMountPath: /mnt/data
π Behind the Question: Resource constraints and workarounds in serverless.
β The Red Flags: pd.read_csv(io.BytesIO(s3.get_object(...)['Body'].read())) on a 5 GB file β OOM crash.
Q83: How do you implement S3 event deduplication for Lambda?β
Technical Answer: S3 event notifications are at-least-once β the same event may trigger Lambda multiple times. Use DynamoDB conditional writes or Redis SET NX with the S3 object key + version as the dedup key to prevent double-processing.
Code/Syntax Justification:
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
version = record['s3']['object'].get('versionId', 'null')
dedup_key = f"{bucket}/{key}/{version}"
try:
table.put_item(
Item={'dedup_key': dedup_key, 'ttl': int(time.time()) + 86400},
ConditionExpression='attribute_not_exists(dedup_key)',
)
except ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
logger.info(f"Duplicate event for {key}, skipping")
continue
raise
process_file(bucket, key)
π Behind the Question: Idempotency in event-driven systems. β The Red Flags: Processing the same uploaded file twice β generating duplicate records in the database.
Q84: How do you implement S3 batch operations triggered by Lambda?β
Technical Answer: S3 Batch Operations runs a Lambda function against millions of S3 objects from a manifest file. Use for bulk operations: format conversion, metadata updates, cross-region copies, or re-processing after a bug fix. S3 manages parallelism and retry logic. Code/Syntax Justification:
# Lambda invoked by S3 Batch Operations
def batch_handler(event, context):
results = []
for task in event['tasks']:
s3_key = urllib.parse.unquote_plus(task['s3Key'])
s3_bucket = task['s3BucketArn'].split(':::')[1]
try:
convert_image_format(s3_bucket, s3_key)
results.append({
'taskId': task['taskId'],
'resultCode': 'Succeeded',
'resultString': f'Converted {s3_key}',
})
except Exception as e:
results.append({
'taskId': task['taskId'],
'resultCode': 'PermanentFailure',
'resultString': str(e),
})
return {'invocationSchemaVersion': '1.0', 'treatMissingKeysAs': 'PermanentFailure', 'invocationId': event['invocationId'], 'results': results}
π Behind the Question: Large-scale S3 data processing. β The Red Flags: Writing a custom script that lists and processes millions of S3 objects sequentially β S3 Batch does this with built-in parallelism and reporting.
Q85: How do you implement S3 Object Lambda for dynamic content transformation?β
Technical Answer: S3 Object Lambda runs a Lambda function when an object is retrieved via an Object Lambda Access Point. The function can transform the object on-the-fly: redact PII, resize images, decompress, or filter data β without creating and storing multiple copies. Code/Syntax Justification:
import boto3
s3 = boto3.client('s3')
def handler(event, context):
# Get the original object from S3
get_context = event['getObjectContext']
original = s3.get_object(
Bucket=event['configuration']['supportingAccessPointArn'],
Key=get_context['inputS3Url'],
)
content = original['Body'].read().decode('utf-8')
# Transform: redact email addresses
import re
redacted = re.sub(r'[\w.+-]+@[\w-]+\.[\w.]+', '[REDACTED]', content)
# Return transformed object to the caller
s3.write_get_object_response(
Body=redacted.encode(),
RequestRoute=get_context['outputRoute'],
RequestToken=get_context['outputToken'],
)
π Behind the Question: Dynamic content transformation without storage duplication. β The Red Flags: Storing 5 versions of every document (original, redacted, summarized, etc.) β storage costs multiply 5x.
Section 17: SNS + Lambdaβ
Q86: How does SNS fan-out to Lambda work?β
Technical Answer: SNS (Simple Notification Service) publishes a message to a topic; all subscribed Lambda functions receive a copy. This is the standard fan-out pattern β one event triggers multiple independent processing pipelines. Each Lambda subscription is independent with its own DLQ and retry. Code/Syntax Justification:
# SAM: SNS topic with multiple Lambda subscribers
OrderTopic:
Type: AWS::SNS::Topic
EmailNotification:
Type: AWS::Serverless::Function
Properties:
Events:
SNS:
Type: SNS
Properties:
Topic: !Ref OrderTopic
FilterPolicy:
order_type: [premium] # Only premium orders
InventoryUpdate:
Type: AWS::Serverless::Function
Properties:
Events:
SNS:
Type: SNS
Properties:
Topic: !Ref OrderTopic # Receives ALL orders
# Publish
sns.publish(
TopicArn=ORDER_TOPIC_ARN,
Message=json.dumps({'order_id': 'ord-123', 'total': 500}),
MessageAttributes={'order_type': {'DataType': 'String', 'StringValue': 'premium'}},
)
π Behind the Question: Event-driven microservice communication. β The Red Flags: Sequentially calling 5 Lambda functions from another Lambda β tight coupling, cascading failures, increased latency.
Q87: How do you implement SNS message filtering to reduce Lambda invocations?β
Technical Answer: SNS filter policies apply at the subscription level β only messages matching the filter are delivered. This prevents unnecessary Lambda invocations. Filter on MessageAttributes (default) or MessageBody (opt-in).
Code/Syntax Justification:
# Only invoke Lambda for high-value orders in specific regions
HighValueOrderSubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref OrderTopic
Protocol: lambda
Endpoint: !GetAtt HighValueHandler.Arn
FilterPolicy:
total: [{ "numeric": [">=", 1000] }]
region: ["us-east-1", "eu-west-1"]
status: [{ "anything-but": ["cancelled"] }]
FilterPolicyScope: MessageAttributes # or MessageBody
π Behind the Question: Cost optimization and efficient event routing. β The Red Flags: Filtering inside Lambda β 90% of invocations return immediately after checking a condition, wasting money.
Q88: What is the difference between SNS and SQS for Lambda integration?β
Technical Answer: SNSβLambda: push-based, fan-out to multiple subscribers, no batching, synchronous invocation. SQSβLambda: poll-based, single consumer per queue, batching (up to 10k messages), built-in retry with visibility timeout, DLQ support. Use SNS for fan-out, SQS for reliable task processing. Code/Syntax Justification:
SNS β Lambda:
β
Fan-out to multiple Lambdas
β
Filter policies per subscriber
β No batching
β No replay
β Synchronous β Lambda error = message lost (unless DLQ configured)
SQS β Lambda:
β
Batching (up to 10,000 messages)
β
Built-in retry (visibility timeout)
β
DLQ for failed messages
β
FIFO ordering support
β Single consumer per queue
β No native fan-out
# Common pattern: SNS β SQS β Lambda (best of both)
# SNS fans out to multiple SQS queues, each processed by its own Lambda
π Behind the Question: Messaging service selection. β The Red Flags: Using SNS for a task queue (no retry on failure) or SQS for fan-out (requires duplicating queues).
Section 18: CloudFormation & SAM Deep Diveβ
Q89: How do you organize a large SAM application with nested stacks?β
Technical Answer: Split a large SAM template into nested stacks β one per domain (API, database, queues, monitoring). The parent stack orchestrates nested stacks with AWS::CloudFormation::Stack. Pass outputs between stacks using !GetAtt NestedStack.Outputs.OutputKey.
Code/Syntax Justification:
# Parent template
Resources:
DatabaseStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: ./stacks/database.yaml
Parameters:
Environment: !Ref Environment
ApiStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: ./stacks/api.yaml
Parameters:
TableArn: !GetAtt DatabaseStack.Outputs.TableArn
Environment: !Ref Environment
Outputs:
ApiUrl:
Value: !GetAtt ApiStack.Outputs.ApiUrl
π Behind the Question: IaC organization for complex serverless applications. β The Red Flags: A single 3,000-line SAM template β impossible to review, slow to deploy, blast radius of every change is the entire stack.
Q90: How do you implement SAM local development and debugging?β
Technical Answer: sam local invoke runs a Lambda function locally in a Docker container. sam local start-api starts a local API Gateway. sam local start-lambda runs a local Lambda endpoint for integration testing. Use sam local generate-event to create test events.
Code/Syntax Justification:
# Generate test events
sam local generate-event s3 put > events/s3_put.json
sam local generate-event apigateway aws-proxy > events/api_gw.json
sam local generate-event sqs receive-message > events/sqs.json
# Invoke function locally
sam local invoke MyFunction -e events/s3_put.json --env-vars env.json
# Start local API
sam local start-api --port 3000 --warm-containers EAGER
# Debug with step-through (VS Code)
sam local invoke MyFunction -e events/test.json -d 5858
# Then attach VS Code debugger to port 5858
π Behind the Question: Local development workflow for serverless. β The Red Flags: Deploying to AWS for every code change during development β slow iteration cycles, wasteful, expensive.
Q91: How do you implement infrastructure drift detection for serverless stacks?β
Technical Answer: CloudFormation drift detection compares actual resource configurations against the template. Use aws cloudformation detect-stack-drift to identify manual changes made outside CloudFormation. Schedule periodic drift checks and alert on non-compliant resources.
Code/Syntax Justification:
# Detect drift
aws cloudformation detect-stack-drift --stack-name my-serverless-app
# Check drift status
aws cloudformation describe-stack-drift-detection-status \
--stack-drift-detection-id <detection-id>
# Get drifted resources
aws cloudformation describe-stack-resource-drifts \
--stack-name my-serverless-app \
--stack-resource-drift-status-filters MODIFIED DELETED
# Automated drift detection with EventBridge
DriftCheckSchedule:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: rate(1 day)
Targets:
- Arn: !GetAtt DriftCheckFunction.Arn
Id: daily-drift-check
π Behind the Question: Infrastructure compliance and configuration management. β The Red Flags: Someone manually changes a Lambda's memory to 10GB in the console β next CloudFormation deploy reverts it, causing a production incident.
Section 19: Lambda + Containers & ECS Patternsβ
Q92: When should you use Lambda vs ECS Fargate?β
Technical Answer: Use Lambda for: event-driven workloads, request/response <15min, bursty traffic, simple deployment. Use Fargate for: long-running processes, consistent high throughput, WebSocket servers, workloads requiring >10GB memory, specific runtime requirements. Code/Syntax Justification:
Lambda wins:
β
<100ms startup (warm), pay-per-request
β
Auto-scales to 0 (no idle cost)
β
Event integrations (S3, SQS, DynamoDB)
β
Max 15 min execution, 10 GB memory
β
Cost-effective below ~1M requests/month
Fargate wins:
β
Long-running (hours/days)
β
Up to 120 GB memory, 16 vCPU
β
Persistent connections (WebSocket, gRPC)
β
Custom networking (service mesh)
β
Cost-effective above ~10M requests/month
π Behind the Question: Compute service selection. β The Red Flags: Running a WebSocket server on Lambda β 15-minute timeout kills persistent connections.
Q93: How do you implement Lambda function composition with Step Functions vs direct invocation?β
Technical Answer: Direct invocation (lambda.invoke()) is simple but creates tight coupling. Step Functions provides: visual workflow, built-in error handling per step, wait states, parallel execution, and execution history. Use Step Functions for workflows >2 steps.
Code/Syntax Justification:
# Direct invocation (simple but tightly coupled)
def function_a(event, context):
result_a = process_a(event)
lambda_client.invoke(
FunctionName='function-b',
Payload=json.dumps(result_a),
)
# If function_b fails, function_a has no idea
# No retry, no visibility, no audit trail
# Step Functions (decoupled, observable)
{
"StartAt": "FunctionA",
"States": {
"FunctionA": {"Type": "Task", "Resource": "arn:...:function-a", "Next": "FunctionB",
"Retry": [{"ErrorEquals": ["States.ALL"], "MaxAttempts": 3}]},
"FunctionB": {"Type": "Task", "Resource": "arn:...:function-b", "End": true,
"Catch": [{"ErrorEquals": ["States.ALL"], "Next": "HandleFailure"}]},
"HandleFailure": {"Type": "Task", "Resource": "arn:...:alert", "End": true}
}
}
π Behind the Question: Workflow orchestration patterns. β The Red Flags: 5-level deep Lambda-calling-Lambda β spaghetti architecture, impossible to debug, cascading timeouts.
Section 20: Lambda Performance Engineeringβ
Q94: How do you profile a Lambda function to find performance bottlenecks?β
Technical Answer: Use X-Ray subsegments for timing code sections, CloudWatch Lambda Insights for system metrics, and custom timing logs with time.perf_counter(). Profile locally with cProfile or py-spy. Focus on: init duration (cold start), import time, DB queries, and API calls.
Code/Syntax Justification:
import time
from aws_xray_sdk.core import xray_recorder
def handler(event, context):
timings = {}
start = time.perf_counter()
with xray_recorder.in_subsegment('db_query'):
data = query_database(event['user_id'])
timings['db_query'] = time.perf_counter() - start
start = time.perf_counter()
with xray_recorder.in_subsegment('transform'):
result = transform_data(data)
timings['transform'] = time.perf_counter() - start
start = time.perf_counter()
with xray_recorder.in_subsegment('api_call'):
response = call_external_api(result)
timings['api_call'] = time.perf_counter() - start
logger.info(json.dumps({'timings': timings, 'total': sum(timings.values())}))
return response
π Behind the Question: Performance engineering methodology. β The Red Flags: Blindly increasing Lambda memory to "fix" slowness β the bottleneck might be an external API call that more CPU won't help.
Q95: How do you implement connection reuse in Lambda?β
Technical Answer: Initialize HTTP clients, database connections, and SDK clients at module level (outside the handler). Lambda reuses the execution environment for subsequent invocations β module-level objects persist. Enable HTTP keep-alive for urllib3/requests and boto3.
Code/Syntax Justification:
import boto3
import os
from botocore.config import Config
# Module-level: initialized once per container, reused across invocations
boto_config = Config(
max_pool_connections=10,
retries={'max_attempts': 3, 'mode': 'adaptive'},
)
dynamodb = boto3.resource('dynamodb', config=boto_config)
table = dynamodb.Table(os.environ['TABLE_NAME'])
s3 = boto3.client('s3', config=boto_config)
# Keep-alive for HTTP connections
os.environ['AWS_LAMBDA_EXEC_WRAPPER'] = '' # Enable keep-alive by default in newer SDKs
def handler(event, context):
# Reuses existing connections β no handshake overhead
item = table.get_item(Key={'pk': event['id']})
return item.get('Item')
π Behind the Question: Warm invocation optimization. β The Red Flags: Creating a new boto3 client inside the handler β 50-100ms overhead on every invocation for SDK initialization.
Q96: How do you reduce Lambda deployment package size?β
Technical Answer: Use Lambda Layers for shared dependencies, strip unused packages with pip install --no-deps, exclude test files and docs, use __pycache__ cleanup, and consider lambda-packages for pre-compiled native dependencies. Smaller packages = faster cold starts.
Code/Syntax Justification:
# Minimal deployment package
pip install -r requirements.txt -t ./package --no-cache-dir
# Remove unnecessary files
cd package
find . -name "*.pyc" -delete
find . -name "__pycache__" -type d -exec rm -rf {} +
find . -name "*.dist-info" -type d -exec rm -rf {} +
find . -name "tests" -type d -exec rm -rf {} +
find . -name "*.md" -delete
find . -name "*.txt" -delete
# Layer for heavy dependencies
sam build --use-container
# Or create a layer
pip install pandas numpy -t ./layer/python/
cd layer && zip -r layer.zip python/
aws lambda publish-layer-version --layer-name data-deps --zip-file fileb://layer.zip
π Behind the Question: Cold start optimization and deployment efficiency. β The Red Flags: A 250 MB deployment package including scipy, matplotlib, and test fixtures β 5-second cold start.
Q97: How do you implement Lambda response caching with API Gateway?β
Technical Answer: REST API supports response caching at the stage level. Configure cache capacity (0.5-237 GB), TTL, and per-method cache settings. Cache keys include URL path, query params, and specified headers. Caching eliminates Lambda invocation for repeated identical requests. Code/Syntax Justification:
# SAM: Enable API caching
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
CacheClusterEnabled: true
CacheClusterSize: "0.5" # 0.5 GB cache
MethodSettings:
- HttpMethod: GET
ResourcePath: /products
CachingEnabled: true
CacheTtlInSeconds: 300 # Cache for 5 minutes
CacheDataEncrypted: true
# Cache key includes query parameters
# GET /products?category=shoes β cached
# GET /products?category=hats β different cache entry
# Invalidate cache
# aws apigateway flush-stage-cache --rest-api-id xxx --stage-name prod
π Behind the Question: Latency reduction and cost optimization. β The Red Flags: Lambda computing the same product catalog response 10,000 times per hour when it changes once per day.
Q98: How do you implement async Lambda invocations for fire-and-forget patterns?β
Technical Answer: Use InvocationType: 'Event' for async invocations β Lambda queues the event and returns immediately (202). AWS retries failed async invocations up to 2 times. Configure MaximumRetryAttempts and MaximumEventAgeInSeconds via EventInvokeConfig.
Code/Syntax Justification:
# Async invocation (fire-and-forget)
lambda_client.invoke(
FunctionName='send-email',
InvocationType='Event', # Async β returns immediately with 202
Payload=json.dumps({'to': 'user@example.com', 'subject': 'Welcome!'}),
)
# Caller continues immediately β does NOT wait for email to send
# SAM: Configure async behavior
SendEmailFunction:
Type: AWS::Serverless::Function
Properties:
EventInvokeConfig:
MaximumRetryAttempts: 2
MaximumEventAgeInSeconds: 3600
DestinationConfig:
OnSuccess:
Destination: !GetAtt SuccessQueue.Arn
OnFailure:
Destination: !GetAtt FailureQueue.Arn
π Behind the Question: Async patterns in serverless. β The Red Flags: Synchronous Lambda-to-Lambda calls for non-critical tasks like logging β doubles latency for no reason.
Q99: How do you implement Lambda concurrency management?β
Technical Answer: Three concurrency controls: Unreserved (default, shared pool), Reserved (dedicated pool per function, prevents starvation), Provisioned (pre-warmed instances, eliminates cold starts). Account-level default is 1,000 concurrent executions. Code/Syntax Justification:
# Reserved concurrency β guarantees capacity but limits max
CriticalFunction:
Type: AWS::Serverless::Function
Properties:
ReservedConcurrentExecutions: 100 # Always has 100 available, never exceeds 100
# Provisioned concurrency β pre-warmed for low latency
LatencySensitiveFunction:
Type: AWS::Serverless::Function
Properties:
AutoPublishAlias: live
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 20
# Account-level limit increase
# aws service-quotas request-service-quota-increase \
# --service-code lambda \
# --quota-code L-B99A9384 \
# --desired-value 3000
π Behind the Question: Resource isolation and capacity planning. β The Red Flags: No reserved concurrency β a traffic spike on one function consumes all 1,000 concurrent executions, starving every other function in the account.
Q100: How do you implement Lambda function URLs with IAM auth?β
Technical Answer: Lambda Function URLs with AWS_IAM auth require the caller to sign requests with SigV4. This is ideal for service-to-service communication without API Gateway overhead. The caller needs lambda:InvokeFunctionUrl permission.
Code/Syntax Justification:
# Calling a Lambda Function URL with IAM auth (using botocore SigV4)
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
import botocore.session
session = botocore.session.get_session()
credentials = session.get_credentials()
url = 'https://abc123.lambda-url.us-east-1.on.aws/'
request = AWSRequest(method='POST', url=url, data=json.dumps({'key': 'value'}),
headers={'Content-Type': 'application/json'})
SigV4Auth(credentials, 'lambda', 'us-east-1').add_auth(request)
response = requests.post(url, headers=dict(request.headers), data=request.body)
# SAM: IAM-authenticated Function URL
MyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionUrlConfig:
AuthType: AWS_IAM
π Behind the Question: Secure service-to-service communication without API Gateway.
β The Red Flags: Using AuthType: NONE for internal service endpoints β anyone with the URL can invoke the function.
Section 21: Lambda Edge Cases & Troubleshootingβ
Q101: How do you debug Lambda timeout issues?β
Technical Answer: Timeouts indicate the function didn't complete within the configured limit. Root causes: slow downstream services, database connection issues, or CPU-intensive operations. Use X-Ray to trace which subsegment consumed the most time. Check context.get_remaining_time_in_millis() at key checkpoints.
Code/Syntax Justification:
def handler(event, context):
logger.info(f"Remaining time: {context.get_remaining_time_in_millis()}ms")
# Checkpoint 1: Database query
start = time.time()
data = query_db()
logger.info(f"DB query took {time.time()-start:.2f}s, remaining: {context.get_remaining_time_in_millis()}ms")
# Checkpoint 2: External API
remaining = context.get_remaining_time_in_millis() / 1000
if remaining < 5:
raise TimeoutError("Not enough time to call external API")
try:
result = requests.get(API_URL, timeout=min(remaining - 2, 10))
except requests.Timeout:
logger.error(f"External API timed out with {context.get_remaining_time_in_millis()}ms remaining")
raise
return result.json()
π Behind the Question: Production troubleshooting methodology. β The Red Flags: Setting timeout to 15 minutes to "fix" timeouts β masking the real problem, burning money.
Q102: How do you handle Lambda throttling (429 errors)?β
Technical Answer: Throttling occurs when concurrent executions exceed the account/function limit. For synchronous invocations, the caller gets a 429 error. For async, Lambda retries automatically. Solutions: request limit increase, implement client-side retry with exponential backoff, use SQS as a buffer. Code/Syntax Justification:
# Client-side retry with exponential backoff
import time
from botocore.exceptions import ClientError
def invoke_with_retry(function_name, payload, max_retries=5):
for attempt in range(max_retries):
try:
return lambda_client.invoke(
FunctionName=function_name,
Payload=json.dumps(payload),
)
except ClientError as e:
if e.response['Error']['Code'] == 'TooManyRequestsException':
wait = 2 ** attempt + random.random()
logger.warning(f"Throttled, retrying in {wait:.1f}s")
time.sleep(wait)
else:
raise
raise Exception("Max retries exceeded due to throttling")
# Better: buffer through SQS (Lambda polls at a controlled rate)
sqs.send_message(QueueUrl=QUEUE_URL, MessageBody=json.dumps(payload))
π Behind the Question: Scalability limits and resilience patterns. β The Red Flags: No retry on throttle errors β caller gets 429 and gives up, data is lost.
Q103: How do you handle Lambda memory errors (OOM)?β
Technical Answer: Lambda kills the function with Runtime.ExitError or Runtime.OutOfMemory when memory exceeds the configured limit. Root causes: loading large files into memory, unbounded list growth, or memory leaks in warm containers. Monitor max_memory_used in Lambda Insights.
Code/Syntax Justification:
# BAD: Loading entire file into memory
def handler(event, context):
data = s3.get_object(...)['Body'].read() # 1 GB file β OOM at 512 MB
rows = json.loads(data)
return process(rows)
# GOOD: Stream processing
def handler(event, context):
body = s3.get_object(...)['Body']
for line in body.iter_lines():
process_row(json.loads(line))
# Monitor memory usage
def handler(event, context):
import resource
memory_mb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
logger.info(f"Memory used: {memory_mb:.1f} MB / {context.memory_limit_in_mb} MB")
if memory_mb > context.memory_limit_in_mb * 0.9:
logger.warning("Memory usage above 90%!")
π Behind the Question: Resource management in constrained environments. β The Red Flags: Setting memory to 10 GB for a function that processes 1 KB payloads β wasting $4,000/month.
Q104: How do you handle Lambda /tmp storage limitations?β
Technical Answer: /tmp provides 512 MB by default (configurable up to 10 GB). It persists across warm invocations β cleanup is your responsibility. For large temp files, use S3 as intermediate storage or mount EFS for virtually unlimited storage.
Code/Syntax Justification:
import os
import tempfile
def handler(event, context):
# Check /tmp usage
tmp_usage = sum(
os.path.getsize(os.path.join(dirpath, f))
for dirpath, _, filenames in os.walk('/tmp')
for f in filenames
)
logger.info(f"/tmp usage: {tmp_usage / 1024 / 1024:.1f} MB")
# Always clean up temp files
with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.csv', delete=True) as tmp:
s3.download_fileobj('bucket', 'key', tmp)
tmp.seek(0)
result = process_csv(tmp.name)
return result
# Extended /tmp storage
MyFunction:
Type: AWS::Serverless::Function
Properties:
EphemeralStorage:
Size: 5120 # 5 GB /tmp
π Behind the Question: Ephemeral storage management.
β The Red Flags: Writing files to /tmp without cleanup β warm containers accumulate files until /tmp is full, causing subsequent invocations to fail.
Q105: How do you implement Lambda graceful error handling and custom error responses?β
Technical Answer: For API Gateway integration, return structured error responses with appropriate HTTP status codes. Use try/except to catch all exceptions, log the full traceback, and return a user-friendly error message. Never expose internal errors to clients. Code/Syntax Justification:
import traceback
class AppError(Exception):
def __init__(self, message, status_code=400):
self.message = message
self.status_code = status_code
def handler(event, context):
try:
body = json.loads(event.get('body', '{}'))
result = process_request(body)
return {
'statusCode': 200,
'body': json.dumps(result),
'headers': {'Content-Type': 'application/json'},
}
except AppError as e:
return {'statusCode': e.status_code, 'body': json.dumps({'error': e.message})}
except json.JSONDecodeError:
return {'statusCode': 400, 'body': json.dumps({'error': 'Invalid JSON'})}
except Exception as e:
logger.error(f"Unhandled error: {traceback.format_exc()}")
return {
'statusCode': 500,
'body': json.dumps({'error': 'Internal server error', 'request_id': context.aws_request_id}),
}
π Behind the Question: Production error handling and security. β The Red Flags: Returning raw Python tracebacks to the client β exposes internal code paths, library versions, and potential vulnerabilities.
Section 22: Real-World System Design with Lambdaβ
Q106: Design a real-time image processing pipeline with Lambda.β
Technical Answer: S3 upload trigger β Lambda resizes/optimizes β stores processed versions back to S3 β updates DynamoDB metadata β sends SNS notification. For large images, use Step Functions to orchestrate multiple processing steps (resize, watermark, thumbnail, face detection). Code/Syntax Justification:
from PIL import Image
import io
SIZES = {'thumbnail': (150, 150), 'medium': (800, 600), 'large': (1920, 1080)}
def handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
original = s3.get_object(Bucket=bucket, Key=key)
image = Image.open(io.BytesIO(original['Body'].read()))
for size_name, dimensions in SIZES.items():
resized = image.copy()
resized.thumbnail(dimensions, Image.LANCZOS)
buffer = io.BytesIO()
resized.save(buffer, format='JPEG', quality=85, optimize=True)
buffer.seek(0)
output_key = f"processed/{size_name}/{key.split('/')[-1]}"
s3.put_object(Bucket=bucket, Key=output_key, Body=buffer, ContentType='image/jpeg')
# Update metadata
table.put_item(Item={
'image_id': key.split('/')[-1],
'sizes': list(SIZES.keys()),
'processed_at': datetime.utcnow().isoformat(),
})
π Behind the Question: End-to-end serverless pipeline design. β The Red Flags: Processing images synchronously in the API request handler β user waits 10 seconds for upload to complete.
Q107: Design a serverless webhook processing system.β
Technical Answer: API Gateway receives webhook β Lambda validates signature β SQS queues for reliable processing β Worker Lambda processes with retry and DLQ β DynamoDB stores delivery status β EventBridge triggers alerts on failures. Code/Syntax Justification:
import hmac
import hashlib
def webhook_receiver(event, context):
body = event['body']
signature = event['headers'].get('x-webhook-signature')
# Validate webhook signature
expected = hmac.new(WEBHOOK_SECRET.encode(), body.encode(), hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature, expected):
return {'statusCode': 401, 'body': 'Invalid signature'}
# Queue for reliable processing
sqs.send_message(
QueueUrl=PROCESSING_QUEUE,
MessageBody=body,
MessageAttributes={
'webhook_type': {'DataType': 'String', 'StringValue': event['headers'].get('x-webhook-type', 'unknown')},
},
)
return {'statusCode': 200, 'body': 'Accepted'}
def webhook_processor(event, context):
failed = []
for record in event['Records']:
try:
payload = json.loads(record['body'])
handle_webhook_event(payload)
table.update_item(
Key={'webhook_id': payload['id']},
UpdateExpression='SET #s = :s, processed_at = :t',
ExpressionAttributeNames={'#s': 'status'},
ExpressionAttributeValues={':s': 'processed', ':t': datetime.utcnow().isoformat()},
)
except Exception as e:
failed.append({'itemIdentifier': record['messageId']})
return {'batchItemFailures': failed}
π Behind the Question: Production webhook architecture. β The Red Flags: Processing webhooks synchronously β if processing takes >30 seconds, the webhook sender considers it failed and retries.
Q108: Design a serverless data lake ingestion pipeline.β
Technical Answer: Data sources β Kinesis Data Streams β Lambda (transform + validate) β S3 (Parquet format in partitioned structure) β AWS Glue Catalog β Athena queries. Use Kinesis Data Firehose for simpler use cases with built-in S3 delivery. Code/Syntax Justification:
import pyarrow as pa
import pyarrow.parquet as pq
import io
def transform_and_write(event, context):
records = []
for record in event['Records']:
data = json.loads(base64.b64decode(record['kinesis']['data']))
records.append({
'event_type': data['type'],
'user_id': data['user_id'],
'timestamp': data['timestamp'],
'properties': json.dumps(data.get('properties', {})),
})
if records:
table = pa.Table.from_pylist(records)
buffer = io.BytesIO()
pq.write_table(table, buffer, compression='snappy')
buffer.seek(0)
# Partitioned S3 path: s3://lake/events/year=2026/month=04/day=25/
now = datetime.utcnow()
key = f"events/year={now.year}/month={now.month:02d}/day={now.day:02d}/{uuid.uuid4()}.parquet"
s3.put_object(Bucket='data-lake', Key=key, Body=buffer)
π Behind the Question: Data engineering with serverless. β The Red Flags: Writing individual JSON files per event β millions of tiny files, slow Athena queries.
Q109: Design a serverless authentication system with Cognito and Lambda triggers.β
Technical Answer: Cognito User Pool handles registration, login, MFA, and JWT issuance. Lambda triggers customize the auth flow: pre-signup validation, post-confirmation actions, pre-token generation (inject custom claims), and custom message (branded emails). Code/Syntax Justification:
# Pre-signup trigger: validate email domain
def pre_signup(event, context):
email = event['request']['userAttributes']['email']
if not email.endswith('@company.com'):
raise Exception("Only company emails are allowed")
event['response']['autoConfirmUser'] = False
return event
# Post-confirmation trigger: create user profile
def post_confirmation(event, context):
user_sub = event['request']['userAttributes']['sub']
email = event['request']['userAttributes']['email']
table.put_item(Item={
'PK': f'USER#{user_sub}',
'SK': 'PROFILE',
'email': email,
'created_at': datetime.utcnow().isoformat(),
'plan': 'free',
})
return event
# Pre-token generation: add custom claims to JWT
def pre_token(event, context):
user = table.get_item(Key={'PK': f"USER#{event['request']['userAttributes']['sub']}", 'SK': 'PROFILE'})
event['response']['claimsOverrideDetails'] = {
'claimsToAddOrOverride': {
'custom:plan': user['Item']['plan'],
'custom:tenant_id': user['Item'].get('tenant_id', ''),
}
}
return event
π Behind the Question: Serverless authentication architecture. β The Red Flags: Building a custom auth system with Lambda + DynamoDB instead of using Cognito β reinventing password hashing, token management, MFA.
Q110: Design a serverless notification system (email, SMS, push).β
Technical Answer: EventBridge receives notification events β Lambda routes to the appropriate channel (SES for email, SNS for SMS, Pinpoint for push) β SQS buffers for rate limiting β DynamoDB tracks delivery status β CloudWatch monitors delivery rates and failures. Code/Syntax Justification:
from enum import Enum
class Channel(Enum):
EMAIL = 'email'
SMS = 'sms'
PUSH = 'push'
def notification_router(event, context):
detail = event['detail']
user_id = detail['user_id']
preferences = get_user_preferences(user_id)
for channel in preferences['channels']:
if channel == Channel.EMAIL.value:
sqs.send_message(QueueUrl=EMAIL_QUEUE, MessageBody=json.dumps({
'to': preferences['email'],
'template': detail['template'],
'data': detail['data'],
}))
elif channel == Channel.SMS.value:
sqs.send_message(QueueUrl=SMS_QUEUE, MessageBody=json.dumps({
'phone': preferences['phone'],
'message': render_sms(detail['template'], detail['data']),
}))
def email_sender(event, context):
for record in event['Records']:
msg = json.loads(record['body'])
ses.send_templated_email(
Source='noreply@myapp.com',
Destination={'ToAddresses': [msg['to']]},
Template=msg['template'],
TemplateData=json.dumps(msg['data']),
)
π Behind the Question: Multi-channel notification architecture. β The Red Flags: Sending emails synchronously in the API handler β 500ms SES latency added to every API response.
Section 23: Lambda + AI/ML Integrationβ
Q111: How do you integrate Lambda with Amazon Bedrock for LLM inference?β
Technical Answer: Lambda invokes Bedrock models (Claude, Titan, Llama) via the bedrock-runtime API. Keep the Lambda timeout generous (30-60s for LLM calls). Stream responses using response streaming for chat applications. Cache common prompts in ElastiCache.
Code/Syntax Justification:
import boto3
import json
bedrock = boto3.client('bedrock-runtime')
def handler(event, context):
prompt = event['body']['prompt']
response = bedrock.invoke_model(
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
contentType='application/json',
accept='application/json',
body=json.dumps({
'anthropic_version': 'bedrock-2023-05-31',
'max_tokens': 1024,
'messages': [{'role': 'user', 'content': prompt}],
}),
)
result = json.loads(response['body'].read())
return {
'statusCode': 200,
'body': json.dumps({'response': result['content'][0]['text']}),
}
π Behind the Question: AI integration in serverless architectures. β The Red Flags: 3-second Lambda timeout for LLM inference β Bedrock calls routinely take 5-15 seconds.
Q112: How do you implement Lambda-based RAG (Retrieval-Augmented Generation)?β
Technical Answer: Query β Lambda embeds query with Bedrock Titan Embeddings β searches vector store (OpenSearch Serverless, Pinecone, or pgvector) β retrieves relevant documents β constructs prompt with context β calls Bedrock LLM β returns augmented response. Code/Syntax Justification:
def rag_handler(event, context):
query = event['body']['query']
# Step 1: Embed the query
embedding_response = bedrock.invoke_model(
modelId='amazon.titan-embed-text-v2:0',
body=json.dumps({'inputText': query}),
)
query_embedding = json.loads(embedding_response['body'].read())['embedding']
# Step 2: Search vector store
search_results = opensearch.search(index='documents', body={
'query': {'knn': {'embedding': {'vector': query_embedding, 'k': 5}}},
})
context_docs = [hit['_source']['text'] for hit in search_results['hits']['hits']]
# Step 3: Generate augmented response
augmented_prompt = f"""Answer based on the following context:
Context:
{chr(10).join(context_docs)}
Question: {query}
Answer:"""
response = bedrock.invoke_model(
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
body=json.dumps({
'anthropic_version': 'bedrock-2023-05-31',
'max_tokens': 2048,
'messages': [{'role': 'user', 'content': augmented_prompt}],
}),
)
return json.loads(response['body'].read())['content'][0]['text']
π Behind the Question: Production RAG architecture with serverless. β The Red Flags: Sending the entire document corpus in the prompt β exceeds token limits, extremely expensive.
Section 24: Lambda Governance & Complianceβ
Q113: How do you implement Lambda function tagging and resource organization?β
Technical Answer: Tag all Lambda functions with standard tags: Environment, Team, CostCenter, Application, Compliance. Use AWS Organizations SCPs to enforce tagging. Tags enable cost allocation, access control (tag-based IAM policies), and resource grouping.
Code/Syntax Justification:
Globals:
Function:
Tags:
Environment: !Ref Environment
Team: backend
CostCenter: engineering
Application: order-service
Compliance: pci-dss
DataClassification: confidential
# Tag-based IAM policy β only allow access to same-team resources
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": "*",
"Condition": {"StringEquals": {"aws:ResourceTag/Team": "${aws:PrincipalTag/Team}"}}
}
π Behind the Question: Cloud governance and cost management. β The Red Flags: 200 Lambda functions with no tags β impossible to attribute costs, identify owners, or enforce compliance.
Q114: How do you implement Lambda audit logging with CloudTrail?β
Technical Answer: CloudTrail logs all Lambda API calls (CreateFunction, UpdateFunctionCode, InvokeFunction). Enable CloudTrail data events for Lambda to capture every invocation with request/response metadata. Store in S3 for compliance retention. Code/Syntax Justification:
AuditTrail:
Type: AWS::CloudTrail::Trail
Properties:
IsLogging: true
S3BucketName: !Ref AuditBucket
EnableLogFileValidation: true
EventSelectors:
- DataResources:
- Type: AWS::Lambda::Function
Values: ["arn:aws:lambda"] # All Lambda functions
IncludeManagementEvents: true
ReadWriteType: All
# Query audit logs with Athena
# SELECT * FROM cloudtrail_logs
# WHERE eventsource = 'lambda.amazonaws.com'
# AND eventname = 'UpdateFunctionCode20150331v2'
# AND eventtime > '2026-04-01'
π Behind the Question: Security audit and compliance (SOC 2, HIPAA). β The Red Flags: No audit trail β can't prove who deployed what Lambda code and when for compliance audits.
Q115: How do you implement Lambda resource limits and guardrails?β
Technical Answer: Use AWS Service Control Policies (SCPs), Lambda permission boundaries, and CloudFormation Guard rules to enforce: max memory, required VPC configuration, approved runtimes, required encryption, and mandatory DLQ configuration. Code/Syntax Justification:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": ["lambda:CreateFunction", "lambda:UpdateFunctionConfiguration"],
"Resource": "*",
"Condition": {
"NumericGreaterThan": { "lambda:FunctionMemorySize": 3008 }
}
},
{
"Effect": "Deny",
"Action": "lambda:CreateFunction",
"Resource": "*",
"Condition": {
"StringNotEquals": {
"lambda:Runtime": ["python3.12", "python3.11", "nodejs20.x"]
}
}
}
]
}
π Behind the Question: Organizational governance for serverless. β The Red Flags: A developer creates a Lambda with 10 GB memory and Python 3.8 runtime β no guardrails to prevent it.
Section 25: Advanced Serverless Patternsβ
Q116: How do you implement the CQRS pattern with Lambda?β
Technical Answer: Command Query Responsibility Segregation separates write (command) and read (query) paths. Writes go through Lambda β DynamoDB (optimized for writes). DynamoDB Streams β Lambda β OpenSearch/read-optimized store (optimized for complex queries). Each side scales independently. Code/Syntax Justification:
Write Path (Command):
API Gateway β Lambda β DynamoDB (single-table design)
- Optimized for transactional writes
- Simple key-value access patterns
Read Path (Query):
DynamoDB Stream β Lambda β OpenSearch Serverless
API Gateway β Lambda β OpenSearch (complex queries, full-text search, aggregations)
- Optimized for complex reads
- Eventually consistent with write path (~1-2 seconds lag)
π Behind the Question: Advanced architectural patterns for serverless. β The Red Flags: Trying to do complex aggregation queries on DynamoDB β wrong tool for analytical queries.
Q117: How do you implement the event sourcing pattern with Lambda?β
Technical Answer: Store every state change as an immutable event in DynamoDB (or Kinesis). Reconstruct current state by replaying events. Benefits: complete audit trail, temporal queries ("what was the order status at 3 PM?"), and event replay for debugging or reprocessing. Code/Syntax Justification:
# Store event
def store_event(aggregate_id: str, event_type: str, data: dict, version: int):
table.put_item(
Item={
'PK': f'AGG#{aggregate_id}',
'SK': f'EVENT#{version:010d}',
'event_type': event_type,
'data': data,
'timestamp': datetime.utcnow().isoformat(),
'version': version,
},
ConditionExpression='attribute_not_exists(PK)', # Optimistic concurrency
)
# Reconstruct state by replaying events
def get_current_state(aggregate_id: str) -> dict:
events = table.query(
KeyConditionExpression='PK = :pk AND begins_with(SK, :sk)',
ExpressionAttributeValues={':pk': f'AGG#{aggregate_id}', ':sk': 'EVENT#'},
)['Items']
state = {}
for event in events:
state = apply_event(state, event)
return state
π Behind the Question: Event-driven state management. β The Red Flags: Mutable state updates (UPDATE row) β lose history, can't audit, can't debug past states.
Q118: How do you implement multi-region active-active serverless architectures?β
Technical Answer: Deploy identical SAM stacks in multiple regions. Use Route 53 latency-based routing to direct users to the nearest region. DynamoDB Global Tables replicates data across regions. API Gateway custom domains with Route 53 health checks provide automatic failover. Code/Syntax Justification:
# DynamoDB Global Table
OrdersTable:
Type: AWS::DynamoDB::GlobalTable
Properties:
Replicas:
- Region: us-east-1
- Region: eu-west-1
- Region: ap-southeast-1
# Route 53 latency-based routing
DNSRecord:
Type: AWS::Route53::RecordSet
Properties:
Name: api.myapp.com
Type: A
SetIdentifier: us-east-1
Region: us-east-1
AliasTarget:
DNSName: !GetAtt ApiDomainName.RegionalDomainName
HostedZoneId: !GetAtt ApiDomainName.RegionalHostedZoneId
HealthCheckId: !Ref USEast1HealthCheck
π Behind the Question: Global serverless architecture. β The Red Flags: Single-region deployment for a global SaaS product β 200ms latency for users 10,000km away.
Q119: How do you implement feature flags with Lambda?β
Technical Answer: Use AWS AppConfig (part of Systems Manager) to deliver feature flags to Lambda without redeployment. The AppConfig Lambda extension caches flags locally with configurable polling intervals. Flag changes take effect within seconds across all Lambda instances. Code/Syntax Justification:
import requests
APPCONFIG_PORT = 2772 # Lambda extension port
def get_feature_flags():
url = f"http://localhost:{APPCONFIG_PORT}/applications/myapp/environments/prod/configurations/features"
response = requests.get(url, timeout=1)
return response.json()
def handler(event, context):
flags = get_feature_flags()
if flags.get('new_checkout_flow', False):
return new_checkout(event)
else:
return legacy_checkout(event)
# SAM: AppConfig extension layer
MyFunction:
Type: AWS::Serverless::Function
Properties:
Layers:
- !Sub "arn:aws:lambda:${AWS::Region}:027255383542:layer:AWS-AppConfig-Extension:100"
Environment:
Variables:
AWS_APPCONFIG_EXTENSION_POLL_INTERVAL_SECONDS: 30
π Behind the Question: Progressive delivery and deployment risk management. β The Red Flags: Deploying new code to 100% of users simultaneously β if it's broken, everyone is affected.
Q120: How do you implement a serverless GraphQL API with Lambda?β
Technical Answer: Use AWS AppSync (managed GraphQL) with Lambda resolvers, or deploy a self-managed GraphQL server (Strawberry, Ariadne) on Lambda behind API Gateway. AppSync provides built-in subscriptions (WebSocket), caching, and DynamoDB/Aurora direct resolvers. Code/Syntax Justification:
# Lambda-based GraphQL with Strawberry
import strawberry
from strawberry.fastapi import GraphQLRouter
from mangum import Mangum
@strawberry.type
class User:
id: str
name: str
email: str
@strawberry.type
class Query:
@strawberry.field
def user(self, id: str) -> User:
data = table.get_item(Key={'PK': f'USER#{id}', 'SK': 'PROFILE'})['Item']
return User(id=id, name=data['name'], email=data['email'])
@strawberry.field
def users(self, limit: int = 10) -> list[User]:
result = table.scan(Limit=limit)
return [User(id=i['PK'].split('#')[1], name=i['name'], email=i['email']) for i in result['Items']]
schema = strawberry.Schema(Query)
app = FastAPI()
app.include_router(GraphQLRouter(schema), prefix="/graphql")
handler = Mangum(app)
π Behind the Question: GraphQL on serverless infrastructure. β The Red Flags: REST APIs returning 50 fields when the client needs 3 β GraphQL solves over-fetching, but adds complexity.
Section 26: Serverless Testing Strategiesβ
Q121: How do you implement contract testing for Lambda-based microservices?β
Technical Answer: Contract testing verifies that Lambda functions honor the agreed-upon API contract (request/response schema) without end-to-end integration. Use Pact for consumer-driven contracts or JSON Schema validation in CI to verify Lambda input/output conformance. Code/Syntax Justification:
# Contract test with JSON Schema
import jsonschema
ORDER_RESPONSE_SCHEMA = {
"type": "object",
"required": ["order_id", "status", "total", "created_at"],
"properties": {
"order_id": {"type": "string", "pattern": "^ord-[a-z0-9]+$"},
"status": {"type": "string", "enum": ["pending", "confirmed", "shipped"]},
"total": {"type": "number", "minimum": 0},
"created_at": {"type": "string", "format": "date-time"},
}
}
def test_create_order_contract():
event = {'body': json.dumps({'product_id': 'prod-1', 'quantity': 2})}
result = handler(event, MockContext())
response_body = json.loads(result['body'])
jsonschema.validate(response_body, ORDER_RESPONSE_SCHEMA) # Fails if contract violated
π Behind the Question: Microservice testing without integration environment dependency. β The Red Flags: Only end-to-end tests β slow, fragile, and require all services to be running simultaneously.
Q122: How do you implement integration testing for Lambda with LocalStack?β
Technical Answer: LocalStack emulates AWS services locally (DynamoDB, S3, SQS, SNS, etc.). Run integration tests against LocalStack in CI β no AWS account needed, fast, deterministic, and free. Use testcontainers-python for Docker-based test fixtures.
Code/Syntax Justification:
import pytest
import boto3
import testcontainers.localstack
@pytest.fixture(scope='session')
def localstack():
with testcontainers.localstack.LocalStackContainer(image="localstack/localstack:3.0") as ls:
ls.with_services("dynamodb,s3,sqs")
yield ls
@pytest.fixture
def dynamodb_table(localstack):
client = boto3.resource('dynamodb', endpoint_url=localstack.get_url())
table = client.create_table(
TableName='orders',
KeySchema=[{'AttributeName': 'PK', 'KeyType': 'HASH'}, {'AttributeName': 'SK', 'KeyType': 'RANGE'}],
AttributeDefinitions=[{'AttributeName': 'PK', 'AttributeType': 'S'}, {'AttributeName': 'SK', 'AttributeType': 'S'}],
BillingMode='PAY_PER_REQUEST',
)
return table
def test_order_creation_integration(dynamodb_table):
# Full integration test against local DynamoDB
result = create_order(dynamodb_table, 'user-1', 'prod-1', 2)
assert result['status'] == 'created'
item = dynamodb_table.get_item(Key={'PK': f"ORDER#{result['order_id']}", 'SK': 'DETAILS'})
assert item['Item']['quantity'] == 2
π Behind the Question: Realistic integration testing without AWS costs. β The Red Flags: Skipping integration tests because "we can't run AWS locally" β LocalStack solves this.
Q123: How do you implement chaos engineering for Lambda?β
Technical Answer: Inject faults into Lambda to test resilience: simulate timeouts (add time.sleep()), inject errors (raise exceptions based on environment variable), simulate DynamoDB throttling, or use AWS Fault Injection Simulator (FIS) for managed chaos experiments.
Code/Syntax Justification:
import os
import random
def chaos_middleware(handler_fn):
"""Inject faults based on environment variables for chaos testing."""
def wrapper(event, context):
if os.environ.get('CHAOS_ENABLED') == 'true':
# Random latency injection
if random.random() < float(os.environ.get('CHAOS_LATENCY_RATE', '0')):
delay = float(os.environ.get('CHAOS_LATENCY_MS', '5000')) / 1000
time.sleep(delay)
# Random error injection
if random.random() < float(os.environ.get('CHAOS_ERROR_RATE', '0')):
raise Exception("Chaos: Injected failure")
return handler_fn(event, context)
return wrapper
@chaos_middleware
def handler(event, context):
return process(event)
π Behind the Question: Resilience verification and failure mode discovery. β The Red Flags: Assuming Lambda functions always succeed β never testing what happens when DynamoDB throttles or an external API times out.
Section 27: Lambda Migrations & Upgradesβ
Q124: How do you migrate from one Lambda runtime to another (e.g., Python 3.9 β 3.12)?β
Technical Answer: Test locally with the new runtime, check for breaking changes (removed stdlib modules, syntax changes), update requirements.txt for dependency compatibility, deploy with canary/linear traffic shifting, and monitor error rates during rollout.
Code/Syntax Justification:
# 1. Test locally with new runtime
sam build --use-container --build-image amazon/aws-sam-cli-build-image-python3.12
sam local invoke MyFunction -e test_event.json
# 2. Update SAM template
# Runtime: python3.12
# Architectures: [arm64] # Also migrate to Graviton for 20% savings
# 3. Deploy with canary
DeploymentPreference:
Type: Canary10Percent5Minutes
Alarms:
- !Ref ErrorRateAlarm
- !Ref LatencyAlarm
# 4. Monitor after deployment
# CloudWatch: check for new error types, latency changes
# X-Ray: compare traces between old and new versions
π Behind the Question: Runtime lifecycle management. β The Red Flags: Ignoring runtime deprecation notices β AWS stops patching security vulnerabilities for deprecated runtimes.
Q125: How do you implement Lambda function migration from ZIP to container image?β
Technical Answer: Create a Dockerfile based on the AWS Lambda base image, copy function code and dependencies, change PackageType from Zip to Image in SAM, push to ECR, and deploy. The function behavior is identical β only the packaging changes.
Code/Syntax Justification:
FROM public.ecr.aws/lambda/python:3.12
# Install system dependencies (not possible with ZIP)
RUN dnf install -y gcc libffi-devel
COPY requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir
COPY app/ ${LAMBDA_TASK_ROOT}/
CMD ["app.handler"]
# SAM: switch from ZIP to container
MyFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image # Changed from Zip
ImageUri: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/my-function:latest"
Metadata:
DockerTag: latest
DockerContext: ./src
Dockerfile: Dockerfile
π Behind the Question: Deployment packaging evolution. β The Red Flags: Fighting with Lambda ZIP 250MB limit when the container approach supports 10GB images.
Section 28: Serverless Microservices Communicationβ
Q126: How do you implement synchronous service-to-service communication in serverless?β
Technical Answer: Use Lambda Function URLs with IAM auth for direct invocation, or API Gateway with service-to-service IAM auth. Avoid Lambda-to-Lambda direct invocation (tight coupling). Prefer async communication (EventBridge, SQS) whenever possible. Code/Syntax Justification:
# Sync call via Lambda Function URL with SigV4
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
def call_user_service(user_id: str):
url = f"{USER_SERVICE_URL}/users/{user_id}"
request = AWSRequest(method='GET', url=url)
SigV4Auth(credentials, 'lambda', REGION).add_auth(request)
response = requests.get(url, headers=dict(request.headers), timeout=5)
response.raise_for_status()
return response.json()
# Prefer async when possible
def handler(event, context):
order = create_order(event)
# Don't call notification service synchronously
events.put_events(Entries=[{
'Source': 'order-service',
'DetailType': 'OrderCreated',
'Detail': json.dumps({'order_id': order['id']}),
}])
π Behind the Question: Microservice communication patterns. β The Red Flags: Synchronous cascade: API β Lambda A β Lambda B β Lambda C β Lambda D β latency multiplies, any failure kills the chain.
Q127: How do you implement the API composition pattern with Lambda?β
Technical Answer: An API composer Lambda aggregates data from multiple backend services into a single response. Use asyncio.gather() to call services in parallel, reducing total latency to the slowest service call rather than the sum of all calls.
Code/Syntax Justification:
import asyncio
import aiohttp
async def fetch_service(session, url):
async with session.get(url, timeout=aiohttp.ClientTimeout(total=5)) as resp:
return await resp.json()
async def compose_user_profile(user_id: str):
async with aiohttp.ClientSession() as session:
user_data, orders, preferences = await asyncio.gather(
fetch_service(session, f"{USER_SERVICE}/users/{user_id}"),
fetch_service(session, f"{ORDER_SERVICE}/users/{user_id}/orders"),
fetch_service(session, f"{PREF_SERVICE}/users/{user_id}/preferences"),
return_exceptions=True, # Don't fail all if one fails
)
return {
'user': user_data if not isinstance(user_data, Exception) else None,
'orders': orders if not isinstance(orders, Exception) else [],
'preferences': preferences if not isinstance(preferences, Exception) else {},
}
def handler(event, context):
result = asyncio.run(compose_user_profile(event['pathParameters']['user_id']))
return {'statusCode': 200, 'body': json.dumps(result)}
π Behind the Question: API aggregation and parallel data fetching. β The Red Flags: Sequential service calls β 3 services Γ 200ms each = 600ms; parallel = 200ms.
Q128: How do you implement the saga pattern with Lambda and EventBridge?β
Technical Answer: Choreography-based saga: each Lambda publishes success/failure events to EventBridge. Subsequent steps listen for these events. Compensating Lambda functions listen for failure events to rollback previous steps. No central orchestrator β each service is autonomous. Code/Syntax Justification:
# Step 1: Order Service β publishes OrderCreated or OrderFailed
def create_order(event, context):
try:
order = save_order(event['detail'])
publish_event('OrderCreated', {'order_id': order['id']})
except Exception as e:
publish_event('OrderFailed', {'order_id': event['detail']['order_id'], 'reason': str(e)})
# Step 2: Payment Service β listens for OrderCreated
def process_payment(event, context):
try:
charge_customer(event['detail']['order_id'])
publish_event('PaymentCompleted', event['detail'])
except Exception as e:
publish_event('PaymentFailed', event['detail'])
# Compensation: Order Service β listens for PaymentFailed
def rollback_order(event, context):
cancel_order(event['detail']['order_id'])
publish_event('OrderCancelled', event['detail'])
def publish_event(event_type, detail):
events.put_events(Entries=[{
'Source': 'order-saga',
'DetailType': event_type,
'Detail': json.dumps(detail),
}])
π Behind the Question: Distributed transaction management without a coordinator. β The Red Flags: No compensating transactions β payment fails after inventory is reserved, leaving inventory permanently locked.
Section 29: Lambda Anti-Patternsβ
Q129: What are the most common Lambda anti-patterns?β
Technical Answer: (1) Lambda-calling-Lambda chains (use Step Functions). (2) Monolithic Lambda (one function does everything). (3) Ignoring cold starts for user-facing paths. (4) No error handling. (5) Synchronous calls when async works. (6) Over-engineering with nano-functions (one per endpoint). (7) Not using environment variables. Code/Syntax Justification:
# ANTI-PATTERN 1: Lambda calling Lambda
def handler_a(event, context):
result = lambda_client.invoke(FunctionName='handler-b', Payload=json.dumps(event))
# Tight coupling, double billing, cascading timeouts
return json.loads(result['Payload'].read())
# CORRECT: Use Step Functions or EventBridge
# Step Functions orchestrates A β B with built-in retry and error handling
# ANTI-PATTERN 2: Monolithic Lambda (single function, huge handler)
def handler(event, context):
if event['path'] == '/users': return handle_users(event)
elif event['path'] == '/orders': return handle_orders(event)
elif event['path'] == '/products': return handle_products(event)
# 50 more routes...
# CORRECT: Separate functions per domain, or use a framework like FastAPI + Mangum
π Behind the Question: Serverless architecture maturity. β The Red Flags: All 5 anti-patterns present in a single application β sign of serverless inexperience.
Q130: What is the "Lambda cold start death spiral" and how do you prevent it?β
Technical Answer: A cold start death spiral happens when: cold start β timeout β retry β new cold start β timeout β retry β more cold starts. Each failed invocation spawns a new container with its own cold start. Prevention: warm the critical path with provisioned concurrency, set appropriate timeouts, and avoid cascading retries. Code/Syntax Justification:
# Prevention: Provisioned concurrency for critical paths
CriticalFunction:
Type: AWS::Serverless::Function
Properties:
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 5
Timeout: 30 # Don't set too short β cold start may exceed it
MemorySize: 1769 # 1 vCPU β faster init
# Prevention: Circuit breaker on downstream calls
def handler(event, context):
remaining = context.get_remaining_time_in_millis()
if remaining < 5000: # Less than 5 seconds left
logger.error("Insufficient time remaining, aborting early")
return {'statusCode': 503, 'body': 'Service temporarily unavailable'}
π Behind the Question: Operational resilience understanding. β The Red Flags: 3-second timeout on a function with 5-second cold start β 100% of cold invocations fail, creating more cold containers.
Section 30: Serverless Observability Platformβ
Q131: How do you build a comprehensive observability stack for serverless?β
Technical Answer: Three pillars: Logs (CloudWatch + structured JSON + Logs Insights queries), Metrics (CloudWatch Metrics + EMF + custom dashboards), Traces (X-Ray + service map + latency analysis). Add Powertools for Python to unify all three with minimal code. Code/Syntax Justification:
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.metrics import MetricUnit
logger = Logger(service="order-service", log_uncaught_exceptions=True)
tracer = Tracer(service="order-service")
metrics = Metrics(namespace="MyApp", service="order-service")
@logger.inject_lambda_context(correlation_id_path="requestContext.requestId")
@tracer.capture_lambda_handler
@metrics.log_metrics(capture_cold_start_metric=True)
def handler(event, context):
logger.info("Processing order", extra={"order_id": event.get("order_id")})
with tracer.provider.in_subsegment("validate_order") as subsegment:
validate(event)
subsegment.put_annotation("order_type", event.get("type"))
metrics.add_metric(name="OrdersProcessed", unit=MetricUnit.Count, value=1)
metrics.add_metric(name="OrderValue", unit=MetricUnit.Count, value=event.get("total", 0))
return {"statusCode": 200}
π Behind the Question: Production-grade observability.
β The Red Flags: print() statements as the only logging β no structure, no correlation IDs, no metrics, no traces.
Q132: How do you implement cross-service distributed tracing in serverless?β
Technical Answer: Pass the X-Ray trace ID through all service calls (API Gateway β Lambda β SQS β Lambda β DynamoDB). X-Ray auto-propagates through AWS SDK calls. For custom HTTP calls, add the X-Amzn-Trace-Id header. The service map shows the complete request flow across services.
Code/Syntax Justification:
# X-Ray trace propagation through HTTP calls
import requests
from aws_xray_sdk.core import xray_recorder
@xray_recorder.capture('call_external_service')
def call_external_service(data):
trace_header = xray_recorder.current_subsegment().get_origin_trace_header()
response = requests.post(
EXTERNAL_SERVICE_URL,
json=data,
headers={'X-Amzn-Trace-Id': trace_header.to_header_str()},
)
return response.json()
# SQS propagation (automatic with Powertools)
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType
processor = BatchProcessor(event_type=EventType.SQS)
tracer = Tracer()
@tracer.capture_method
def process_record(record):
# Trace context automatically extracted from SQS message attributes
return handle(record)
π Behind the Question: End-to-end request visibility. β The Red Flags: Traces that stop at the first Lambda β no visibility into downstream SQS processing, DynamoDB calls, or external API latency.
Q133: How do you implement alerting and on-call for serverless applications?β
Technical Answer: CloudWatch Alarms on: Lambda error rate, 5xx response rate, queue depth, DLQ message count, Kinesis iterator age, and custom business metrics. Route alarms to SNS β PagerDuty/Opsgenie for on-call notification. Use CloudWatch Composite Alarms to reduce alert noise. Code/Syntax Justification:
# Critical alerts
ErrorRateAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub "${AWS::StackName}-error-rate-high"
MetricName: Errors
Namespace: AWS/Lambda
Dimensions:
- Name: FunctionName
Value: !Ref MyFunction
Statistic: Sum
Period: 300
EvaluationPeriods: 2
Threshold: 5
ComparisonOperator: GreaterThanThreshold
AlarmActions: [!Ref PagerDutyTopic]
TreatMissingData: notBreaching
# Composite alarm β only page if BOTH error rate AND queue depth are high
CompositeAlarm:
Type: AWS::CloudWatch::CompositeAlarm
Properties:
AlarmName: !Sub "${AWS::StackName}-critical-composite"
AlarmRule: !Sub 'ALARM("${ErrorRateAlarm}") AND ALARM("${QueueDepthAlarm}")'
AlarmActions: [!Ref PagerDutyTopic]
π Behind the Question: Operational maturity and incident response. β The Red Flags: No alerts β discovering a broken Lambda function from customer complaints 12 hours later.
Section 31: Serverless Cost Analysisβ
Q134: How do you perform a detailed cost analysis of a serverless application?β
Technical Answer: Break down costs by: Lambda (invocations + duration), API Gateway (requests), DynamoDB (RCU/WCU + storage), S3 (requests + storage), CloudWatch (logs + metrics), X-Ray (traces), and data transfer. Use AWS Cost Explorer with resource-level tags. Code/Syntax Justification:
Monthly cost breakdown for a typical serverless app (1M requests/month):
Lambda:
Invocations: 1M Γ $0.20/M = $0.20
Duration: 1M Γ 512MB Γ 200ms = 100,000 GB-s Γ $0.0000166667 = $1.67
Subtotal: $1.87
API Gateway (HTTP API):
Requests: 1M Γ $1.00/M = $1.00
DynamoDB (On-Demand):
Writes: 500K Γ $1.25/M = $0.63
Reads: 2M Γ $0.25/M = $0.50
Storage: 10 GB Γ $0.25 = $2.50
Subtotal: $3.63
CloudWatch Logs:
Ingestion: 5 GB Γ $0.50 = $2.50
Storage: 5 GB Γ $0.03 = $0.15
TOTAL: ~$9.15/month for 1M requests
π Behind the Question: FinOps and cost-conscious architecture. β The Red Flags: "Serverless is free" β it's cheap at low scale but can be expensive at high scale without optimization.
Q135: How do you optimize Lambda costs for high-volume applications?β
Technical Answer: (1) Right-size memory with Power Tuning. (2) Use ARM64 (20% cheaper). (3) Batch SQS messages. (4) Filter events at source (EventBridge rules, SNS filter policies, S3 prefix filters). (5) Cache responses (API Gateway caching, ElastiCache). (6) Use Savings Plans for predictable workloads. Code/Syntax Justification:
# Cost optimization checklist
Globals:
Function:
Architectures: [arm64] # 20% cheaper
MemorySize: 512 # Right-sized (not default 128)
Runtime: python3.12 # Latest runtime = best performance
# Batch SQS processing (reduce invocations by 100x)
Events:
SQS:
Type: SQS
Properties:
BatchSize: 1000 # Process 1000 messages per invocation
MaximumBatchingWindowInSeconds: 30 # Wait to fill batch
# Filter events at source (prevent unnecessary invocations)
Events:
S3:
Type: S3
Properties:
Filter:
S3Key:
Rules:
- Name: suffix
Value: .csv # Only trigger for CSV files
π Behind the Question: Cost engineering at scale.
β The Red Flags: Processing 100M SQS messages with BatchSize: 1 β 100M Lambda invocations instead of 100K with batching.
Section 32: Serverless Frameworks Comparisonβ
Q136: How does AWS SAM compare to Serverless Framework and CDK?β
Technical Answer: SAM: AWS-native, YAML, extension of CloudFormation, best for pure-AWS serverless. Serverless Framework: multi-cloud, plugin ecosystem, community-driven. CDK: imperative (TypeScript/Python), full AWS resource support, best for complex infrastructure. Choose based on team expertise and complexity. Code/Syntax Justification:
SAM:
β
AWS-native, official support
β
`sam local` for testing
β
Simplified Lambda syntax
β AWS-only
β Limited to CloudFormation
Serverless Framework:
β
Multi-cloud (AWS, Azure, GCP)
β
Rich plugin ecosystem
β
Large community
β Third-party dependency
β v4 licensing changes
CDK:
β
Real programming languages (TypeScript, Python)
β
Full AWS resource support
β
Constructs for reusable patterns
β Steeper learning curve
β Generates CloudFormation (debugging is hard)
Terraform:
β
Multi-cloud
β
State management
β
Large ecosystem
β Not optimized for serverless
β Verbose for Lambda
π Behind the Question: Tooling selection and ecosystem awareness. β The Red Flags: Writing raw CloudFormation for a complex serverless app β SAM or CDK provides 80% less boilerplate.
Q137: How do you implement infrastructure testing for serverless applications?β
Technical Answer: Test at three levels: (1) Unit: test CloudFormation/SAM templates with cfn-lint and cfn-guard. (2) Integration: deploy to a test account and run integration tests. (3) Contract: verify IAM policies, resource configurations, and security settings with checkov or cfn-nag.
Code/Syntax Justification:
# Lint SAM template
cfn-lint template.yaml
# Security scanning
pip install checkov
checkov -f template.yaml --framework cloudformation
# Policy validation with cfn-guard
cfn-guard validate \
--data template.yaml \
--rules rules/lambda-security.guard
# rules/lambda-security.guard
let lambda_functions = Resources.*[ Type == 'AWS::Serverless::Function' ]
rule lambda_must_have_dlq {
%lambda_functions.Properties.DeadLetterQueue EXISTS
}
rule lambda_max_timeout {
%lambda_functions.Properties.Timeout <= 300
}
π Behind the Question: Infrastructure quality and security. β The Red Flags: No IaC testing β deploying broken CloudFormation templates to production.
Section 33: Lambda + Database Patternsβ
Q138: How do you implement connection pooling for RDS with Lambda?β
Technical Answer: Lambda's scaling model (thousands of concurrent instances) is incompatible with RDS connection limits (~100-1000). Use RDS Proxy as a connection pooler β it maintains a pool of database connections and multiplexes Lambda connections through them. Code/Syntax Justification:
# RDS Proxy configuration
DBProxy:
Type: AWS::RDS::DBProxy
Properties:
DBProxyName: lambda-rds-proxy
EngineFamily: POSTGRESQL
RequireTLS: true
RoleArn: !GetAtt ProxyRole.Arn
Auth:
- AuthScheme: SECRETS
SecretArn: !Ref DBSecret
IAMAuth: REQUIRED
VpcSubnetIds: [!Ref PrivateSubnet1, !Ref PrivateSubnet2]
# Lambda connects through proxy
MyFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
DB_HOST: !GetAtt DBProxy.Endpoint
VpcConfig:
SecurityGroupIds: [!Ref LambdaSG]
SubnetIds: [!Ref PrivateSubnet1, !Ref PrivateSubnet2]
π Behind the Question: Database connection management in serverless. β The Red Flags: 1000 concurrent Lambda invocations each opening a direct RDS connection β database crashes with "too many connections."
Q139: How do you implement Aurora Serverless v2 with Lambda?β
Technical Answer: Aurora Serverless v2 scales compute capacity automatically (0.5-128 ACUs). Combined with RDS Proxy and Lambda, it provides a fully serverless database stack. Aurora Serverless v2 scales to zero ACUs when idle (with v2 auto-pause), eliminating idle database costs. Code/Syntax Justification:
AuroraCluster:
Type: AWS::RDS::DBCluster
Properties:
Engine: aurora-postgresql
EngineMode: provisioned # v2 uses provisioned mode with serverless scaling
ServerlessV2ScalingConfiguration:
MinCapacity: 0.5 # Scale down to 0.5 ACU
MaxCapacity: 16 # Scale up to 16 ACU
DatabaseName: myapp
AuroraInstance:
Type: AWS::RDS::DBInstance
Properties:
DBClusterIdentifier: !Ref AuroraCluster
DBInstanceClass: db.serverless # Serverless v2 instance
Engine: aurora-postgresql
π Behind the Question: Fully serverless database architecture. β The Red Flags: Running a db.r5.xlarge 24/7 for a dev environment β Aurora Serverless v2 would cost $5/month instead of $300.
Q140: How do you implement database migrations in a serverless architecture?β
Technical Answer: Run Alembic/Django migrations as a Lambda function invoked during deployment (via CloudFormation custom resource or CodePipeline step). Use a separate "migration" Lambda with longer timeout and VPC access to the database. Never run migrations from the application Lambda. Code/Syntax Justification:
# migration_lambda.py
from alembic.config import Config
from alembic import command
def handler(event, context):
alembic_cfg = Config("alembic.ini")
alembic_cfg.set_main_option("sqlalchemy.url", os.environ['DATABASE_URL'])
if event.get('RequestType') == 'Create' or event.get('RequestType') == 'Update':
command.upgrade(alembic_cfg, "head")
return {'statusCode': 200, 'body': 'Migrations applied'}
return {'statusCode': 200, 'body': 'No migration needed'}
MigrationFunction:
Type: AWS::Serverless::Function
Properties:
Handler: migration_lambda.handler
Timeout: 300 # 5 minutes for long migrations
VpcConfig:
SecurityGroupIds: [!Ref LambdaSG]
SubnetIds: [!Ref PrivateSubnet1]
# Run during deployment
RunMigrations:
Type: Custom::Migrations
Properties:
ServiceToken: !GetAtt MigrationFunction.Arn
π Behind the Question: Schema management in serverless deployments.
β The Red Flags: Running alembic upgrade head manually via SSH β not reproducible, not auditable, error-prone.
Section 34: Serverless Security Hardeningβ
Q141: How do you implement least-privilege IAM for a complex serverless application?β
Technical Answer: Start with zero permissions and add only what each function needs. Use SAM policy templates for common patterns. Enable IAM Access Analyzer to identify unused permissions. Use permission boundaries to cap maximum privileges. Review with cloudsplaining or parliament.
Code/Syntax Justification:
# Per-function least-privilege policies
GetUserFunction:
Type: AWS::Serverless::Function
Properties:
Policies:
- DynamoDBReadPolicy:
TableName: !Ref UsersTable
# Only read β no write, no delete, no scan
CreateOrderFunction:
Type: AWS::Serverless::Function
Properties:
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
- SQSSendMessagePolicy:
QueueName: !GetAtt NotificationQueue.QueueName
# Can CRUD orders and send to notification queue β nothing else
# Permission boundary β caps what any function can ever do
PermissionBoundary:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Statement:
- Effect: Deny
Action: ["iam:*", "organizations:*", "s3:DeleteBucket"]
Resource: "*"
π Behind the Question: Security engineering depth.
β The Red Flags: Action: "*", Resource: "*" β one compromised Lambda can delete your entire AWS account.
Q142: How do you implement secrets rotation for Lambda?β
Technical Answer: Store secrets in Secrets Manager with automatic rotation enabled. Rotation Lambda runs on a schedule (every 30-90 days), creates a new credential, tests it, and updates the secret. Application Lambdas retrieve the current secret via the Secrets Manager extension (cached). Code/Syntax Justification:
DBSecret:
Type: AWS::SecretsManager::Secret
Properties:
GenerateSecretString:
SecretStringTemplate: '{"username": "admin"}'
GenerateStringKey: "password"
PasswordLength: 32
SecretRotation:
Type: AWS::SecretsManager::RotationSchedule
Properties:
SecretId: !Ref DBSecret
RotationLambdaARN: !GetAtt RotationFunction.Arn
RotationRules:
AutomaticallyAfterDays: 30
# Application Lambda retrieves current (possibly rotated) secret
MyFunction:
Type: AWS::Serverless::Function
Properties:
Layers:
- !Sub "arn:aws:lambda:${AWS::Region}:177933569100:layer:AWS-Parameters-and-Secrets-Lambda-Extension:11"
Environment:
Variables:
DB_SECRET_ARN: !Ref DBSecret
π Behind the Question: Credential lifecycle management. β The Red Flags: Database passwords that haven't been rotated in 3 years β one compromised password = permanent access.
Section 35: Lambda Best Practices Summaryβ
Q143: What are the top 10 Lambda production best practices?β
Technical Answer: (1) Module-level init for connection reuse. (2) Structured logging with correlation IDs. (3) X-Ray tracing. (4) acks_late equivalent (SQS ReportBatchItemFailures). (5) DLQ on every async invocation. (6) Idempotent handlers. (7) Least-privilege IAM. (8) ARM64 architecture. (9) Provisioned concurrency for latency-critical paths. (10) Infrastructure as Code (SAM/CDK).
Code/Syntax Justification:
# The perfect Lambda function template
import json
import os
import boto3
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.utilities.idempotency import idempotent, DynamoDBPersistenceLayer
logger = Logger()
tracer = Tracer()
metrics = Metrics()
# Module-level init β reused across invocations
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['TABLE_NAME'])
persistence = DynamoDBPersistenceLayer(table_name=os.environ['IDEMPOTENCY_TABLE'])
@logger.inject_lambda_context(correlation_id_path="requestContext.requestId")
@tracer.capture_lambda_handler
@metrics.log_metrics(capture_cold_start_metric=True)
@idempotent(persistence_store=persistence)
def handler(event, context):
try:
body = json.loads(event.get('body', '{}'))
result = process(body)
metrics.add_metric(name="Success", unit="Count", value=1)
return {'statusCode': 200, 'body': json.dumps(result)}
except ValidationError as e:
return {'statusCode': 400, 'body': json.dumps({'error': str(e)})}
except Exception as e:
logger.exception("Unhandled error")
metrics.add_metric(name="Error", unit="Count", value=1)
return {'statusCode': 500, 'body': json.dumps({'error': 'Internal error'})}
π Behind the Question: Comprehensive best practices application. β The Red Flags: Lambda functions missing more than 3 of these best practices β indicates serverless immaturity.
Q144: How do you evaluate when NOT to use Lambda?β
Technical Answer: Avoid Lambda for: (1) Long-running processes >15 min. (2) WebSocket/gRPC servers requiring persistent connections. (3) Workloads requiring >10 GB memory. (4) High-throughput, consistent load (>10M requests/hour, Fargate is cheaper). (5) Workloads with heavy local state. (6) GPU-dependent tasks. Code/Syntax Justification:
Use Lambda: Don't use Lambda:
β
Event-driven processing β Video transcoding (>15 min)
β
API backends (<15 min) β ML model training (GPU, hours)
β
Data transformation β WebSocket game server
β
Scheduled tasks (<15 min) β Persistent TCP connections
β
File processing triggers β 100+ concurrent DB connections
β
IoT message processing β Large in-memory caches (>10 GB)
β
Chatbot backends β Steady 10,000 req/sec (Fargate cheaper)
π Behind the Question: Architectural judgment β knowing when NOT to use a technology. β The Red Flags: Forcing Lambda on every workload β "when all you have is a hammer."
Section 36: Lambda Networking & VPC Patternsβ
Q145: How does Lambda VPC networking work and what are the cold start implications?β
Technical Answer: Lambda creates Hyperplane ENIs (Elastic Network Interfaces) shared across function instances within the same VPC/subnet/security-group combination. Since the 2019 improvement, VPC-enabled Lambdas no longer suffer significant cold start penalties because ENIs are pre-provisioned. However, you must provide sufficient IP addresses in your subnets and use at least two subnets across AZs for HA.
Code/Syntax Justification:
# SAM template with VPC configuration
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
VpcConfig:
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroupIds:
- !Ref LambdaSecurityGroup
Policies:
- VPCAccessPolicy: {}
# Verify VPC connectivity in handler
import socket
def handler(event, context):
# Test internal DNS resolution
try:
ip = socket.gethostbyname("internal-service.corp.local")
return {"status": "connected", "resolved_ip": ip}
except socket.gaierror as e:
return {"status": "dns_failed", "error": str(e)}
π Behind the Question: Understanding Lambda networking fundamentals for enterprise deployments. β The Red Flags: Placing Lambda in VPC unnecessarily or not providing multi-AZ subnets.
Q146: How do you access the internet from a VPC-enabled Lambda?β
Technical Answer: VPC-enabled Lambdas lose direct internet access. To reach the internet, route traffic through a NAT Gateway in a public subnet while the Lambda runs in a private subnet. For AWS services only, use VPC Endpoints (Gateway for S3/DynamoDB, Interface for others) to avoid NAT Gateway costs.
Code/Syntax Justification:
# VPC Endpoint for S3 (free, no NAT needed)
Resources:
S3Endpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
VpcId: !Ref VPC
ServiceName: !Sub "com.amazonaws.${AWS::Region}.s3"
VpcEndpointType: Gateway
RouteTableIds:
- !Ref PrivateRouteTable
# Interface endpoint for Secrets Manager
SecretsEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
VpcId: !Ref VPC
ServiceName: !Sub "com.amazonaws.${AWS::Region}.secretsmanager"
VpcEndpointType: Interface
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
PrivateDnsEnabled: true
π Behind the Question: Network architecture for Lambda in enterprise VPCs. β The Red Flags: Using NAT Gateway for all AWS service calls instead of VPC Endpoints.
Q147: How do you configure Lambda security groups for database access?β
Technical Answer: Create a dedicated security group for Lambda that allows outbound traffic to the database port. The database security group should allow inbound from the Lambda security group (reference by SG ID, not CIDR). This enables secure, IP-independent access control.
Code/Syntax Justification:
Resources:
LambdaSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Lambda function security group
VpcId: !Ref VPC
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 5432
ToPort: 5432
DestinationSecurityGroupId: !Ref DatabaseSG
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0 # HTTPS for AWS APIs
DatabaseSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: RDS security group
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 5432
ToPort: 5432
SourceSecurityGroupId: !Ref LambdaSG # Reference by SG, not IP
π Behind the Question: Least-privilege network security for serverless.
β The Red Flags: Using 0.0.0.0/0 in database security group ingress rules.
Q148: How do you handle DNS resolution for private resources in Lambda VPC?β
Technical Answer: VPC-enabled Lambdas use the VPC's DNS resolver (Amazon-provided DNS at VPC CIDR +2). For Route 53 Private Hosted Zones, ensure the zone is associated with the Lambda's VPC. For on-premises DNS via Direct Connect, configure Route 53 Resolver endpoints.
Code/Syntax Justification:
import boto3
import socket
def handler(event, context):
# Resolve private hostname
hostname = "api.internal.company.com"
try:
ip = socket.gethostbyname(hostname)
print(f"Resolved {hostname} to {ip}")
except socket.gaierror:
print(f"Failed to resolve {hostname}")
# Fallback: use Route 53 Resolver
client = boto3.client("route53resolver")
raise
# For RDS, use the endpoint hostname (auto-resolves to current primary)
rds_host = "mydb.cluster-abc123.us-east-1.rds.amazonaws.com"
rds_ip = socket.gethostbyname(rds_host)
return {"rds_ip": rds_ip}
π Behind the Question: DNS architecture for hybrid cloud Lambda deployments. β The Red Flags: Hardcoding IP addresses instead of using DNS hostnames for internal services.
Q149: How do you implement Lambda-to-Lambda communication patterns?β
Technical Answer:
Direct invocation (synchronous or async) via invoke(), event-driven via SQS/SNS/EventBridge, or shared state via DynamoDB/ElastiCache. Prefer async event-driven patterns for loose coupling. Direct invocation creates tight coupling and cascading failures.
Code/Syntax Justification:
import boto3
import json
lambda_client = boto3.client("lambda")
# Anti-pattern: synchronous direct invocation (tight coupling)
def handler_sync(event, context):
response = lambda_client.invoke(
FunctionName="downstream-function",
InvocationType="RequestResponse", # Synchronous β blocks
Payload=json.dumps({"data": event["body"]}),
)
return json.loads(response["Payload"].read())
# Better: async invocation (fire-and-forget)
def handler_async(event, context):
lambda_client.invoke(
FunctionName="downstream-function",
InvocationType="Event", # Async β returns immediately
Payload=json.dumps({"data": event["body"]}),
)
return {"statusCode": 202, "body": "Accepted"}
# Best: event-driven via SQS (decoupled, durable)
sqs = boto3.client("sqs")
def handler_decoupled(event, context):
sqs.send_message(
QueueUrl="https://sqs.us-east-1.amazonaws.com/123/my-queue",
MessageBody=json.dumps({"data": event["body"]}),
)
return {"statusCode": 202, "body": "Queued"}
π Behind the Question: Service communication patterns in serverless architectures. β The Red Flags: Chains of synchronous Lambda-to-Lambda calls creating distributed monoliths.
Q150: What is AWS PrivateLink and how does it relate to Lambda?β
Technical Answer: PrivateLink creates private Interface VPC Endpoints that keep traffic within the AWS network. Lambda uses PrivateLink to securely access AWS services, third-party SaaS, and your own services without traversing the internet. Each endpoint creates ENIs in your subnets.
Code/Syntax Justification:
import boto3
# Lambda accessing SQS via VPC Endpoint (PrivateLink)
# No code change needed β SDK auto-uses the endpoint
sqs = boto3.client("sqs") # Traffic stays on AWS network
# For custom services, use the VPC endpoint DNS
# endpoint: vpce-abc123.execute-api.us-east-1.vpce.amazonaws.com
import requests
def handler(event, context):
# Access partner API via PrivateLink
response = requests.get(
"https://vpce-abc123.execute-api.us-east-1.vpce.amazonaws.com/prod/data",
headers={"Host": "partner-api.example.com"},
)
return {"statusCode": 200, "body": response.json()}
π Behind the Question: Private connectivity for zero-trust Lambda architectures. β The Red Flags: Routing sensitive API calls over the public internet when PrivateLink is available.
Q151: How do you handle IP address exhaustion in Lambda VPC subnets?β
Technical Answer:
Each unique (security-group, subnet) pair shares a Hyperplane ENI, not one ENI per invocation. However, heavy concurrency can still exhaust IPs. Use /19 or larger subnets for Lambda, monitor ENIUtilization CloudWatch metric, and configure Reserved Concurrency to cap IP usage.
Code/Syntax Justification:
import ipaddress
def calculate_subnet_capacity(cidr: str) -> dict:
network = ipaddress.ip_network(cidr)
total_ips = network.num_addresses
usable_ips = total_ips - 5 # AWS reserves 5 IPs per subnet
return {
"cidr": cidr,
"total_ips": total_ips,
"usable_ips": usable_ips,
"recommended_for_lambda": usable_ips > 100,
}
# Recommended minimum: /24 (251 usable) per AZ for Lambda
print(calculate_subnet_capacity("10.0.1.0/24")) # 251 usable
print(calculate_subnet_capacity("10.0.0.0/19")) # 8187 usable β better
# Monitor in CloudWatch
# Metric: AWS/Lambda/ENIUtilization
# Alarm when > 80% capacity
π Behind the Question: Network capacity planning for high-concurrency Lambda. β The Red Flags: Using /28 subnets for Lambda or not monitoring ENI utilization.
Q152: How do you implement cross-account Lambda invocation securely?β
Technical Answer:
Use resource-based policies on the target Lambda to allow the source account's IAM role to invoke it. The source account's role needs lambda:InvokeFunction permission. For event-driven patterns, use cross-account SQS, SNS, or EventBridge.
Code/Syntax Justification:
import boto3
import json
# Source account: invoke Lambda in target account
lambda_client = boto3.client("lambda")
def handler(event, context):
response = lambda_client.invoke(
FunctionName="arn:aws:lambda:us-east-1:TARGET_ACCOUNT:function:shared-service",
InvocationType="RequestResponse",
Payload=json.dumps({"action": "process", "data": event}),
)
return json.loads(response["Payload"].read())
// Target Lambda resource policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCrossAccountInvoke",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::SOURCE_ACCOUNT:role/invoker-role" },
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-1:TARGET_ACCOUNT:function:shared-service"
}
]
}
π Behind the Question: Multi-account Lambda architecture for enterprise isolation. β The Red Flags: Using overly broad cross-account permissions or sharing credentials.
Q153: How do you configure Lambda with Elastic File System (EFS)?β
Technical Answer: Lambda can mount EFS file systems for shared, persistent storage accessible across invocations and functions. EFS requires VPC configuration, an access point for Lambda-specific POSIX permissions, and throughput mode consideration. Useful for ML models, large config files, or shared data.
Code/Syntax Justification:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
VpcConfig:
SubnetIds: [!Ref PrivateSubnet1, !Ref PrivateSubnet2]
SecurityGroupIds: [!Ref LambdaSG]
FileSystemConfigs:
- Arn: !GetAtt EFSAccessPoint.Arn
LocalMountPath: /mnt/data
Policies:
- EFSWriteAccessPolicy:
FileSystem: !Ref EFSFileSystem
AccessPoint: !Ref EFSAccessPoint
import os
def handler(event, context):
# EFS mounted at /mnt/data β read/write like local filesystem
model_path = "/mnt/data/models/latest.pkl"
if os.path.exists(model_path):
import pickle
with open(model_path, "rb") as f:
model = pickle.load(f)
prediction = model.predict(event["features"])
return {"prediction": prediction.tolist()}
return {"error": "Model not found"}
π Behind the Question: Shared persistent storage for serverless compute. β The Red Flags: Using /tmp for data that needs to persist across invocations or be shared between functions.
Q154: How do you implement Lambda function URLs as lightweight API endpoints?β
Technical Answer: Function URLs provide HTTPS endpoints without API Gateway, reducing cost and latency for simple use cases. They support IAM auth or no auth (public), CORS configuration, and streaming responses. No request throttling, caching, or custom domains without CloudFront.
Code/Syntax Justification:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
FunctionUrlConfig:
AuthType: AWS_IAM # or NONE for public
Cors:
AllowOrigins: ["https://myapp.com"]
AllowMethods: ["GET", "POST"]
AllowHeaders: ["Content-Type", "Authorization"]
MaxAge: 3600
def handler(event, context):
# Function URL event structure differs from API Gateway
method = event.get("requestContext", {}).get("http", {}).get("method")
path = event.get("rawPath", "/")
body = event.get("body", "")
if method == "GET" and path == "/health":
return {"statusCode": 200, "body": '{"status": "healthy"}'}
if method == "POST" and path == "/process":
import json
data = json.loads(body) if body else {}
result = process(data)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(result),
}
return {"statusCode": 404, "body": "Not found"}
π Behind the Question: Simplified HTTP endpoints without API Gateway overhead. β The Red Flags: Using Function URLs for production APIs that need throttling, caching, or custom domains.
Q155: How do you implement multi-region Lambda deployment for disaster recovery?β
Technical Answer: Deploy identical Lambda stacks in multiple regions using CloudFormation StackSets or CDK Pipelines. Use Route 53 with health checks for failover routing. Replicate data with DynamoDB Global Tables, S3 Cross-Region Replication, and Aurora Global Database.
Code/Syntax Justification:
# Multi-region health check handler
import boto3
import os
REGION = os.environ["AWS_REGION"]
PRIMARY_REGION = os.environ.get("PRIMARY_REGION", "us-east-1")
def health_handler(event, context):
checks = {
"region": REGION,
"is_primary": REGION == PRIMARY_REGION,
"dynamodb": check_dynamodb(),
"timestamp": context.get_remaining_time_in_millis(),
}
all_healthy = all(v for k, v in checks.items() if k not in ("region", "is_primary", "timestamp"))
return {
"statusCode": 200 if all_healthy else 503,
"body": json.dumps(checks),
}
def check_dynamodb():
try:
table = boto3.resource("dynamodb").Table(os.environ["TABLE_NAME"])
table.get_item(Key={"pk": "health_check"})
return True
except Exception:
return False
π Behind the Question: High availability and disaster recovery for serverless. β The Red Flags: Single-region Lambda deployments for critical production workloads.
Q156: How do you handle Lambda concurrency limits to protect downstream services?β
Technical Answer:
Use Reserved Concurrency to cap a function's concurrency, preventing it from consuming all account-level concurrency or overwhelming downstream resources. Provisioned Concurrency pre-warms instances for latency-sensitive paths. Monitor Throttles metric.
Code/Syntax Justification:
Resources:
# Function that writes to RDS β limit to match connection pool
DatabaseWriter:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
ReservedConcurrentExecutions: 50 # Match RDS max_connections / safety_factor
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 10 # Pre-warmed instances
# API handler with auto-scaling provisioned concurrency
ApiHandler:
Type: AWS::Serverless::Function
Properties:
Handler: api.handler
Runtime: python3.12
AutoPublishAlias: live
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 5
# Monitor throttling
import boto3
def check_throttling():
cw = boto3.client("cloudwatch")
response = cw.get_metric_statistics(
Namespace="AWS/Lambda",
MetricName="Throttles",
Dimensions=[{"Name": "FunctionName", "Value": "my-function"}],
StartTime=datetime.utcnow() - timedelta(hours=1),
EndTime=datetime.utcnow(),
Period=300,
Statistics=["Sum"],
)
return sum(dp["Sum"] for dp in response["Datapoints"])
π Behind the Question: Concurrency management to prevent cascade failures. β The Red Flags: No reserved concurrency on functions that access limited-connection databases.
Q157: How do you implement WebSocket APIs with Lambda?β
Technical Answer:
API Gateway WebSocket APIs route $connect, $disconnect, and custom routes to Lambda functions. Store connection IDs in DynamoDB. Use the API Gateway Management API to push messages back to connected clients. Each route maps to a separate Lambda handler.
Code/Syntax Justification:
import boto3
import json
import os
dynamodb = boto3.resource("dynamodb")
connections_table = dynamodb.Table(os.environ["CONNECTIONS_TABLE"])
def connect_handler(event, context):
connection_id = event["requestContext"]["connectionId"]
connections_table.put_item(Item={"connectionId": connection_id})
return {"statusCode": 200}
def disconnect_handler(event, context):
connection_id = event["requestContext"]["connectionId"]
connections_table.delete_item(Key={"connectionId": connection_id})
return {"statusCode": 200}
def message_handler(event, context):
connection_id = event["requestContext"]["connectionId"]
domain = event["requestContext"]["domainName"]
stage = event["requestContext"]["stage"]
body = json.loads(event.get("body", "{}"))
# Broadcast to all connected clients
apigw = boto3.client("apigatewaymanagementapi",
endpoint_url=f"https://{domain}/{stage}")
connections = connections_table.scan()["Items"]
for conn in connections:
try:
apigw.post_to_connection(
ConnectionId=conn["connectionId"],
Data=json.dumps({"message": body.get("message"), "from": connection_id}),
)
except apigw.exceptions.GoneException:
connections_table.delete_item(Key={"connectionId": conn["connectionId"]})
return {"statusCode": 200}
π Behind the Question: Real-time communication patterns with serverless. β The Red Flags: Not cleaning up stale connections or scanning full table for broadcasts at scale.
Q158: How do you implement Lambda@Edge vs CloudFront Functions?β
Technical Answer: Lambda@Edge runs at CloudFront edge locations for viewer/origin request/response manipulation. CloudFront Functions run at even more locations but are limited to lightweight viewer request/response transforms (URL rewrites, header manipulation). Lambda@Edge supports Node.js/Python, CloudFront Functions use JavaScript only.
Code/Syntax Justification:
# Lambda@Edge: origin request (add auth header)
def origin_request_handler(event, context):
request = event["Records"][0]["cf"]["request"]
# Add authentication for origin
import hmac, hashlib, time
timestamp = str(int(time.time()))
signature = hmac.new(b"secret_key", timestamp.encode(), hashlib.sha256).hexdigest()
request["headers"]["x-auth-timestamp"] = [{"key": "X-Auth-Timestamp", "value": timestamp}]
request["headers"]["x-auth-signature"] = [{"key": "X-Auth-Signature", "value": signature}]
return request
# Lambda@Edge: viewer response (add security headers)
def viewer_response_handler(event, context):
response = event["Records"][0]["cf"]["response"]
headers = response["headers"]
headers["strict-transport-security"] = [{"key": "Strict-Transport-Security", "value": "max-age=31536000; includeSubdomains"}]
headers["x-content-type-options"] = [{"key": "X-Content-Type-Options", "value": "nosniff"}]
headers["x-frame-options"] = [{"key": "X-Frame-Options", "value": "DENY"}]
return response
π Behind the Question: Edge computing patterns for global applications. β The Red Flags: Using Lambda@Edge for simple URL rewrites that CloudFront Functions can handle.
Q159: How do you handle Lambda IP whitelisting requirements?β
Technical Answer: Lambda in a VPC with NAT Gateway gets a fixed Elastic IP for outbound traffic. Assign an EIP to the NAT Gateway and provide that IP to third-party services for whitelisting. Without VPC, Lambda uses dynamic AWS IP ranges that change frequently.
Code/Syntax Justification:
Resources:
# NAT Gateway with fixed EIP for whitelisting
EIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NATGateway:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EIP.AllocationId
SubnetId: !Ref PublicSubnet
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
PrivateRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NATGateway
# Verify outbound IP
import requests
def handler(event, context):
response = requests.get("https://api.ipify.org?format=json")
outbound_ip = response.json()["ip"]
# This should match your NAT Gateway EIP
return {"outbound_ip": outbound_ip}
π Behind the Question: Fixed IP requirements for enterprise integrations. β The Red Flags: Trying to whitelist Lambda IPs without NAT Gateway/EIP setup.
Section 37: Advanced Error Handling & Resilienceβ
Q160: How do you implement the circuit breaker pattern in Lambda?β
Technical Answer: Store circuit state (closed/open/half-open) in DynamoDB or Parameter Store. When error rate exceeds a threshold, open the circuit to fail fast without calling the downstream service. After a timeout, allow a test request (half-open). Reset to closed if it succeeds.
Code/Syntax Justification:
import boto3
import time
import json
dynamodb = boto3.resource("dynamodb")
circuit_table = dynamodb.Table("circuit_breakers")
class CircuitBreaker:
def __init__(self, service_name: str, failure_threshold: int = 5,
recovery_timeout: int = 60):
self.service_name = service_name
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
def get_state(self) -> dict:
item = circuit_table.get_item(Key={"service": self.service_name}).get("Item", {})
return {
"state": item.get("state", "closed"),
"failure_count": int(item.get("failure_count", 0)),
"last_failure": float(item.get("last_failure", 0)),
}
def is_available(self) -> bool:
state = self.get_state()
if state["state"] == "closed":
return True
if state["state"] == "open":
if time.time() - state["last_failure"] > self.recovery_timeout:
self._set_state("half-open")
return True # Allow one test request
return False
return True # half-open: allow test
def record_success(self):
circuit_table.put_item(Item={
"service": self.service_name,
"state": "closed",
"failure_count": 0,
"last_failure": 0,
})
def record_failure(self):
state = self.get_state()
new_count = state["failure_count"] + 1
new_state = "open" if new_count >= self.failure_threshold else "closed"
circuit_table.put_item(Item={
"service": self.service_name,
"state": new_state,
"failure_count": new_count,
"last_failure": str(time.time()),
})
def _set_state(self, state: str):
circuit_table.update_item(
Key={"service": self.service_name},
UpdateExpression="SET #s = :s",
ExpressionAttributeNames={"#s": "state"},
ExpressionAttributeValues={":s": state},
)
# Usage in handler
cb = CircuitBreaker("payment-api")
def handler(event, context):
if not cb.is_available():
return {"statusCode": 503, "body": "Service temporarily unavailable"}
try:
result = call_payment_api(event)
cb.record_success()
return {"statusCode": 200, "body": json.dumps(result)}
except Exception as e:
cb.record_failure()
raise
π Behind the Question: Resilience patterns for distributed serverless systems. β The Red Flags: No circuit breaker protection for external API calls.
Q161: How do you implement idempotent Lambda handlers?β
Technical Answer:
Use a unique idempotency key (from event or client) stored in DynamoDB with a TTL. Before processing, check if the key exists. If it does, return the cached response. Use DynamoDB conditional writes (attribute_not_exists) for atomic check-and-set.
Code/Syntax Justification:
import boto3
import hashlib
import json
import time
dynamodb = boto3.resource("dynamodb")
idempotency_table = dynamodb.Table("idempotency")
def handler(event, context):
# Generate idempotency key from event
idem_key = hashlib.sha256(json.dumps(event, sort_keys=True).encode()).hexdigest()
ttl = int(time.time()) + 86400 # 24 hour TTL
# Check for existing result
try:
idempotency_table.put_item(
Item={
"idempotency_key": idem_key,
"status": "PROCESSING",
"ttl": ttl,
},
ConditionExpression="attribute_not_exists(idempotency_key)",
)
except dynamodb.meta.client.exceptions.ConditionalCheckFailedException:
# Already processed β return cached result
existing = idempotency_table.get_item(Key={"idempotency_key": idem_key})["Item"]
if existing.get("status") == "COMPLETED":
return json.loads(existing["result"])
# Still processing β return accepted
return {"statusCode": 202, "body": "Processing"}
try:
result = process_event(event)
idempotency_table.update_item(
Key={"idempotency_key": idem_key},
UpdateExpression="SET #s = :s, #r = :r",
ExpressionAttributeNames={"#s": "status", "#r": "result"},
ExpressionAttributeValues={":s": "COMPLETED", ":r": json.dumps(result)},
)
return result
except Exception as e:
idempotency_table.delete_item(Key={"idempotency_key": idem_key})
raise
π Behind the Question: At-least-once delivery semantics require idempotent processing. β The Red Flags: Lambda handlers that produce duplicate side effects on retry.
Q162: How do you implement retry strategies with exponential backoff in Lambda?β
Technical Answer:
Use built-in retry behavior for async invocations (2 retries with backoff). For synchronous paths, implement exponential backoff with jitter in code. For SQS triggers, configure maxReceiveCount and VisibilityTimeout. Use Retry in Step Functions for orchestrated retries.
Code/Syntax Justification:
import time
import random
from functools import wraps
def retry_with_backoff(max_retries=3, base_delay=1.0, max_delay=30.0):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(max_retries + 1):
try:
return func(*args, **kwargs)
except (ConnectionError, TimeoutError) as e:
if attempt == max_retries:
raise
delay = min(base_delay * (2 ** attempt), max_delay)
jitter = random.uniform(0, delay * 0.5)
time.sleep(delay + jitter)
print(f"Retry {attempt + 1}/{max_retries} after {delay + jitter:.1f}s")
return wrapper
return decorator
@retry_with_backoff(max_retries=3, base_delay=0.5)
def call_external_api(payload):
response = requests.post("https://api.partner.com/process", json=payload, timeout=5)
response.raise_for_status()
return response.json()
def handler(event, context):
remaining_ms = context.get_remaining_time_in_millis()
if remaining_ms < 10000: # Less than 10s left β don't retry
return call_external_api(event)
return call_external_api(event)
π Behind the Question: Fault-tolerant external service calls. β The Red Flags: Fixed-delay retries without jitter (causes thundering herd on recovery).
Q163: How do you handle partial batch failures with SQS Lambda triggers?β
Technical Answer:
Return batchItemFailures with the itemIdentifier of failed messages. Lambda re-processes only failed messages instead of the entire batch. Enable FunctionResponseTypes: ReportBatchItemFailures in the event source mapping.
Code/Syntax Justification:
def handler(event, context):
batch_failures = []
for record in event["Records"]:
try:
message = json.loads(record["body"])
process_message(message)
except Exception as e:
print(f"Failed to process {record['messageId']}: {e}")
batch_failures.append({
"itemIdentifier": record["messageId"]
})
return {"batchItemFailures": batch_failures}
# SAM configuration
Resources:
MySQSFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Events:
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt MyQueue.Arn
BatchSize: 10
FunctionResponseTypes:
- ReportBatchItemFailures
π Behind the Question: Efficient error handling in batch processing. β The Red Flags: Failing the entire batch when one message has an error.
Q164: How do you implement dead letter queue processing and replay?β
Technical Answer: Configure DLQ on Lambda (for async invocations) or on SQS (for queue triggers). DLQ messages contain the original payload plus error metadata. Build a replay mechanism that moves messages from DLQ back to the main queue after fixing the issue.
Code/Syntax Justification:
import boto3
import json
sqs = boto3.client("sqs")
def replay_dlq(dlq_url: str, main_queue_url: str, max_messages: int = 100):
"""Move messages from DLQ back to main queue."""
replayed = 0
while replayed < max_messages:
response = sqs.receive_message(
QueueUrl=dlq_url,
MaxNumberOfMessages=10,
WaitTimeSeconds=1,
)
messages = response.get("Messages", [])
if not messages:
break
for msg in messages:
sqs.send_message(
QueueUrl=main_queue_url,
MessageBody=msg["Body"],
MessageAttributes=msg.get("MessageAttributes", {}),
)
sqs.delete_message(
QueueUrl=dlq_url,
ReceiptHandle=msg["ReceiptHandle"],
)
replayed += 1
return {"replayed": replayed}
# DLQ processor β analyze and alert on failures
def dlq_handler(event, context):
for record in event["Records"]:
body = json.loads(record["body"])
attributes = record.get("attributes", {})
receive_count = int(attributes.get("ApproximateReceiveCount", 0))
# Log failure context
print(json.dumps({
"level": "ERROR",
"message": "DLQ message received",
"original_message": body,
"receive_count": receive_count,
"sent_timestamp": attributes.get("SentTimestamp"),
}))
# Send alert for persistent failures
if receive_count > 3:
send_alert(f"Message failed {receive_count} times", body)
π Behind the Question: Error recovery and observability for async processing. β The Red Flags: DLQ messages accumulating without monitoring, alerting, or replay capability.
Q165: How do you implement graceful degradation in Lambda?β
Technical Answer: When downstream services fail, return cached/default responses instead of errors. Prioritize core functionality over optional features. Use feature flags to disable non-critical paths during outages. Log degraded responses for later reconciliation.
Code/Syntax Justification:
import boto3
import json
dynamodb = boto3.resource("dynamodb")
cache_table = dynamodb.Table("response_cache")
def handler(event, context):
user_id = event["pathParameters"]["userId"]
# Primary path: real-time data
try:
profile = get_user_profile(user_id) # External service call
# Cache successful response
cache_table.put_item(Item={
"key": f"profile:{user_id}",
"data": json.dumps(profile),
"ttl": int(time.time()) + 3600,
})
return {"statusCode": 200, "body": json.dumps(profile)}
except Exception as e:
print(f"Primary service failed: {e}")
# Degraded path: cached data
try:
cached = cache_table.get_item(Key={"key": f"profile:{user_id}"}).get("Item")
if cached:
return {
"statusCode": 200,
"headers": {"X-Data-Source": "cache", "X-Cache-Age": "stale"},
"body": cached["data"],
}
except Exception:
pass
# Minimal path: default response
return {
"statusCode": 200,
"headers": {"X-Data-Source": "default"},
"body": json.dumps({"user_id": user_id, "status": "limited_data"}),
}
π Behind the Question: Availability over consistency in user-facing services. β The Red Flags: Returning 500 errors to users when cached or default responses are available.
Q166: How do you implement Lambda function composition with Step Functions error handling?β
Technical Answer:
Step Functions provides built-in retry, catch, and fallback states. Use Retry for transient errors with backoff, Catch for specific error types with fallback states, and ResultPath to preserve original input alongside error information.
Code/Syntax Justification:
{
"StartAt": "ProcessOrder",
"States": {
"ProcessOrder": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123:function:process-order",
"Retry": [
{
"ErrorEquals": ["ServiceUnavailable", "TooManyRequestsException"],
"IntervalSeconds": 2,
"MaxAttempts": 3,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.TaskFailed"],
"IntervalSeconds": 5,
"MaxAttempts": 2,
"BackoffRate": 1.5
}
],
"Catch": [
{
"ErrorEquals": ["PaymentDeclined"],
"Next": "HandlePaymentFailure",
"ResultPath": "$.error"
},
{
"ErrorEquals": ["States.ALL"],
"Next": "HandleUnexpectedError",
"ResultPath": "$.error"
}
],
"Next": "SendConfirmation"
},
"HandlePaymentFailure": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123:function:notify-payment-failure",
"End": true
},
"HandleUnexpectedError": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123:function:log-error-and-alert",
"End": true
},
"SendConfirmation": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123:function:send-confirmation",
"End": true
}
}
}
π Behind the Question: Orchestrated error handling for multi-step workflows. β The Red Flags: Implementing retry/catch logic in Lambda code when Step Functions handles it natively.
Q167: How do you handle Lambda timeout scenarios gracefully?β
Technical Answer:
Monitor remaining time with context.get_remaining_time_in_millis(). When time is running low, checkpoint progress, clean up resources, and return a partial result or enqueue remaining work. Set CloudWatch alarms on Duration approaching the timeout.
Code/Syntax Justification:
import json
import boto3
import time
sqs = boto3.client("sqs")
def handler(event, context):
items = event.get("items", [])
processed = []
failed = []
buffer_ms = 5000 # Reserve 5 seconds for cleanup
for i, item in enumerate(items):
remaining = context.get_remaining_time_in_millis()
if remaining < buffer_ms:
# Time running out β checkpoint and requeue remaining
remaining_items = items[i:]
sqs.send_message(
QueueUrl=os.environ["QUEUE_URL"],
MessageBody=json.dumps({"items": remaining_items}),
)
print(f"Checkpoint: {len(processed)} processed, {len(remaining_items)} requeued")
break
try:
result = process_item(item)
processed.append(result)
except Exception as e:
failed.append({"item": item, "error": str(e)})
return {
"processed": len(processed),
"failed": len(failed),
"remaining_requeued": len(items) - len(processed) - len(failed),
}
π Behind the Question: Time-aware processing for long-running batches. β The Red Flags: Ignoring remaining execution time and getting killed mid-operation.
Q168: How do you implement poison message handling in Lambda SQS consumers?β
Technical Answer:
A poison message repeatedly fails and blocks the queue. Configure maxReceiveCount on the SQS redrive policy to move it to DLQ after N failures. In the handler, catch and explicitly identify poison messages, optionally deleting them or moving to an error topic.
Code/Syntax Justification:
import json
import traceback
MAX_PROCESSING_ATTEMPTS = 3
def handler(event, context):
batch_failures = []
for record in event["Records"]:
receive_count = int(record["attributes"]["ApproximateReceiveCount"])
message_id = record["messageId"]
try:
body = json.loads(record["body"])
except json.JSONDecodeError:
# Poison: can never parse β skip to DLQ
print(f"Poison message (invalid JSON): {message_id}")
continue # Don't add to failures β let it be deleted
if receive_count > MAX_PROCESSING_ATTEMPTS:
print(f"Poison message (exceeded retries): {message_id}, count={receive_count}")
log_poison_message(message_id, body, "exceeded_retries")
continue # Let SQS move to DLQ
try:
process_message(body)
except ValidationError as e:
# Permanent error β will never succeed, skip to DLQ
print(f"Validation error for {message_id}: {e}")
log_poison_message(message_id, body, str(e))
continue
except Exception as e:
# Transient error β retry
print(f"Transient error for {message_id}: {e}")
batch_failures.append({"itemIdentifier": message_id})
return {"batchItemFailures": batch_failures}
π Behind the Question: Queue health and processing reliability. β The Red Flags: No distinction between permanent and transient errors in message processing.
Q169: How do you implement saga pattern rollbacks in Lambda?β
Technical Answer: The saga pattern breaks a distributed transaction into local transactions with compensating actions. If step N fails, execute compensating actions for steps N-1 through 1 in reverse. Step Functions orchestrates the saga with Catch states triggering rollback branches.
Code/Syntax Justification:
# Compensating action handlers
def cancel_reservation(event, context):
"""Compensating action: cancel hotel reservation."""
reservation_id = event["reservation_id"]
hotel_client.cancel(reservation_id)
return {"status": "cancelled", "reservation_id": reservation_id}
def refund_payment(event, context):
"""Compensating action: refund payment."""
payment_id = event["payment_id"]
payment_client.refund(payment_id)
return {"status": "refunded", "payment_id": payment_id}
def cancel_flight(event, context):
"""Compensating action: cancel flight booking."""
booking_id = event["booking_id"]
flight_client.cancel(booking_id)
return {"status": "cancelled", "booking_id": booking_id}
{
"Comment": "Travel booking saga",
"StartAt": "BookFlight",
"States": {
"BookFlight": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:book-flight",
"ResultPath": "$.flight",
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "BookingFailed" }],
"Next": "BookHotel"
},
"BookHotel": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:book-hotel",
"ResultPath": "$.hotel",
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "CancelFlight" }],
"Next": "ChargePayment"
},
"ChargePayment": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:charge-payment",
"Catch": [
{ "ErrorEquals": ["States.ALL"], "Next": "CancelHotelAndFlight" }
],
"End": true
},
"CancelHotelAndFlight": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "CancelHotel",
"States": {
"CancelHotel": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:cancel-hotel",
"End": true
}
}
},
{
"StartAt": "CancelFlightRollback",
"States": {
"CancelFlightRollback": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:cancel-flight",
"End": true
}
}
}
],
"Next": "BookingFailed"
},
"CancelFlight": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:cancel-flight",
"Next": "BookingFailed"
},
"BookingFailed": {
"Type": "Fail",
"Error": "BookingSagaFailed",
"Cause": "One or more booking steps failed"
}
}
}
π Behind the Question: Distributed transaction management in serverless. β The Red Flags: No compensating actions for multi-service operations.
Q170: How do you implement event replay and audit logging in Lambda?β
Technical Answer: Store every incoming event in a durable event store (S3, Kinesis Firehose, or DynamoDB) before processing. This enables replay for debugging, reprocessing after bug fixes, and audit compliance. Include metadata like timestamp, function version, and request ID.
Code/Syntax Justification:
import boto3
import json
import os
from datetime import datetime
s3 = boto3.client("s3")
AUDIT_BUCKET = os.environ["AUDIT_BUCKET"]
def audit_log(event, context):
"""Store event in S3 for audit and replay capability."""
timestamp = datetime.utcnow()
key = (
f"events/{timestamp.strftime('%Y/%m/%d/%H')}/"
f"{context.function_name}/{context.aws_request_id}.json"
)
s3.put_object(
Bucket=AUDIT_BUCKET,
Key=key,
Body=json.dumps({
"event": event,
"metadata": {
"function_name": context.function_name,
"function_version": context.function_version,
"request_id": context.aws_request_id,
"timestamp": timestamp.isoformat(),
"log_group": context.log_group_name,
"log_stream": context.log_stream_name,
},
}),
ContentType="application/json",
)
def handler(event, context):
# Audit first, then process
audit_log(event, context)
return process_event(event)
# Replay function
def replay_events(date: str, function_name: str):
"""Replay all events from a given date."""
prefix = f"events/{date}/{function_name}/"
paginator = s3.get_paginator("list_objects_v2")
lambda_client = boto3.client("lambda")
for page in paginator.paginate(Bucket=AUDIT_BUCKET, Prefix=prefix):
for obj in page.get("Contents", []):
event_data = json.loads(
s3.get_object(Bucket=AUDIT_BUCKET, Key=obj["Key"])["Body"].read()
)
lambda_client.invoke(
FunctionName=function_name,
InvocationType="Event",
Payload=json.dumps(event_data["event"]),
)
π Behind the Question: Event sourcing and auditability in serverless systems. β The Red Flags: No ability to replay or audit past events.
Q171: How do you handle Lambda cold start optimization for Java/Python runtimes?β
Technical Answer:
Python cold starts: minimize package size, use Lambda layers for dependencies, lazy-import heavy modules, use Provisioned Concurrency for latency-critical paths. Pre-initialize SDK clients outside the handler. Use __warmup__ patterns for scheduled warming.
Code/Syntax Justification:
import os
import json
# GOOD: Initialize outside handler (reused across warm invocations)
import boto3
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(os.environ["TABLE_NAME"])
# Lazy imports for heavy libraries
_numpy = None
def get_numpy():
global _numpy
if _numpy is None:
import numpy as np
_numpy = np
return _numpy
def handler(event, context):
# Handle warmup ping
if event.get("source") == "serverless-plugin-warmup":
return {"statusCode": 200, "body": "Warm"}
# Use lazy-loaded heavy library only when needed
if event.get("compute_stats"):
np = get_numpy()
data = np.array(event["values"])
return {"mean": float(data.mean()), "std": float(data.std())}
# Normal path β uses pre-initialized DynamoDB
item = table.get_item(Key={"pk": event["id"]}).get("Item")
return {"statusCode": 200, "body": json.dumps(item, default=str)}
π Behind the Question: Latency optimization for serverless functions. β The Red Flags: Importing all dependencies inside the handler or re-creating SDK clients per invocation.
Q172: How do you implement request validation in Lambda API handlers?β
Technical Answer: Validate at multiple layers: API Gateway request validators for schema enforcement, Pydantic or marshmallow in Lambda for business rule validation, and DynamoDB condition expressions for data-level validation. Return structured error responses with specific field-level messages.
Code/Syntax Justification:
from pydantic import BaseModel, Field, validator
from typing import Optional
import json
class OrderRequest(BaseModel):
product_id: str = Field(..., min_length=1, max_length=50)
quantity: int = Field(..., gt=0, le=1000)
shipping_address: str = Field(..., min_length=10)
email: str = Field(..., pattern=r"^[\w.+-]+@[\w-]+\.[\w.]+$")
discount_code: Optional[str] = None
@validator("quantity")
def validate_quantity(cls, v, values):
if v > 100 and not values.get("discount_code"):
raise ValueError("Bulk orders require a discount code")
return v
def handler(event, context):
try:
body = json.loads(event.get("body", "{}"))
order = OrderRequest(**body)
except json.JSONDecodeError:
return {"statusCode": 400, "body": json.dumps({"error": "Invalid JSON"})}
except Exception as e:
errors = []
if hasattr(e, "errors"):
errors = [{"field": err["loc"][-1], "message": err["msg"]} for err in e.errors()]
return {"statusCode": 422, "body": json.dumps({"errors": errors})}
result = process_order(order)
return {"statusCode": 201, "body": json.dumps(result)}
π Behind the Question: Input validation defense in depth. β The Red Flags: No input validation or only client-side validation.
Q173: How do you implement feature flags in Lambda functions?β
Technical Answer: Use AWS AppConfig (native Lambda extension for dynamic config), SSM Parameter Store (simple key-value), or third-party services (LaunchDarkly, Unleash). AppConfig provides gradual rollout, validation, and rollback. Cache flags in memory with TTL to avoid per-invocation API calls.
Code/Syntax Justification:
import boto3
import json
import time
import os
ssm = boto3.client("ssm")
# In-memory cache with TTL
_flag_cache = {}
_cache_ttl = 300 # 5 minutes
def get_feature_flag(flag_name: str, default: bool = False) -> bool:
now = time.time()
if flag_name in _flag_cache and _flag_cache[flag_name]["expires"] > now:
return _flag_cache[flag_name]["value"]
try:
param = ssm.get_parameter(Name=f"/features/{flag_name}")
value = param["Parameter"]["Value"].lower() == "true"
except ssm.exceptions.ParameterNotFound:
value = default
_flag_cache[flag_name] = {"value": value, "expires": now + _cache_ttl}
return value
def handler(event, context):
if get_feature_flag("new_pricing_engine"):
price = new_pricing_engine(event)
else:
price = legacy_pricing(event)
if get_feature_flag("enable_recommendations"):
recommendations = get_recommendations(event["user_id"])
else:
recommendations = []
return {"price": price, "recommendations": recommendations}
π Behind the Question: Runtime behavior control without redeployment. β The Red Flags: Hardcoding feature toggles or redeploying to enable/disable features.
Q174: How do you implement request/response correlation across distributed Lambda functions?β
Technical Answer: Propagate a correlation ID (trace ID) through all service calls, logs, and events. Use X-Ray trace ID, generate a UUID at the entry point, or extract from the incoming request. Include the correlation ID in all log messages and pass it via headers, message attributes, or event fields.
Code/Syntax Justification:
import uuid
import json
import logging
import os
logger = logging.getLogger()
class CorrelationContext:
_correlation_id = None
@classmethod
def get_or_create(cls, event: dict) -> str:
if cls._correlation_id:
return cls._correlation_id
# Try to extract from various sources
cls._correlation_id = (
event.get("headers", {}).get("x-correlation-id")
or event.get("correlation_id")
or event.get("requestContext", {}).get("requestId")
or str(uuid.uuid4())
)
return cls._correlation_id
def handler(event, context):
correlation_id = CorrelationContext.get_or_create(event)
# Structured logging with correlation
logger.info(json.dumps({
"correlation_id": correlation_id,
"request_id": context.aws_request_id,
"action": "process_order",
"user_id": event.get("user_id"),
}))
# Pass correlation to downstream services
sqs = boto3.client("sqs")
sqs.send_message(
QueueUrl=os.environ["QUEUE_URL"],
MessageBody=json.dumps(event),
MessageAttributes={
"correlation_id": {"StringValue": correlation_id, "DataType": "String"},
},
)
return {
"statusCode": 200,
"headers": {"x-correlation-id": correlation_id},
"body": json.dumps({"result": "processed"}),
}
π Behind the Question: Distributed tracing and debugging across serverless functions. β The Red Flags: No correlation ID propagation β impossible to trace requests across services.
Q175: How do you implement event filtering for Lambda event source mappings?β
Technical Answer: Event source mapping filters (available for SQS, SNS, Kinesis, DynamoDB Streams, and MSK) let Lambda process only matching events at the service level, reducing invocations and cost. Filter patterns use JSON-based matching with numeric, prefix, suffix, and exists operators.
Code/Syntax Justification:
Resources:
OrderProcessor:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Events:
OrderEvents:
Type: SQS
Properties:
Queue: !GetAtt OrderQueue.Arn
BatchSize: 10
FilterCriteria:
Filters:
- Pattern: '{"body": {"order_type": ["premium"], "amount": [{"numeric": [">", 100]}]}}'
HighValueDynamoProcessor:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Events:
DynamoStream:
Type: DynamoDB
Properties:
Stream: !GetAtt OrderTable.StreamArn
FilterCriteria:
Filters:
- Pattern: '{"eventName": ["INSERT", "MODIFY"], "dynamodb": {"NewImage": {"status": {"S": ["shipped"]}}}}'
# The handler only receives matching events β no filtering code needed
def handler(event, context):
for record in event["Records"]:
# All records here match the filter β guaranteed premium orders > $100
order = json.loads(record["body"])
process_premium_order(order)
π Behind the Question: Cost optimization by filtering at the source. β The Red Flags: Processing all events in Lambda and filtering in code when event source filters are available.
Q176: How do you implement Lambda Powertools for observability?β
Technical Answer: AWS Lambda Powertools for Python provides structured logging, distributed tracing, custom metrics, and idempotency as decorators. It standardizes observability without boilerplate, integrates with CloudWatch and X-Ray, and follows Well-Architected serverless best practices.
Code/Syntax Justification:
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.utilities.idempotency import (
DynamoDBPersistenceLayer, idempotent,
)
logger = Logger(service="order-service")
tracer = Tracer(service="order-service")
metrics = Metrics(service="order-service", namespace="OrderService")
app = APIGatewayRestResolver()
persistence = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
@app.post("/orders")
@tracer.capture_method
def create_order():
body = app.current_event.json_body
logger.info("Creating order", extra={"user_id": body["user_id"]})
metrics.add_metric(name="OrderCreated", unit="Count", value=1)
metrics.add_metadata(key="order_type", value=body.get("type"))
return {"order_id": "123", "status": "created"}
@logger.inject_lambda_context(log_event=True)
@tracer.capture_lambda_handler
@metrics.log_metrics(capture_cold_start_metric=True)
@idempotent(persistence_store=persistence)
def handler(event: dict, context: LambdaContext):
return app.resolve(event, context)
π Behind the Question: Production-grade observability with minimal boilerplate. β The Red Flags: Building custom logging/tracing/metrics infrastructure when Powertools exists.
Q177: How do you implement Lambda response streaming?β
Technical Answer:
Response streaming (Node.js runtime, with Python support via custom runtime) sends data incrementally instead of buffering the entire response. Useful for large payloads, server-sent events, and progressive rendering. Requires Function URL with RESPONSE_STREAM invoke mode.
Code/Syntax Justification:
# Python response streaming via Lambda Web Adapter
# Uses FastAPI with streaming response behind Lambda Web Adapter
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import asyncio
app = FastAPI()
@app.get("/stream")
async def stream_response():
async def generate():
for i in range(100):
yield f"data: chunk {i}\n\n"
await asyncio.sleep(0.1)
return StreamingResponse(generate(), media_type="text/event-stream")
# SAM template configuration
# FunctionUrlConfig:
# AuthType: NONE
# InvokeMode: RESPONSE_STREAM
π Behind the Question: Progressive response delivery for improved user experience. β The Red Flags: Buffering large responses in memory when streaming is available.
Q178: How do you implement Lambda function versioning and alias strategies?β
Technical Answer:
Publish immutable versions with PublishVersion. Create aliases (dev, staging, prod) pointing to specific versions. Use weighted aliases for canary deployments (10% to new version). CodeDeploy automates traffic shifting with pre/post hooks and automatic rollback.
Code/Syntax Justification:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
AutoPublishAlias: live
DeploymentPreference:
Type: Canary10Percent5Minutes # 10% traffic for 5 min, then 100%
Alarms:
- !Ref ErrorAlarm
- !Ref LatencyAlarm
Hooks:
PreTraffic: !Ref PreTrafficHook
PostTraffic: !Ref PostTrafficHook
PreTrafficHook:
Type: AWS::Serverless::Function
Properties:
Handler: hooks.pre_traffic
Runtime: python3.12
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action: codedeploy:PutLifecycleEventHookExecutionStatus
Resource: "*"
# Pre-traffic hook: validate new version before receiving traffic
import boto3
def pre_traffic(event, context):
deployment_id = event["DeploymentId"]
hook_id = event["LifecycleEventHookExecutionId"]
codedeploy = boto3.client("codedeploy")
try:
# Run smoke tests against new version
lambda_client = boto3.client("lambda")
response = lambda_client.invoke(
FunctionName=f"my-function:live",
Payload=json.dumps({"test": True}),
)
assert response["StatusCode"] == 200
status = "Succeeded"
except Exception:
status = "Failed"
codedeploy.put_lifecycle_event_hook_execution_status(
deploymentId=deployment_id,
lifecycleEventHookExecutionId=hook_id,
status=status,
)
π Behind the Question: Safe deployment strategies for serverless. β The Red Flags: Deploying directly to $LATEST without versioning or canary rollout.
Q179: How do you implement cost-effective scheduled Lambda functions?β
Technical Answer: Use EventBridge Scheduler (not CloudWatch Events) for cron/rate triggers. Combine multiple scheduled tasks into one Lambda with event routing to reduce cold starts. Use Provisioned Concurrency only for sub-second latency requirements. Monitor actual vs expected invocation counts.
Code/Syntax Justification:
Resources:
ScheduledFunction:
Type: AWS::Serverless::Function
Properties:
Handler: scheduler.handler
Runtime: python3.12
Timeout: 900
Events:
HourlyCleanup:
Type: ScheduleV2
Properties:
ScheduleExpression: "rate(1 hour)"
Input: '{"task": "cleanup"}'
DailyReport:
Type: ScheduleV2
Properties:
ScheduleExpression: "cron(0 8 * * ? *)"
Input: '{"task": "daily_report"}'
WeeklyDigest:
Type: ScheduleV2
Properties:
ScheduleExpression: "cron(0 9 ? * MON *)"
Input: '{"task": "weekly_digest"}'
# Multi-task scheduler handler
TASK_HANDLERS = {
"cleanup": cleanup_expired_records,
"daily_report": generate_daily_report,
"weekly_digest": send_weekly_digest,
}
def handler(event, context):
task = event.get("task", "unknown")
handler_fn = TASK_HANDLERS.get(task)
if not handler_fn:
raise ValueError(f"Unknown task: {task}")
return handler_fn(event, context)
π Behind the Question: Efficient scheduled job architecture. β The Red Flags: Separate Lambda functions for every scheduled task, each incurring cold starts.
Section 38: Lambda Data Processing Patternsβ
Q180: How do you implement fan-out/fan-in pattern with Lambda?β
Technical Answer:
Fan-out distributes work to parallel Lambda invocations. Fan-in collects results. Use Step Functions Map state for orchestrated fan-out/fan-in, SNS for fire-and-forget fan-out, or SQS for durable fan-out with DynamoDB for result aggregation.
Code/Syntax Justification:
{
"StartAt": "PrepareItems",
"States": {
"PrepareItems": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:prepare",
"Next": "ProcessInParallel"
},
"ProcessInParallel": {
"Type": "Map",
"ItemsPath": "$.items",
"MaxConcurrency": 50,
"Iterator": {
"StartAt": "ProcessItem",
"States": {
"ProcessItem": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:process-item",
"End": true
}
}
},
"Next": "AggregateResults"
},
"AggregateResults": {
"Type": "Task",
"Resource": "arn:aws:lambda:...:aggregate",
"End": true
}
}
}
# Aggregate handler collects Map state results
def aggregate_handler(event, context):
# event is an array of results from parallel invocations
results = event # Each element is a ProcessItem result
total = sum(r["amount"] for r in results)
success_count = sum(1 for r in results if r["status"] == "success")
return {
"total_items": len(results),
"successful": success_count,
"total_amount": total,
}
π Behind the Question: Parallel processing patterns for serverless. β The Red Flags: Sequential processing of independent items instead of parallel fan-out.
Q181: How do you implement ETL pipelines with Lambda and S3?β
Technical Answer: Trigger Lambda on S3 PUT events. Process the file (CSV/JSON/Parquet), transform data, and write results to another S3 bucket or database. For large files, use S3 Select for server-side filtering or stream the file with range requests.
Code/Syntax Justification:
import boto3
import json
import csv
import io
s3 = boto3.client("s3")
def handler(event, context):
bucket = event["Records"][0]["s3"]["bucket"]["name"]
key = event["Records"][0]["s3"]["object"]["key"]
# For large files: use S3 Select to filter server-side
if key.endswith(".csv"):
result = s3.select_object_content(
Bucket=bucket, Key=key,
Expression="SELECT * FROM s3object WHERE CAST(amount AS DECIMAL) > 100",
ExpressionType="SQL",
InputSerialization={"CSV": {"FileHeaderInfo": "USE"}},
OutputSerialization={"JSON": {}},
)
records = []
for event_stream in result["Payload"]:
if "Records" in event_stream:
records.extend(
json.loads(line)
for line in event_stream["Records"]["Payload"].decode().strip().split("\n")
if line
)
else:
# Read full file for JSON/Parquet
obj = s3.get_object(Bucket=bucket, Key=key)
records = json.loads(obj["Body"].read())
# Transform
transformed = [transform_record(r) for r in records]
# Write results
output_key = f"processed/{key.rsplit('/', 1)[-1]}"
s3.put_object(
Bucket=os.environ["OUTPUT_BUCKET"],
Key=output_key,
Body=json.dumps(transformed),
ContentType="application/json",
)
return {"processed": len(transformed), "output_key": output_key}
π Behind the Question: Event-driven ETL with serverless. β The Red Flags: Reading entire large files into Lambda memory without streaming or S3 Select.
Q182: How do you implement real-time data aggregation with Kinesis and Lambda?β
Technical Answer:
Use Kinesis Data Streams as the event source with configurable batch size, parallelization factor, and tumbling windows. Lambda receives batches of records to aggregate. Use tumbling_window_in_seconds for time-based aggregation without external state.
Code/Syntax Justification:
Resources:
AggregatorFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Events:
KinesisEvent:
Type: Kinesis
Properties:
Stream: !GetAtt DataStream.Arn
BatchSize: 100
StartingPosition: LATEST
ParallelizationFactor: 10
TumblingWindowInSeconds: 60
FunctionResponseTypes:
- ReportBatchItemFailures
def handler(event, context):
# Tumbling window state from previous invocation
state = event.get("state", {})
counts = state.get("counts", {})
totals = state.get("totals", {})
for record in event["Records"]:
data = json.loads(
base64.b64decode(record["kinesis"]["data"]).decode()
)
category = data["category"]
amount = data["amount"]
counts[category] = counts.get(category, 0) + 1
totals[category] = totals.get(category, 0) + amount
# Check if window is closing
if event.get("isFinalInvokeForWindow"):
# Flush aggregated results
for category in counts:
store_aggregation(category, counts[category], totals[category])
return {"state": {}} # Reset state
# Return state for next invocation in window
return {"state": {"counts": counts, "totals": totals}}
π Behind the Question: Stateful stream processing with serverless. β The Red Flags: Not using tumbling windows for time-based aggregation, or re-implementing in Lambda code.
Q183: How do you implement data lake ingestion with Lambda?β
Technical Answer: Use Lambda to receive events from API Gateway, SQS, or Kinesis, validate/transform the data, and write to S3 in partitioned Parquet format. Use Kinesis Firehose for high-throughput ingestion with automatic batching, compression, and partitioning.
Code/Syntax Justification:
import boto3
import pyarrow as pa
import pyarrow.parquet as pq
import io
from datetime import datetime
s3 = boto3.client("s3")
def handler(event, context):
records = [json.loads(r["body"]) for r in event["Records"]]
if not records:
return {"processed": 0}
# Convert to PyArrow table
table = pa.Table.from_pylist(records)
# Write Parquet to partitioned path
now = datetime.utcnow()
partition = f"year={now.year}/month={now.month:02d}/day={now.day:02d}/hour={now.hour:02d}"
key = f"raw/{partition}/{context.aws_request_id}.parquet"
buffer = io.BytesIO()
pq.write_table(table, buffer, compression="zstd")
buffer.seek(0)
s3.put_object(
Bucket=os.environ["DATA_LAKE_BUCKET"],
Key=key,
Body=buffer.getvalue(),
ContentType="application/octet-stream",
)
return {"processed": len(records), "partition": partition}
π Behind the Question: Data lake architecture with serverless ingestion. β The Red Flags: Writing individual JSON files per event instead of batched Parquet.
Q184: How do you implement Lambda-based data validation pipelines?β
Technical Answer: Validate incoming data against schemas, business rules, and referential integrity. Route valid records downstream and invalid records to error handling. Use Pydantic for schema validation, custom rules for business logic, and lookup tables for referential checks.
Code/Syntax Justification:
from pydantic import BaseModel, validator
from typing import Optional
from enum import Enum
class OrderStatus(str, Enum):
PENDING = "pending"
CONFIRMED = "confirmed"
SHIPPED = "shipped"
class OrderRecord(BaseModel):
order_id: str
customer_id: str
amount: float
status: OrderStatus
email: str
@validator("amount")
def amount_must_be_positive(cls, v):
if v <= 0:
raise ValueError("Amount must be positive")
if v > 1_000_000:
raise ValueError("Amount exceeds maximum")
return round(v, 2)
def handler(event, context):
valid_records = []
invalid_records = []
for record in event["Records"]:
raw = json.loads(record["body"])
try:
validated = OrderRecord(**raw)
valid_records.append(validated.dict())
except Exception as e:
invalid_records.append({
"raw": raw,
"errors": str(e),
"message_id": record["messageId"],
})
# Route valid records downstream
if valid_records:
send_to_downstream(valid_records)
# Route invalid records to error handling
if invalid_records:
send_to_error_queue(invalid_records)
return {"valid": len(valid_records), "invalid": len(invalid_records)}
π Behind the Question: Data quality enforcement at ingestion. β The Red Flags: No validation layer between raw input and downstream processing.
Q185: How do you implement Lambda-based file processing with size limits?β
Technical Answer: Lambda has 512MB /tmp storage (configurable to 10GB) and 15-minute timeout. For large files, use streaming with S3 range reads, split processing across multiple invocations via Step Functions, or use EFS for larger storage. Monitor /tmp usage and clean up.
Code/Syntax Justification:
import boto3
import os
import tempfile
s3 = boto3.client("s3")
def handler(event, context):
bucket = event["bucket"]
key = event["key"]
# Check file size before downloading
head = s3.head_object(Bucket=bucket, Key=key)
file_size = head["ContentLength"]
max_size = 500 * 1024 * 1024 # 500MB
if file_size > max_size:
# Split into chunks for parallel processing
chunk_size = 100 * 1024 * 1024
chunks = []
for start in range(0, file_size, chunk_size):
end = min(start + chunk_size - 1, file_size - 1)
chunks.append({"bucket": bucket, "key": key, "range": f"bytes={start}-{end}"})
return {"action": "split", "chunks": chunks}
# Small enough: download and process
tmp_path = f"/tmp/{os.path.basename(key)}"
try:
s3.download_file(bucket, key, tmp_path)
result = process_file(tmp_path)
return {"action": "processed", "result": result}
finally:
if os.path.exists(tmp_path):
os.remove(tmp_path) # Clean up /tmp
def process_file(path: str) -> dict:
"""Process file and return results."""
size = os.path.getsize(path)
# Check /tmp space
tmp_stat = os.statvfs("/tmp")
available = tmp_stat.f_bavail * tmp_stat.f_frsize
print(f"File: {size/1e6:.1f}MB, /tmp available: {available/1e6:.1f}MB")
# ... processing logic
return {"size": size}
π Behind the Question: Resource management for file processing in Lambda. β The Red Flags: Not checking file size before download or not cleaning up /tmp.
Q186: How do you implement Lambda-based API rate limiting?β
Technical Answer: Use API Gateway's built-in throttling for basic rate limiting. For custom limits (per-user, per-tenant), use DynamoDB or ElastiCache as a token bucket or sliding window counter. Lambda checks the counter before processing requests.
Code/Syntax Justification:
import boto3
import time
import json
dynamodb = boto3.resource("dynamodb")
rate_table = dynamodb.Table("rate_limits")
def check_rate_limit(user_id: str, limit: int = 100, window_seconds: int = 60) -> bool:
now = int(time.time())
window_key = f"{user_id}:{now // window_seconds}"
try:
response = rate_table.update_item(
Key={"pk": window_key},
UpdateExpression="SET #c = if_not_exists(#c, :zero) + :one, #t = :ttl",
ExpressionAttributeNames={"#c": "count", "#t": "ttl"},
ExpressionAttributeValues={
":one": 1,
":zero": 0,
":ttl": now + window_seconds + 60, # TTL for cleanup
":limit": limit,
},
ConditionExpression="attribute_not_exists(#c) OR #c < :limit",
ReturnValues="UPDATED_NEW",
)
return True # Within limit
except dynamodb.meta.client.exceptions.ConditionalCheckFailedException:
return False # Rate limited
def handler(event, context):
user_id = event["requestContext"]["authorizer"]["principalId"]
if not check_rate_limit(user_id, limit=100, window_seconds=60):
return {
"statusCode": 429,
"headers": {"Retry-After": "60"},
"body": json.dumps({"error": "Rate limit exceeded"}),
}
return process_request(event)
π Behind the Question: Custom rate limiting for multi-tenant serverless APIs. β The Red Flags: No rate limiting on public-facing Lambda APIs.
Q187: How do you implement Lambda-based image processing pipelines?β
Technical Answer: Trigger Lambda on S3 upload, use Pillow or sharp for image transformation (resize, crop, watermark, format conversion), and write results to a processed bucket. Use Lambda layers for image libraries. For heavy processing, use container image Lambdas with 10GB image support.
Code/Syntax Justification:
from PIL import Image
import boto3
import io
import os
s3 = boto3.client("s3")
SIZES = {
"thumbnail": (150, 150),
"medium": (600, 600),
"large": (1200, 1200),
}
def handler(event, context):
bucket = event["Records"][0]["s3"]["bucket"]["name"]
key = event["Records"][0]["s3"]["object"]["key"]
# Skip processed images to prevent infinite loops
if key.startswith("processed/"):
return {"skipped": True}
# Download original
response = s3.get_object(Bucket=bucket, Key=key)
image = Image.open(io.BytesIO(response["Body"].read()))
# Auto-orient based on EXIF
from PIL import ImageOps
image = ImageOps.exif_transpose(image)
results = {}
for size_name, dimensions in SIZES.items():
resized = image.copy()
resized.thumbnail(dimensions, Image.Resampling.LANCZOS)
buffer = io.BytesIO()
resized.save(buffer, format="WEBP", quality=85)
buffer.seek(0)
output_key = f"processed/{size_name}/{key.rsplit('.', 1)[0]}.webp"
s3.put_object(
Bucket=os.environ["OUTPUT_BUCKET"],
Key=output_key,
Body=buffer.getvalue(),
ContentType="image/webp",
)
results[size_name] = output_key
return {"processed": results}
π Behind the Question: Media processing with serverless compute. β The Red Flags: Not preventing infinite trigger loops or processing images synchronously in API handlers.
Q188: How do you implement Lambda-based PDF generation?β
Technical Answer: Use libraries like ReportLab, WeasyPrint, or Puppeteer (via Lambda layer or container) for PDF generation. For HTML-to-PDF, use a headless Chrome layer. Store templates in S3, generate PDFs in /tmp, and upload results to S3.
Code/Syntax Justification:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
import boto3
import io
import json
s3 = boto3.client("s3")
def handler(event, context):
order = event["order"]
# Generate PDF in memory
buffer = io.BytesIO()
c = canvas.Canvas(buffer, pagesize=letter)
width, height = letter
# Header
c.setFont("Helvetica-Bold", 24)
c.drawString(1 * inch, height - 1 * inch, "Invoice")
# Order details
c.setFont("Helvetica", 12)
y = height - 2 * inch
c.drawString(1 * inch, y, f"Order ID: {order['id']}")
y -= 20
c.drawString(1 * inch, y, f"Customer: {order['customer_name']}")
y -= 20
c.drawString(1 * inch, y, f"Date: {order['date']}")
# Line items
y -= 40
for item in order["items"]:
c.drawString(1 * inch, y, f"{item['name']} x{item['qty']} - ${item['price']:.2f}")
y -= 20
# Total
y -= 20
c.setFont("Helvetica-Bold", 14)
total = sum(i["price"] * i["qty"] for i in order["items"])
c.drawString(1 * inch, y, f"Total: ${total:.2f}")
c.save()
buffer.seek(0)
# Upload to S3
key = f"invoices/{order['id']}.pdf"
s3.put_object(
Bucket=os.environ["DOCUMENTS_BUCKET"],
Key=key,
Body=buffer.getvalue(),
ContentType="application/pdf",
)
# Generate presigned URL for download
url = s3.generate_presigned_url(
"get_object",
Params={"Bucket": os.environ["DOCUMENTS_BUCKET"], "Key": key},
ExpiresIn=3600,
)
return {"pdf_key": key, "download_url": url}
π Behind the Question: Document generation workflows with serverless. β The Red Flags: Generating PDFs synchronously in API request path instead of async with S3 presigned URLs.
Q189: How do you implement DynamoDB Streams processing with Lambda?β
Technical Answer:
DynamoDB Streams capture item-level changes (INSERT, MODIFY, REMOVE) as ordered events. Lambda processes these for cross-region replication, materialized views, audit logs, and event-driven workflows. Configure StreamViewType to control what data is captured.
Code/Syntax Justification:
def handler(event, context):
for record in event["Records"]:
event_name = record["eventName"] # INSERT, MODIFY, REMOVE
if event_name == "INSERT":
new_item = deserialize(record["dynamodb"]["NewImage"])
handle_new_item(new_item)
elif event_name == "MODIFY":
old_item = deserialize(record["dynamodb"]["OldImage"])
new_item = deserialize(record["dynamodb"]["NewImage"])
changes = diff_items(old_item, new_item)
handle_update(new_item, changes)
elif event_name == "REMOVE":
old_item = deserialize(record["dynamodb"]["OldImage"])
handle_deletion(old_item)
def deserialize(image: dict) -> dict:
"""Convert DynamoDB type descriptors to Python types."""
from boto3.dynamodb.types import TypeDeserializer
deserializer = TypeDeserializer()
return {k: deserializer.deserialize(v) for k, v in image.items()}
def diff_items(old: dict, new: dict) -> dict:
"""Find changed fields between old and new images."""
changes = {}
all_keys = set(old.keys()) | set(new.keys())
for key in all_keys:
if old.get(key) != new.get(key):
changes[key] = {"old": old.get(key), "new": new.get(key)}
return changes
π Behind the Question: Change data capture for event-driven architectures.
β The Red Flags: Not using OldImage for change detection or not handling all event types.
Q190: How do you implement Lambda-based GraphQL resolvers?β
Technical Answer: Use AppSync with Lambda resolvers or implement a standalone GraphQL server with Ariadne/Strawberry behind API Gateway. Each resolver maps to a Lambda handler or a single Lambda handles all resolvers with field-level routing. Use batching to solve N+1 queries.
Code/Syntax Justification:
# AppSync direct Lambda resolver
def handler(event, context):
field = event["info"]["fieldName"]
args = event.get("arguments", {})
source = event.get("source") # Parent object for nested resolvers
resolvers = {
"getUser": resolve_get_user,
"listOrders": resolve_list_orders,
"createOrder": resolve_create_order,
}
resolver = resolvers.get(field)
if not resolver:
raise ValueError(f"Unknown field: {field}")
return resolver(args, source, event["info"])
def resolve_get_user(args, source, info):
user_id = args["userId"]
user = users_table.get_item(Key={"pk": user_id}).get("Item")
return user
def resolve_list_orders(args, source, info):
# Batching: if called from User parent, use source.pk
user_id = source["pk"] if source else args.get("userId")
response = orders_table.query(
KeyConditionExpression=Key("user_id").eq(user_id),
Limit=args.get("limit", 20),
)
return response["Items"]
π Behind the Question: GraphQL API patterns with serverless backends. β The Red Flags: Separate Lambda per GraphQL field (wasteful) or no batching for nested queries.
Q191: How do you implement Lambda-based webhook receivers?β
Technical Answer: Expose a Lambda function URL or API Gateway endpoint to receive webhook payloads. Verify signatures, respond quickly with 200, and process asynchronously via SQS. Store raw payloads for replay. Handle retries and idempotency.
Code/Syntax Justification:
import hashlib
import hmac
import json
import os
import boto3
sqs = boto3.client("sqs")
def handler(event, context):
# 1. Verify webhook signature
body = event.get("body", "")
signature = event.get("headers", {}).get("x-webhook-signature", "")
secret = os.environ["WEBHOOK_SECRET"]
expected = hmac.new(
secret.encode(), body.encode(), hashlib.sha256
).hexdigest()
if not hmac.compare_digest(signature, expected):
return {"statusCode": 401, "body": "Invalid signature"}
# 2. Respond immediately (webhooks have short timeouts)
# 3. Queue for async processing
sqs.send_message(
QueueUrl=os.environ["WEBHOOK_QUEUE_URL"],
MessageBody=body,
MessageAttributes={
"webhook_type": {
"StringValue": event.get("headers", {}).get("x-webhook-event", "unknown"),
"DataType": "String",
},
},
MessageDeduplicationId=hashlib.sha256(body.encode()).hexdigest(),
MessageGroupId="webhooks",
)
return {"statusCode": 200, "body": json.dumps({"received": True})}
π Behind the Question: Event-driven integration with external services. β The Red Flags: Processing webhook payload synchronously or not verifying signatures.
Q192: How do you implement Lambda-based email processing with SES?β
Technical Answer: SES receives emails and stores them in S3, triggering Lambda for processing. Lambda parses the MIME message, extracts attachments, processes content, and routes based on sender/subject/content. Use SES receipt rules for email-based workflows.
Code/Syntax Justification:
import boto3
import email
from email import policy
import json
import os
s3 = boto3.client("s3")
def handler(event, context):
# SES stores email in S3, triggers Lambda with metadata
ses_record = event["Records"][0]["ses"]
message_id = ses_record["mail"]["messageId"]
sender = ses_record["mail"]["source"]
subject = ses_record["mail"]["commonHeaders"]["subject"]
# Fetch full email from S3
bucket = os.environ["EMAIL_BUCKET"]
key = f"incoming/{message_id}"
response = s3.get_object(Bucket=bucket, Key=key)
raw_email = response["Body"].read()
# Parse MIME message
msg = email.message_from_bytes(raw_email, policy=policy.default)
# Extract text body
body = ""
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_content()
break
else:
body = msg.get_content()
# Extract attachments
attachments = []
for part in msg.iter_attachments():
filename = part.get_filename()
content = part.get_content()
att_key = f"attachments/{message_id}/{filename}"
s3.put_object(Bucket=bucket, Key=att_key, Body=content)
attachments.append({"filename": filename, "s3_key": att_key})
# Route based on content
route_email(sender, subject, body, attachments)
return {"processed": message_id, "attachments": len(attachments)}
π Behind the Question: Email-driven automation with serverless. β The Red Flags: Not parsing MIME properly or storing raw emails without structured extraction.
Q193: How do you implement Lambda-based cron job monitoring?β
Technical Answer:
Monitor scheduled Lambda executions by tracking expected vs actual invocation times. Use CloudWatch alarms on Invocations metric with missing data treatment. Implement a heartbeat pattern where each execution records its timestamp, and a separate monitor checks for missed beats.
Code/Syntax Justification:
import boto3
import json
import time
dynamodb = boto3.resource("dynamodb")
heartbeat_table = dynamodb.Table("cron_heartbeats")
sns = boto3.client("sns")
def record_heartbeat(job_name: str):
"""Record successful execution."""
heartbeat_table.put_item(Item={
"job_name": job_name,
"last_run": int(time.time()),
"status": "success",
"ttl": int(time.time()) + 86400 * 7,
})
def monitor_handler(event, context):
"""Check all cron jobs ran within expected window."""
now = int(time.time())
jobs = {
"hourly_cleanup": 3600 + 300, # Every hour + 5 min grace
"daily_report": 86400 + 900, # Every day + 15 min grace
"weekly_digest": 604800 + 3600, # Every week + 1 hour grace
}
alerts = []
for job_name, max_interval in jobs.items():
item = heartbeat_table.get_item(Key={"job_name": job_name}).get("Item")
if not item:
alerts.append(f"{job_name}: never ran")
elif now - int(item["last_run"]) > max_interval:
elapsed = now - int(item["last_run"])
alerts.append(f"{job_name}: last ran {elapsed//3600}h ago (max: {max_interval//3600}h)")
if alerts:
sns.publish(
TopicArn=os.environ["ALERT_TOPIC"],
Subject="Cron Job Monitor Alert",
Message="\n".join(alerts),
)
return {"checked": len(jobs), "alerts": len(alerts)}
π Behind the Question: Operational monitoring for scheduled serverless jobs. β The Red Flags: No monitoring for missed cron job executions.
Q194: How do you implement Lambda-based data synchronization between services?β
Technical Answer: Use change events (DynamoDB Streams, database CDC) to trigger Lambda that transforms and pushes changes to downstream services. Implement eventual consistency with conflict resolution. Track sync state for incremental processing and handle ordering.
Code/Syntax Justification:
import boto3
import json
def sync_handler(event, context):
"""Sync DynamoDB changes to Elasticsearch."""
es_client = get_elasticsearch_client()
batch_failures = []
for record in event["Records"]:
try:
event_name = record["eventName"]
table_name = record["eventSourceARN"].split("/")[1]
if event_name in ("INSERT", "MODIFY"):
new_item = deserialize(record["dynamodb"]["NewImage"])
doc_id = new_item["pk"]
es_client.index(
index=f"{table_name}-index",
id=doc_id,
body=new_item,
)
elif event_name == "REMOVE":
old_item = deserialize(record["dynamodb"]["OldImage"])
doc_id = old_item["pk"]
es_client.delete(
index=f"{table_name}-index",
id=doc_id,
ignore=[404],
)
except Exception as e:
print(f"Sync failed for record: {e}")
batch_failures.append({
"itemIdentifier": record["eventID"]
})
return {"batchItemFailures": batch_failures}
π Behind the Question: Cross-service data consistency with event-driven sync. β The Red Flags: Polling for changes instead of using streams/events, or not handling sync failures.
Q195: How do you implement Lambda-based notification dispatching?β
Technical Answer: Use Lambda to route notifications to multiple channels (email via SES, SMS via SNS, push via Pinpoint, Slack via webhooks). Implement user preference-based routing, templating, rate limiting, and delivery tracking. Use SQS for reliable dispatch with DLQ for failures.
Code/Syntax Justification:
import boto3
import json
import os
from string import Template
ses = boto3.client("ses")
sns = boto3.client("sns")
TEMPLATES = {
"order_confirmation": Template("Hi $name, your order #$order_id has been confirmed. Total: $$total"),
"shipping_update": Template("Your order #$order_id has shipped. Tracking: $tracking"),
}
def handler(event, context):
for record in event["Records"]:
notification = json.loads(record["body"])
notification_type = notification["type"]
user = notification["user"]
data = notification["data"]
template = TEMPLATES.get(notification_type)
if not template:
print(f"Unknown notification type: {notification_type}")
continue
message = template.safe_substitute(**data)
# Route based on user preferences
channels = user.get("notification_channels", ["email"])
if "email" in channels and user.get("email"):
ses.send_email(
Source=os.environ["FROM_EMAIL"],
Destination={"ToAddresses": [user["email"]]},
Message={
"Subject": {"Data": f"Order Update: {notification_type.replace('_', ' ').title()}"},
"Body": {"Text": {"Data": message}},
},
)
if "sms" in channels and user.get("phone"):
sns.publish(PhoneNumber=user["phone"], Message=message[:160])
if "slack" in channels and user.get("slack_webhook"):
import requests
requests.post(user["slack_webhook"], json={"text": message})
return {"dispatched": len(event["Records"])}
π Behind the Question: Multi-channel notification architecture. β The Red Flags: Sending notifications synchronously in request handlers or hardcoding channels.
Q196: How do you implement Lambda-based search indexing?β
Technical Answer: Trigger Lambda from database change events to update search indexes (OpenSearch/Elasticsearch, Algolia, Typesense). Batch updates for efficiency, handle partial failures, and implement full re-index capability. Use SQS between the trigger and indexer for buffering.
Code/Syntax Justification:
from opensearchpy import OpenSearch, helpers
import json
def handler(event, context):
os_client = OpenSearch(
hosts=[os.environ["OPENSEARCH_ENDPOINT"]],
http_auth=(os.environ["OS_USER"], os.environ["OS_PASS"]),
use_ssl=True,
)
actions = []
for record in event["Records"]:
body = json.loads(record["body"])
event_type = body["event_type"]
doc = body["document"]
if event_type in ("create", "update"):
actions.append({
"_op_type": "index",
"_index": "products",
"_id": doc["id"],
"_source": {
"title": doc["title"],
"description": doc["description"],
"price": doc["price"],
"category": doc["category"],
"updated_at": doc["updated_at"],
},
})
elif event_type == "delete":
actions.append({
"_op_type": "delete",
"_index": "products",
"_id": doc["id"],
})
if actions:
success, errors = helpers.bulk(os_client, actions, raise_on_error=False)
print(f"Indexed: {success}, Errors: {len(errors)}")
if errors:
print(f"Index errors: {json.dumps(errors[:5])}")
return {"indexed": len(actions)}
π Behind the Question: Real-time search index maintenance. β The Red Flags: Full re-index on every change or synchronous indexing in API handlers.
Q197: How do you implement Lambda-based data export and reporting?β
Technical Answer: For small reports (<6MB), return directly from Lambda. For larger reports, write to S3 and return a presigned URL. Use Step Functions for multi-step report generation. Schedule recurring reports with EventBridge. Use Parquet/CSV format based on consumer needs.
Code/Syntax Justification:
import boto3
import csv
import io
import json
s3 = boto3.client("s3")
def handler(event, context):
report_type = event.get("report_type", "daily_summary")
date_range = event.get("date_range", {})
# Query data
data = query_report_data(report_type, date_range)
# Generate CSV
buffer = io.StringIO()
writer = csv.DictWriter(buffer, fieldnames=data[0].keys() if data else [])
writer.writeheader()
writer.writerows(data)
# Upload to S3
key = f"reports/{report_type}/{context.aws_request_id}.csv"
s3.put_object(
Bucket=os.environ["REPORTS_BUCKET"],
Key=key,
Body=buffer.getvalue().encode(),
ContentType="text/csv",
)
# Generate presigned download URL (1 hour expiry)
url = s3.generate_presigned_url(
"get_object",
Params={"Bucket": os.environ["REPORTS_BUCKET"], "Key": key},
ExpiresIn=3600,
)
# Notify requester
if event.get("notify_email"):
send_email(event["notify_email"], f"Your {report_type} report is ready", url)
return {"report_key": key, "download_url": url, "row_count": len(data)}
π Behind the Question: Async report generation with serverless. β The Red Flags: Generating large reports synchronously in API request handlers.
Q198: How do you implement Lambda-based log aggregation and analysis?β
Technical Answer: Subscribe Lambda to CloudWatch Logs via subscription filters. Lambda receives compressed log batches, parses/transforms them, and forwards to analytics platforms (OpenSearch, S3, Datadog). Use log insights queries for ad-hoc analysis without Lambda.
Code/Syntax Justification:
import base64
import gzip
import json
def handler(event, context):
# CloudWatch Logs subscription delivers compressed, base64-encoded data
compressed = base64.b64decode(event["awslogs"]["data"])
log_data = json.loads(gzip.decompress(compressed))
log_group = log_data["logGroup"]
log_stream = log_data["logStream"]
parsed_events = []
for log_event in log_data["logEvents"]:
message = log_event["message"]
timestamp = log_event["timestamp"]
# Parse structured log (JSON format)
try:
parsed = json.loads(message)
except json.JSONDecodeError:
parsed = {"raw_message": message}
parsed["_log_group"] = log_group
parsed["_timestamp"] = timestamp
parsed["_log_stream"] = log_stream
# Filter for errors
if parsed.get("level") == "ERROR" or "ERROR" in message:
send_to_error_tracker(parsed)
parsed_events.append(parsed)
# Batch forward to analytics
forward_to_analytics(parsed_events)
return {"processed": len(parsed_events)}
π Behind the Question: Centralized log processing for observability. β The Red Flags: Not aggregating logs across Lambda functions for cross-service analysis.
Q199: How do you implement Lambda-based cache warming?β
Technical Answer: Schedule Lambda to pre-populate caches (ElastiCache, DynamoDB DAX, CloudFront) before traffic spikes. Query the most accessed data and write to cache. Use CloudWatch metrics to identify hot keys. Schedule warming before known peak hours.
Code/Syntax Justification:
import boto3
import json
import redis
import os
r = redis.Redis(
host=os.environ["REDIS_HOST"],
port=6379,
decode_responses=True,
)
def handler(event, context):
"""Pre-warm cache with frequently accessed data."""
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table("products")
# Warm top products
response = table.scan(
FilterExpression="view_count > :min_views",
ExpressionAttributeValues={":min_views": 100},
)
warmed = 0
pipeline = r.pipeline()
for item in response["Items"]:
cache_key = f"product:{item['product_id']}"
pipeline.setex(
cache_key,
3600, # 1 hour TTL
json.dumps(item, default=str),
)
warmed += 1
pipeline.execute()
# Warm category listings
categories = ["electronics", "clothing", "books", "home"]
for cat in categories:
items = table.query(
IndexName="category-index",
KeyConditionExpression=Key("category").eq(cat),
Limit=50,
)["Items"]
r.setex(f"category:{cat}", 1800, json.dumps(items, default=str))
return {"warmed_products": warmed, "warmed_categories": len(categories)}
π Behind the Question: Proactive cache management for predictable performance. β The Red Flags: Relying entirely on lazy cache population during peak traffic.
Section 39: Lambda Architecture Decision Guideβ
Q200: When should you use Lambda vs Fargate vs EC2?β
Technical Answer: Lambda: event-driven, sub-15min tasks, variable traffic, pay-per-use. Fargate: long-running containers, steady load, need more control (ports, sidecar). EC2: GPU workloads, persistent connections, maximum control, consistent high throughput. Cost crossover: Lambda is cheaper below ~1M requests/month; Fargate wins at steady high throughput.
Code/Syntax Justification:
Decision Matrix:
Lambda Fargate EC2
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Max duration 15 min Unlimited Unlimited
Max memory 10 GB 120 GB Unlimited
GPU support β β β
Persistent connections β β
β
Cold starts Yes Minimal No
Scaling speed Instant Minutes Minutes
Min cost (idle) $0 ~$10/mo ~$20/mo
Cost at 1M req/mo ~$2 ~$30 ~$60
Cost at 100M req/mo ~$200 ~$100 ~$80
Ops overhead None Low High
π Behind the Question: Architectural judgment for compute selection. β The Red Flags: Using Lambda for everything without evaluating alternatives.
Q201: How do you decide between synchronous and asynchronous Lambda invocation?β
Technical Answer: Synchronous: caller waits for response (API Gateway, direct invoke). Use for request/response APIs. Asynchronous: caller returns immediately (S3 events, SNS, EventBridge). Use for fire-and-forget processing. Async adds built-in retries (2) and DLQ support.
Code/Syntax Justification:
# Synchronous: API Gateway β Lambda β Response to client
def api_handler(event, context):
result = process(event) # Client is waiting
return {"statusCode": 200, "body": json.dumps(result)}
# Asynchronous: S3 Event β Lambda (client doesn't wait)
def s3_handler(event, context):
process_file(event) # No response expected
# If this fails, Lambda retries automatically (2 times)
# After retries, sends to DLQ
# Decision criteria:
# Use sync when: client needs immediate response, <30s processing
# Use async when: processing >30s, client doesn't need result,
# fire-and-forget, batch processing, event-driven
π Behind the Question: Invocation model selection for different use cases. β The Red Flags: Using synchronous invocation for long-running processes that don't need immediate response.
Q202: How do you decide between API Gateway REST vs HTTP APIs?β
Technical Answer: HTTP API: 70% cheaper, faster latency, supports JWT authorizer natively, CORS built-in. REST API: more features (request validation, caching, WAF, usage plans, API keys, request/response transformation). Use HTTP API unless you need REST API-specific features.
Code/Syntax Justification:
# HTTP API (cheaper, simpler)
Resources:
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
Auth:
DefaultAuthorizer: JWTAuthorizer
Authorizers:
JWTAuthorizer:
JwtConfiguration:
issuer: "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xxx"
audience:
- "app-client-id"
CorsConfiguration:
AllowOrigins: ["https://myapp.com"]
AllowMethods: ["GET", "POST"]
# REST API (full-featured)
Resources:
RestApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
ApiKeyRequired: true
UsagePlan:
Throttle:
BurstLimit: 100
RateLimit: 50
Quota:
Limit: 10000
Period: MONTH
Cors:
AllowMethods: "'*'"
AllowOrigin: "'https://myapp.com'"
π Behind the Question: API Gateway tier selection for cost and feature optimization. β The Red Flags: Using REST API for simple CRUD backends that don't need its advanced features.
Q203: How do you decide between EventBridge and SNS for event routing?β
Technical Answer: EventBridge: content-based filtering, schema registry, event replay, cross-account/SaaS integration, archive. SNS: simple pub/sub, higher throughput (30M msg/sec vs 2.4K events/sec for EventBridge), email/SMS/HTTP endpoints. Use EventBridge for complex routing; SNS for high-throughput simple fanout.
Code/Syntax Justification:
# EventBridge: content-based routing with rules
events_client = boto3.client("events")
events_client.put_events(Entries=[{
"Source": "order.service",
"DetailType": "OrderCreated",
"Detail": json.dumps({
"order_id": "123",
"amount": 500,
"customer_tier": "premium",
}),
"EventBusName": "orders",
}])
# EventBridge Rule: route only premium orders > $100
# Pattern: {"detail": {"customer_tier": ["premium"], "amount": [{"numeric": [">", 100]}]}}
# SNS: simple fanout to all subscribers
sns = boto3.client("sns")
sns.publish(
TopicArn="arn:aws:sns:us-east-1:123:order-events",
Message=json.dumps({"order_id": "123"}),
MessageAttributes={
"event_type": {"DataType": "String", "StringValue": "OrderCreated"},
},
)
π Behind the Question: Event routing architecture decisions. β The Red Flags: Using SNS with filter policies when EventBridge rules would be cleaner.
Q204: How do you decide between Step Functions Standard vs Express Workflows?β
Technical Answer: Standard: durable, up to 1 year execution, exactly-once semantics, $25/1M state transitions. Express: short-lived (<5 min), at-least-once, $1/1M requests + duration β 80% cheaper for high-volume short workflows. Use Express for data processing, Standard for long-running orchestration.
Code/Syntax Justification:
# Standard workflow: long-running order fulfillment
Resources:
OrderFulfillment:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineType: STANDARD
Definition:
StartAt: ValidateOrder
States:
ValidateOrder:
Type: Task
Resource: "arn:aws:lambda:...:validate"
Next: WaitForPayment
WaitForPayment:
Type: Wait
Seconds: 86400 # Wait up to 24 hours
Next: ProcessPayment
# Express workflow: real-time data processing
DataProcessor:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineType: EXPRESS
Definition:
StartAt: Parse
States:
Parse:
Type: Task
Resource: "arn:aws:lambda:...:parse"
Next: Enrich
Enrich:
Type: Task
Resource: "arn:aws:lambda:...:enrich"
Next: Store
Store:
Type: Task
Resource: "arn:aws:lambda:...:store"
End: true
π Behind the Question: Workflow engine selection for cost and durability trade-offs. β The Red Flags: Using Standard workflows for high-volume, short-lived processing (cost explosion).
Q205: How do you decide between Lambda Layers vs container images?β
Technical Answer: Layers: share code/dependencies across functions, max 5 layers per function, total 250MB unzipped. Container images: up to 10GB, custom runtimes, standard Docker build tooling, ECR hosting. Use layers for shared libraries; containers for large ML models, custom binaries, or complex builds.
Code/Syntax Justification:
# Container image Lambda
FROM public.ecr.aws/lambda/python:3.12
# Install system dependencies
RUN dnf install -y gcc gcc-c++ && dnf clean all
# Install Python packages
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY app.py .
CMD ["app.handler"]
# Layer-based Lambda
Resources:
SharedDepsLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared-deps
ContentUri: layers/shared-deps/
CompatibleRuntimes:
- python3.12
Metadata:
BuildMethod: python3.12
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Layers:
- !Ref SharedDepsLayer
- !Ref DataLibsLayer
π Behind the Question: Deployment packaging strategies. β The Red Flags: Using 10GB container images when a 50MB layer would suffice.
Q206: How do you design Lambda-based multi-tenant architectures?β
Technical Answer: Three isolation models: (1) Shared Lambda with tenant ID in context (lowest cost, least isolation). (2) Lambda per tenant with separate IAM roles (high isolation, high cost). (3) Shared Lambda with tenant-scoped data access (balanced). Use tenant ID from JWT/API key, scope all data access, and implement per-tenant throttling.
Code/Syntax Justification:
from functools import wraps
import json
def extract_tenant(func):
"""Decorator to extract and validate tenant context."""
@wraps(func)
def wrapper(event, context):
# Extract tenant from JWT claims
claims = event.get("requestContext", {}).get("authorizer", {}).get("claims", {})
tenant_id = claims.get("custom:tenant_id")
if not tenant_id:
return {"statusCode": 403, "body": json.dumps({"error": "Missing tenant"})}
# Inject tenant into event
event["_tenant_id"] = tenant_id
return func(event, context)
return wrapper
@extract_tenant
def handler(event, context):
tenant_id = event["_tenant_id"]
# All queries scoped to tenant
items = table.query(
KeyConditionExpression=Key("pk").eq(f"TENANT#{tenant_id}"),
)["Items"]
return {"statusCode": 200, "body": json.dumps(items, default=str)}
# Per-tenant rate limiting
def check_tenant_quota(tenant_id: str, limit: int = 1000):
"""Track per-tenant API usage."""
usage = rate_table.update_item(
Key={"pk": f"QUOTA#{tenant_id}", "sk": today()},
UpdateExpression="ADD #c :one",
ExpressionAttributeNames={"#c": "count"},
ExpressionAttributeValues={":one": 1},
ReturnValues="UPDATED_NEW",
)
return int(usage["Attributes"]["count"]) <= limit
π Behind the Question: SaaS architecture patterns with serverless. β The Red Flags: No tenant isolation in data access or no per-tenant usage tracking.
Q207: How do you design Lambda functions for testability?β
Technical Answer: Separate business logic from AWS SDK calls using dependency injection or adapter patterns. Use handler wrappers that extract clean inputs from event structures. Mock AWS services with moto or localstack. Test business logic independently of Lambda runtime.
Code/Syntax Justification:
# GOOD: Testable design with dependency injection
from dataclasses import dataclass
from typing import Protocol
class Repository(Protocol):
def get_order(self, order_id: str) -> dict: ...
def save_order(self, order: dict) -> None: ...
class DynamoDBRepository:
def __init__(self, table):
self.table = table
def get_order(self, order_id: str) -> dict:
return self.table.get_item(Key={"pk": order_id}).get("Item", {})
def save_order(self, order: dict) -> None:
self.table.put_item(Item=order)
def process_order(order_data: dict, repo: Repository) -> dict:
"""Pure business logic β no AWS dependencies."""
order = repo.get_order(order_data["order_id"])
order["status"] = "processed"
order["total"] = sum(item["price"] * item["qty"] for item in order_data["items"])
repo.save_order(order)
return order
# Lambda handler: thin wrapper
import boto3
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table("orders")
repo = DynamoDBRepository(table)
def handler(event, context):
order_data = json.loads(event.get("body", "{}"))
result = process_order(order_data, repo)
return {"statusCode": 200, "body": json.dumps(result, default=str)}
# Test: inject mock repository
class MockRepository:
def __init__(self):
self.orders = {}
def get_order(self, order_id):
return self.orders.get(order_id, {"pk": order_id})
def save_order(self, order):
self.orders[order["pk"]] = order
def test_process_order():
repo = MockRepository()
repo.orders["order-1"] = {"pk": "order-1", "status": "pending"}
result = process_order({"order_id": "order-1", "items": [{"price": 10, "qty": 2}]}, repo)
assert result["status"] == "processed"
assert result["total"] == 20
π Behind the Question: Clean architecture principles applied to serverless. β The Red Flags: Lambda handlers with business logic, AWS SDK calls, and error handling all mixed together.
Q208: How do you implement Lambda-based health checks for upstream dependencies?β
Technical Answer: Create a health check Lambda that verifies connectivity to all dependencies (databases, caches, external APIs, queues). Schedule it to run every minute. Route 53 health checks can call Lambda function URLs to determine routing decisions.
Code/Syntax Justification:
import boto3
import json
import time
import os
import socket
def handler(event, context):
checks = {}
start = time.time()
# Check DynamoDB
try:
dynamodb = boto3.client("dynamodb")
dynamodb.describe_table(TableName=os.environ["TABLE_NAME"])
checks["dynamodb"] = {"status": "healthy", "latency_ms": round((time.time() - start) * 1000)}
except Exception as e:
checks["dynamodb"] = {"status": "unhealthy", "error": str(e)}
# Check Redis
try:
sock = socket.create_connection((os.environ["REDIS_HOST"], 6379), timeout=3)
sock.close()
checks["redis"] = {"status": "healthy"}
except Exception as e:
checks["redis"] = {"status": "unhealthy", "error": str(e)}
# Check external API
try:
import requests
resp = requests.get(os.environ["API_HEALTH_URL"], timeout=5)
checks["external_api"] = {"status": "healthy" if resp.status_code == 200 else "degraded"}
except Exception as e:
checks["external_api"] = {"status": "unhealthy", "error": str(e)}
overall = "healthy" if all(c["status"] == "healthy" for c in checks.values()) else "unhealthy"
return {
"statusCode": 200 if overall == "healthy" else 503,
"body": json.dumps({"status": overall, "checks": checks, "duration_ms": round((time.time() - start) * 1000)}),
}
π Behind the Question: Dependency health monitoring for serverless applications. β The Red Flags: No proactive health checking of downstream dependencies.
Q209: How do you estimate Lambda costs for a new workload?β
Technical Answer: Cost = (invocations Γ $0.20/1M) + (GB-seconds Γ $0.0000166667) + API Gateway costs + data transfer. Calculate GB-seconds from memory Γ average duration. Include Provisioned Concurrency cost if used. Compare with Fargate at steady-state loads.
Code/Syntax Justification:
def estimate_lambda_cost(
invocations_per_month: int,
avg_duration_ms: int,
memory_mb: int,
provisioned_concurrency: int = 0,
) -> dict:
# Lambda pricing (us-east-1)
invocation_cost = (invocations_per_month / 1_000_000) * 0.20
gb_seconds = (invocations_per_month * avg_duration_ms / 1000) * (memory_mb / 1024)
compute_cost = gb_seconds * 0.0000166667
# Free tier: 1M requests + 400,000 GB-seconds
free_invocations = min(invocations_per_month, 1_000_000)
free_gb_seconds = min(gb_seconds, 400_000)
total = invocation_cost + compute_cost
# Provisioned Concurrency (if used)
pc_cost = 0
if provisioned_concurrency > 0:
pc_gb_hours = provisioned_concurrency * (memory_mb / 1024) * 730 # hours/month
pc_cost = pc_gb_hours * 0.0000041667 * 3600 # per GB-second provisioned
return {
"invocation_cost": round(invocation_cost, 2),
"compute_cost": round(compute_cost, 2),
"provisioned_cost": round(pc_cost, 2),
"total_monthly": round(total + pc_cost, 2),
"cost_per_invocation": round((total + pc_cost) / max(invocations_per_month, 1) * 1000, 4), # per 1K
}
# Example: API backend
print(estimate_lambda_cost(
invocations_per_month=5_000_000,
avg_duration_ms=200,
memory_mb=256,
))
π Behind the Question: Cost modeling for serverless architecture decisions. β The Red Flags: Deploying Lambda workloads without cost estimation or comparison to alternatives.
Q210: How do you implement Lambda-based A/B testing?β
Technical Answer: Use Lambda function aliases with weighted routing (90/10 split) for backend A/B tests. For feature-level tests, use feature flags with user-segment assignment stored in DynamoDB. Track metrics per variant via CloudWatch custom metrics or analytics events.
Code/Syntax Justification:
import hashlib
import json
def get_variant(user_id: str, experiment: str, split: float = 0.5) -> str:
"""Deterministic variant assignment based on user ID."""
hash_input = f"{user_id}:{experiment}"
hash_val = int(hashlib.md5(hash_input.encode()).hexdigest(), 16)
return "treatment" if (hash_val % 100) < (split * 100) else "control"
def handler(event, context):
user_id = event["requestContext"]["authorizer"]["principalId"]
# Assign variant
variant = get_variant(user_id, "new_pricing_v2", split=0.2)
if variant == "treatment":
price = new_pricing_algorithm(event)
else:
price = current_pricing_algorithm(event)
# Track experiment metric
cloudwatch = boto3.client("cloudwatch")
cloudwatch.put_metric_data(
Namespace="ABTests",
MetricData=[{
"MetricName": "ConversionRate",
"Dimensions": [
{"Name": "Experiment", "Value": "new_pricing_v2"},
{"Name": "Variant", "Value": variant},
],
"Value": 1 if event.get("converted") else 0,
"Unit": "Count",
}],
)
return {
"statusCode": 200,
"headers": {"X-Experiment-Variant": variant},
"body": json.dumps({"price": price}),
}
π Behind the Question: Experimentation infrastructure with serverless. β The Red Flags: Non-deterministic variant assignment or no metric tracking per variant.
Q211: How do you handle Lambda cold start latency for user-facing APIs?β
Technical Answer: Strategies ranked by effectiveness: (1) Provisioned Concurrency (eliminates cold starts, costs money). (2) Smaller deployment packages (faster init). (3) ARM64 architecture (Graviton2 β 20% faster, 20% cheaper). (4) SnapStart (Java only). (5) Keep-warm with scheduled pings (unreliable). (6) Lazy initialization of heavy dependencies.
Code/Syntax Justification:
Resources:
ApiFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Architectures:
- arm64 # Graviton2: 20% faster, 20% cheaper
MemorySize: 512 # More memory = more CPU = faster init
AutoPublishAlias: live
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 5 # Eliminates cold starts
# Auto-scale provisioned concurrency based on utilization
PCScaling:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MinCapacity: 5
MaxCapacity: 50
ResourceId: !Sub function:${ApiFunction}:live
ScalableDimension: lambda:function:ProvisionedConcurrency
ServiceNamespace: lambda
PCScalingPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
ScalableTargetId: !Ref PCScaling
PolicyType: TargetTrackingScaling
TargetTrackingScalingPolicyConfiguration:
TargetValue: 0.7 # Scale when 70% of provisioned is used
PredefinedMetricSpecification:
PredefinedMetricType: LambdaProvisionedConcurrencyUtilization
π Behind the Question: Latency optimization for production serverless APIs. β The Red Flags: Using x86 architecture without evaluating Graviton2, or keep-warm hacks instead of Provisioned Concurrency.
Q212: How do you implement observability dashboards for Lambda applications?β
Technical Answer: Use CloudWatch dashboards with custom metrics: invocation count, duration percentiles (p50/p90/p99), error rate, throttle count, concurrent executions, and iterator age (for stream consumers). Add business metrics via EMF (Embedded Metric Format) for zero-cost custom metrics.
Code/Syntax Justification:
# Embedded Metric Format β zero-cost custom metrics
import json
def emit_metric(metric_name, value, unit="Count", dimensions=None):
"""Emit CloudWatch metric via EMF β no API call needed."""
metric = {
"_aws": {
"Timestamp": int(time.time() * 1000),
"CloudWatchMetrics": [{
"Namespace": "MyApp",
"Dimensions": [list((dimensions or {}).keys())],
"Metrics": [{"Name": metric_name, "Unit": unit}],
}],
},
metric_name: value,
**(dimensions or {}),
}
print(json.dumps(metric)) # CloudWatch agent picks this up automatically
def handler(event, context):
start = time.time()
result = process(event)
# Business metrics β free via EMF
emit_metric("OrderProcessed", 1, dimensions={"Status": result["status"]})
emit_metric("OrderAmount", result["amount"], unit="None", dimensions={"Region": result["region"]})
emit_metric("ProcessingLatency", (time.time() - start) * 1000, unit="Milliseconds")
return result
π Behind the Question: Operational visibility for serverless applications. β The Red Flags: No custom business metrics or relying solely on default Lambda metrics.
Q213: How do you implement Lambda-based CQRS (Command Query Responsibility Segregation)?β
Technical Answer: Commands (writes) go to one Lambda β DynamoDB. Queries (reads) go to a different Lambda β read-optimized store (ElastiCache, OpenSearch, Aurora Read Replica). DynamoDB Streams syncs writes to the read store asynchronously. This separates read/write scaling and optimization.
Code/Syntax Justification:
# Command handler: write to DynamoDB
def command_handler(event, context):
body = json.loads(event["body"])
command = body["command"]
if command == "CreateOrder":
order = {
"pk": f"ORDER#{body['order_id']}",
"sk": "METADATA",
"status": "pending",
"items": body["items"],
"total": sum(i["price"] * i["qty"] for i in body["items"]),
"created_at": datetime.utcnow().isoformat(),
}
orders_table.put_item(Item=order)
return {"statusCode": 201, "body": json.dumps({"order_id": body["order_id"]})}
# Stream handler: sync to read store
def stream_handler(event, context):
for record in event["Records"]:
if record["eventName"] in ("INSERT", "MODIFY"):
item = deserialize(record["dynamodb"]["NewImage"])
# Update read-optimized ElastiCache
redis_client.hset(f"order:{item['pk']}", mapping=item)
# Update search index
es_client.index(index="orders", id=item["pk"], body=item)
# Query handler: read from optimized store
def query_handler(event, context):
query_type = event["queryStringParameters"].get("type")
if query_type == "by_id":
order_id = event["pathParameters"]["id"]
cached = redis_client.hgetall(f"order:ORDER#{order_id}")
return {"statusCode": 200, "body": json.dumps(cached)}
if query_type == "search":
q = event["queryStringParameters"]["q"]
results = es_client.search(index="orders", body={"query": {"match": {"items": q}}})
return {"statusCode": 200, "body": json.dumps(results["hits"]["hits"])}
π Behind the Question: Advanced architectural patterns for read/write optimization. β The Red Flags: Using the same data store and access patterns for both reads and writes at scale.
Q214: How do you implement Lambda-based webhook delivery with reliability?β
Technical Answer: Store webhook subscriptions in DynamoDB. On events, enqueue delivery to SQS with per-subscriber messages. Lambda delivers with retry, exponential backoff, and circuit breaking per endpoint. Track delivery status and automatically disable failing endpoints.
Code/Syntax Justification:
import requests
import hmac
import hashlib
import json
import time
def deliver_webhook(event, context):
for record in event["Records"]:
delivery = json.loads(record["body"])
url = delivery["url"]
payload = delivery["payload"]
secret = delivery["secret"]
attempt = delivery.get("attempt", 1)
# Sign payload
signature = hmac.new(
secret.encode(),
json.dumps(payload).encode(),
hashlib.sha256,
).hexdigest()
try:
response = requests.post(
url,
json=payload,
headers={
"X-Webhook-Signature": signature,
"X-Webhook-ID": delivery["webhook_id"],
"X-Webhook-Timestamp": str(int(time.time())),
},
timeout=10,
)
if response.status_code >= 200 and response.status_code < 300:
update_delivery_status(delivery["webhook_id"], "delivered")
else:
handle_failure(delivery, f"HTTP {response.status_code}", attempt)
except requests.exceptions.Timeout:
handle_failure(delivery, "timeout", attempt)
except requests.exceptions.ConnectionError:
handle_failure(delivery, "connection_error", attempt)
def handle_failure(delivery, error, attempt):
max_attempts = 5
if attempt >= max_attempts:
update_delivery_status(delivery["webhook_id"], "failed")
disable_endpoint_if_persistent(delivery["url"])
return
# Re-enqueue with backoff delay
delay = min(60 * (2 ** attempt), 3600)
delivery["attempt"] = attempt + 1
sqs.send_message(
QueueUrl=os.environ["WEBHOOK_QUEUE"],
MessageBody=json.dumps(delivery),
DelaySeconds=min(delay, 900),
)
π Behind the Question: Reliable event delivery to external systems. β The Red Flags: Fire-and-forget webhook delivery without retries or status tracking.
π Behind the Question: Reliable event delivery to external systems. β The Red Flags: Fire-and-forget webhook delivery without retries or status tracking.
Section 40: Lambda Security Deep Diveβ
Q215: How do you implement least-privilege IAM policies for Lambda?β
Technical Answer:
Start with zero permissions and add only what's needed. Use resource-level ARNs (not *), condition keys for additional restrictions, and separate roles per function. Use IAM Access Analyzer to identify unused permissions and refine policies over time.
Code/Syntax Justification:
Resources:
OrderProcessorRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: OrderProcessorPolicy
PolicyDocument:
Statement:
# Specific table, specific actions
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:Query
Resource:
- !GetAtt OrdersTable.Arn
- !Sub "${OrdersTable.Arn}/index/*"
# Specific queue
- Effect: Allow
Action:
- sqs:SendMessage
Resource: !GetAtt NotificationQueue.Arn
# Specific secret
- Effect: Allow
Action:
- secretsmanager:GetSecretValue
Resource: !Ref DatabaseSecret
Condition:
StringEquals:
"aws:RequestedRegion": !Ref AWS::Region
# CloudWatch Logs (required)
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/order-processor:*"
π Behind the Question: Security principle of least privilege in serverless.
β The Red Flags: Using Resource: "*" or Action: "*" in Lambda execution roles.
Q216: How do you securely manage secrets in Lambda?β
Technical Answer: Use AWS Secrets Manager or SSM Parameter Store (SecureString) for secrets. Cache secrets in memory with TTL using Lambda extensions or SDK. Never hardcode secrets in code, environment variables (visible in console), or commit to source control. Rotate secrets automatically.
Code/Syntax Justification:
import boto3
import json
import time
import os
# Cached secret with TTL
_secret_cache = {}
_cache_ttl = 300 # 5 minutes
def get_secret(secret_name: str) -> dict:
now = time.time()
if secret_name in _secret_cache and _secret_cache[secret_name]["expires"] > now:
return _secret_cache[secret_name]["value"]
client = boto3.client("secretsmanager")
response = client.get_secret_value(SecretId=secret_name)
secret = json.loads(response["SecretString"])
_secret_cache[secret_name] = {"value": secret, "expires": now + _cache_ttl}
return secret
def handler(event, context):
# Get database credentials securely
db_creds = get_secret(os.environ["DB_SECRET_ARN"])
conn = connect_db(
host=db_creds["host"],
user=db_creds["username"],
password=db_creds["password"],
)
return process_with_db(conn, event)
π Behind the Question: Secret management lifecycle in serverless. β The Red Flags: Storing secrets in Lambda environment variables or hardcoding in source code.
Q217: How do you implement Lambda function signing and code integrity verification?β
Technical Answer: AWS Signer creates code signing configurations that verify deployment packages haven't been tampered with. Lambda rejects unsigned or invalid-signed code at deploy time. This prevents unauthorized code deployment even if someone has IAM permissions to update the function.
Code/Syntax Justification:
Resources:
SigningProfile:
Type: AWS::Signer::SigningProfile
Properties:
PlatformId: AWSLambda-SHA384-ECDSA
CodeSigningConfig:
Type: AWS::Lambda::CodeSigningConfig
Properties:
AllowedPublishers:
SigningProfileVersionArns:
- !GetAtt SigningProfile.ProfileVersionArn
CodeSigningPolicies:
UntrustedArtifactOnDeployment: Enforce # Reject unsigned code
SecureFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeSigningConfigArn: !GetAtt CodeSigningConfig.CodeSigningConfigArn
# Sign deployment package
aws signer start-signing-job \
--source '{"s3":{"bucketName":"deploy-bucket","key":"function.zip","version":"abc123"}}' \
--destination '{"s3":{"bucketName":"signed-bucket"}}' \
--profile-name my-signing-profile
π Behind the Question: Supply chain security for serverless deployments. β The Red Flags: No code signing β anyone with deploy permissions can push arbitrary code.
Q218: How do you implement WAF protection for Lambda APIs?β
Technical Answer: Attach AWS WAF to API Gateway (REST API) or CloudFront (for HTTP API/Function URLs). WAF rules block SQL injection, XSS, rate-limit by IP, geo-restrict, and apply custom rules. Use AWS Managed Rules for common protections and custom rules for application-specific logic.
Code/Syntax Justification:
Resources:
WebACL:
Type: AWS::WAFv2::WebACL
Properties:
Scope: REGIONAL
DefaultAction:
Allow: {}
Rules:
- Name: AWSManagedRulesCommonRuleSet
Priority: 1
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesCommonRuleSet
OverrideAction:
None: {}
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: CommonRules
- Name: RateLimitPerIP
Priority: 2
Statement:
RateBasedStatement:
Limit: 2000
AggregateKeyType: IP
Action:
Block: {}
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: RateLimit
- Name: SQLInjectionProtection
Priority: 3
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesSQLiRuleSet
OverrideAction:
None: {}
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: SQLi
WAFAssociation:
Type: AWS::WAFv2::WebACLAssociation
Properties:
ResourceArn: !Sub "arn:aws:apigateway:${AWS::Region}::/restapis/${RestApi}/stages/prod"
WebACLArn: !GetAtt WebACL.Arn
π Behind the Question: Layer 7 security for serverless APIs. β The Red Flags: Public API Gateway endpoints without WAF protection.
Q219: How do you implement input sanitization in Lambda handlers?β
Technical Answer: Validate and sanitize all inputs before processing. Use Pydantic for schema validation, bleach for HTML sanitization, and parameterized queries for database calls. Never trust event data β it can be crafted by attackers. Validate path parameters, query strings, headers, and body.
Code/Syntax Justification:
import json
import re
from html import escape
def sanitize_string(value: str, max_length: int = 1000) -> str:
if not isinstance(value, str):
raise ValueError(f"Expected string, got {type(value)}")
value = value[:max_length]
value = escape(value) # HTML entity encoding
value = re.sub(r"[^\w\s@.\-+]", "", value) # Allow only safe chars
return value.strip()
def handler(event, context):
try:
body = json.loads(event.get("body", "{}"))
except json.JSONDecodeError:
return {"statusCode": 400, "body": json.dumps({"error": "Invalid JSON"})}
# Sanitize inputs
try:
name = sanitize_string(body.get("name", ""), max_length=100)
email = sanitize_string(body.get("email", ""), max_length=254)
if not re.match(r"^[\w.+-]+@[\w-]+\.[\w.]+$", email):
raise ValueError("Invalid email format")
except (ValueError, TypeError) as e:
return {"statusCode": 422, "body": json.dumps({"error": str(e)})}
# Parameterized query β never string interpolation
cursor.execute(
"INSERT INTO users (name, email) VALUES (%s, %s)",
(name, email), # Parameters, not f-string
)
return {"statusCode": 201, "body": json.dumps({"name": name, "email": email})}
π Behind the Question: Defense against injection attacks in serverless. β The Red Flags: Using f-strings or format() for SQL/NoSQL queries with user input.
Q220: How do you implement Lambda resource policies for cross-service access control?β
Technical Answer: Resource-based policies on Lambda control who can invoke the function. Unlike IAM policies (attached to callers), resource policies are attached to the function itself. They enable cross-account access, API Gateway invocation, and S3 event triggers without modifying the caller's IAM.
Code/Syntax Justification:
import boto3
lambda_client = boto3.client("lambda")
# Allow specific S3 bucket to invoke Lambda
lambda_client.add_permission(
FunctionName="image-processor",
StatementId="AllowS3Invoke",
Action="lambda:InvokeFunction",
Principal="s3.amazonaws.com",
SourceArn="arn:aws:s3:::my-images-bucket",
SourceAccount="123456789012", # Prevent confused deputy
)
# Allow specific API Gateway to invoke Lambda
lambda_client.add_permission(
FunctionName="api-handler",
StatementId="AllowAPIGateway",
Action="lambda:InvokeFunction",
Principal="apigateway.amazonaws.com",
SourceArn="arn:aws:execute-api:us-east-1:123456789012:abc123/*/GET/users",
)
# View current resource policy
policy = lambda_client.get_policy(FunctionName="image-processor")
print(json.dumps(json.loads(policy["Policy"]), indent=2))
π Behind the Question: Authorization model for Lambda invocation. β The Red Flags: Using wildcard SourceArn or omitting SourceAccount (confused deputy vulnerability).
Q221: How do you implement encryption at rest and in transit for Lambda?β
Technical Answer: Lambda encrypts environment variables with KMS (default AWS-managed or customer-managed CMK). Data in /tmp is encrypted. Use HTTPS for all external calls. For sensitive data in transit between services, use envelope encryption. Enable encryption on all data stores (S3 SSE, DynamoDB encryption, RDS encryption).
Code/Syntax Justification:
Resources:
EncryptionKey:
Type: AWS::KMS::Key
Properties:
Description: Lambda encryption key
KeyPolicy:
Statement:
- Sid: AllowLambdaUse
Effect: Allow
Principal:
AWS: !GetAtt FunctionRole.Arn
Action:
- kms:Decrypt
- kms:GenerateDataKey
Resource: "*"
SecureFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
KmsKeyArn: !GetAtt EncryptionKey.Arn # Encrypt env vars with CMK
Environment:
Variables:
ENCRYPTED_API_KEY: "AQICAHh..." # KMS-encrypted value
import boto3
import base64
kms = boto3.client("kms")
# Decrypt environment variable encrypted with KMS
def get_decrypted_env(var_name: str) -> str:
encrypted = base64.b64decode(os.environ[var_name])
response = kms.decrypt(CiphertextBlob=encrypted)
return response["Plaintext"].decode()
# Envelope encryption for data in transit
def encrypt_payload(plaintext: bytes, key_id: str) -> dict:
data_key = kms.generate_data_key(KeyId=key_id, KeySpec="AES_256")
from cryptography.fernet import Fernet
f = Fernet(base64.urlsafe_b64encode(data_key["Plaintext"][:32]))
return {
"encrypted_data": base64.b64encode(f.encrypt(plaintext)).decode(),
"encrypted_key": base64.b64encode(data_key["CiphertextBlob"]).decode(),
}
π Behind the Question: Data protection at all layers of serverless architecture. β The Red Flags: Using default KMS key without understanding key rotation or access policies.
Q222: How do you prevent Lambda injection attacks via event data?β
Technical Answer: Event data from API Gateway, SQS, S3, etc. can contain malicious payloads. Validate event structure before processing. For API events, validate Content-Type, check body size limits, and reject unexpected fields. Use allowlist validation (accept known good) over denylist (reject known bad).
Code/Syntax Justification:
import json
from typing import Any
def validate_api_event(event: dict) -> dict:
"""Validate and sanitize API Gateway event."""
# Validate Content-Type
content_type = event.get("headers", {}).get("content-type", "")
if "application/json" not in content_type:
raise ValueError(f"Unsupported Content-Type: {content_type}")
# Validate body size
body = event.get("body", "")
if len(body) > 1_000_000: # 1MB limit
raise ValueError("Request body too large")
# Parse and validate JSON
try:
parsed = json.loads(body)
except json.JSONDecodeError:
raise ValueError("Invalid JSON body")
# Reject unexpected top-level keys (allowlist)
allowed_keys = {"name", "email", "items", "address"}
unexpected = set(parsed.keys()) - allowed_keys
if unexpected:
raise ValueError(f"Unexpected fields: {unexpected}")
# Validate nested structures aren't too deep
if get_depth(parsed) > 5:
raise ValueError("JSON nesting too deep")
return parsed
def get_depth(obj: Any, current: int = 0) -> int:
if isinstance(obj, dict):
return max((get_depth(v, current + 1) for v in obj.values()), default=current)
if isinstance(obj, list):
return max((get_depth(v, current + 1) for v in obj), default=current)
return current
π Behind the Question: Input validation defense in depth. β The Red Flags: Processing event data without structure validation or size limits.
Q223: How do you implement audit trails for Lambda function changes?β
Technical Answer: Enable CloudTrail for Lambda API calls (CreateFunction, UpdateFunctionCode, UpdateFunctionConfiguration, AddPermission). Use Config Rules to detect non-compliant configurations. Alert on unauthorized changes via EventBridge rules matching CloudTrail events.
Code/Syntax Justification:
Resources:
LambdaChangeAlertRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source: ["aws.lambda"]
detail-type: ["AWS API Call via CloudTrail"]
detail:
eventName:
- UpdateFunctionCode
- UpdateFunctionConfiguration
- CreateFunction
- DeleteFunction
- AddPermission
- RemovePermission
- PutFunctionConcurrency
Targets:
- Arn: !GetAtt AlertFunction.Arn
Id: LambdaChangeAlert
def alert_handler(event, context):
detail = event["detail"]
sns = boto3.client("sns")
sns.publish(
TopicArn=os.environ["ALERT_TOPIC"],
Subject=f"Lambda Change: {detail['eventName']}",
Message=json.dumps({
"event": detail["eventName"],
"function": detail.get("requestParameters", {}).get("functionName"),
"user": detail.get("userIdentity", {}).get("arn"),
"source_ip": detail.get("sourceIPAddress"),
"time": detail.get("eventTime"),
"region": detail.get("awsRegion"),
}, indent=2),
)
π Behind the Question: Change management and compliance auditing. β The Red Flags: No monitoring or alerting on Lambda function configuration changes.
Q224: How do you implement Lambda function isolation for multi-tenant security?β
Technical Answer: Options: (1) Shared function with tenant-scoped IAM session policies via STS AssumeRole. (2) Separate functions per tenant with isolated IAM roles. (3) Shared function with row-level security via tenant ID in all queries. Choose based on compliance requirements and cost tolerance.
Code/Syntax Justification:
import boto3
sts = boto3.client("sts")
def get_tenant_session(tenant_id: str):
"""Assume tenant-specific role with scoped permissions."""
response = sts.assume_role(
RoleArn=f"arn:aws:iam::123456789012:role/tenant-{tenant_id}-role",
RoleSessionName=f"lambda-{tenant_id}",
Policy=json.dumps({
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["dynamodb:*"],
"Resource": f"arn:aws:dynamodb:*:*:table/tenant-{tenant_id}-*",
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": [f"TENANT#{tenant_id}"]
}
}
}]
}),
DurationSeconds=900,
)
return boto3.Session(
aws_access_key_id=response["Credentials"]["AccessKeyId"],
aws_secret_access_key=response["Credentials"]["SecretAccessKey"],
aws_session_token=response["Credentials"]["SessionToken"],
)
def handler(event, context):
tenant_id = event["requestContext"]["authorizer"]["claims"]["custom:tenant_id"]
session = get_tenant_session(tenant_id)
table = session.resource("dynamodb").Table("shared-table")
# All operations scoped to tenant via IAM session policy
items = table.query(KeyConditionExpression=Key("pk").begins_with(f"TENANT#{tenant_id}"))
return {"statusCode": 200, "body": json.dumps(items["Items"], default=str)}
π Behind the Question: Data isolation in multi-tenant serverless. β The Red Flags: Relying only on application-level tenant filtering without IAM-level enforcement.
Q225: How do you implement Lambda function URL authentication with IAM?β
Technical Answer:
Function URLs with AuthType: AWS_IAM require SigV4 signed requests. Callers must have lambda:InvokeFunctionUrl permission. Use boto3 or aws-requests-auth to sign requests from other services. For public access with authentication, use Cognito + CloudFront.
Code/Syntax Justification:
# Calling IAM-protected Function URL from another service
import requests
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
import boto3
def call_function_url(url: str, payload: dict) -> dict:
session = boto3.Session()
credentials = session.get_credentials()
request = AWSRequest(
method="POST",
url=url,
data=json.dumps(payload),
headers={"Content-Type": "application/json"},
)
SigV4Auth(credentials, "lambda", "us-east-1").add_auth(request)
response = requests.post(
url,
data=request.body,
headers=dict(request.headers),
)
return response.json()
# Usage
result = call_function_url(
"https://abc123.lambda-url.us-east-1.on.aws/",
{"action": "process", "data": [1, 2, 3]},
)
π Behind the Question: Secure service-to-service communication via Function URLs.
β The Red Flags: Using AuthType: NONE for internal service communication.
Q226: How do you prevent Lambda from being used as an open proxy?β
Technical Answer: Validate that outbound requests from Lambda only go to approved destinations. Use VPC with restricted security groups, DNS-based allowlisting, and application-level URL validation. Block SSRF attacks by rejecting requests to internal metadata endpoints (169.254.169.254).
Code/Syntax Justification:
import re
import ipaddress
from urllib.parse import urlparse
ALLOWED_DOMAINS = {"api.partner.com", "service.internal.com"}
BLOCKED_IP_RANGES = [
ipaddress.ip_network("169.254.0.0/16"), # Link-local / metadata
ipaddress.ip_network("10.0.0.0/8"), # Private
ipaddress.ip_network("172.16.0.0/12"), # Private
ipaddress.ip_network("192.168.0.0/16"), # Private
]
def validate_url(url: str) -> bool:
parsed = urlparse(url)
# Must be HTTPS
if parsed.scheme != "https":
return False
# Check against allowlist
if parsed.hostname not in ALLOWED_DOMAINS:
return False
# Resolve and check for private IPs (SSRF prevention)
import socket
try:
ip = ipaddress.ip_address(socket.gethostbyname(parsed.hostname))
for blocked in BLOCKED_IP_RANGES:
if ip in blocked:
return False
except (socket.gaierror, ValueError):
return False
return True
def handler(event, context):
target_url = event.get("url")
if not validate_url(target_url):
return {"statusCode": 403, "body": "URL not allowed"}
response = requests.get(target_url, timeout=5)
return {"statusCode": 200, "body": response.text[:1000]}
π Behind the Question: SSRF prevention in serverless functions. β The Red Flags: Lambda making outbound requests to user-supplied URLs without validation.
Q227: How do you implement API key management and rotation for Lambda APIs?β
Technical Answer: Use API Gateway usage plans with API keys for client identification (not authentication). For authentication, use Cognito, Lambda authorizers, or IAM. Implement key rotation by issuing new keys before deprecating old ones. Monitor key usage via CloudWatch.
Code/Syntax Justification:
import boto3
import json
from datetime import datetime, timedelta
apigw = boto3.client("apigateway")
def rotate_api_key(old_key_id: str, usage_plan_id: str) -> dict:
"""Rotate API key with overlap period."""
# Create new key
new_key = apigw.create_api_key(
name=f"client-key-{datetime.utcnow().strftime('%Y%m%d')}",
enabled=True,
)
# Associate with usage plan
apigw.create_usage_plan_key(
usagePlanId=usage_plan_id,
keyId=new_key["id"],
keyType="API_KEY",
)
# Schedule old key deactivation (allow overlap)
# In production, use EventBridge scheduled rule
return {
"new_key_id": new_key["id"],
"new_key_value": new_key["value"],
"old_key_id": old_key_id,
"deprecation_date": (datetime.utcnow() + timedelta(days=30)).isoformat(),
}
# Lambda authorizer for custom API key validation
def authorizer(event, context):
api_key = event.get("headers", {}).get("x-api-key", "")
# Validate against DynamoDB key store
key_record = keys_table.get_item(Key={"api_key": api_key}).get("Item")
if not key_record or key_record.get("revoked"):
return generate_deny_policy(event)
if key_record.get("expires_at") and key_record["expires_at"] < datetime.utcnow().isoformat():
return generate_deny_policy(event)
return generate_allow_policy(event, key_record["client_id"])
π Behind the Question: API key lifecycle management. β The Red Flags: Using API keys as the sole authentication mechanism or never rotating them.
Q228: How do you implement content security for Lambda API responses?β
Technical Answer: Set security headers on all API responses: Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, Strict-Transport-Security, and Cache-Control. Use API Gateway response templates or Lambda response middleware to enforce headers consistently.
Code/Syntax Justification:
SECURITY_HEADERS = {
"Strict-Transport-Security": "max-age=31536000; includeSubDomains",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"X-XSS-Protection": "1; mode=block",
"Content-Security-Policy": "default-src 'self'",
"Cache-Control": "no-store, no-cache, must-revalidate",
"Referrer-Policy": "strict-origin-when-cross-origin",
}
def add_security_headers(handler_func):
"""Decorator to add security headers to all responses."""
def wrapper(event, context):
response = handler_func(event, context)
if isinstance(response, dict):
headers = response.get("headers", {})
headers.update(SECURITY_HEADERS)
response["headers"] = headers
return response
return wrapper
@add_security_headers
def handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({"data": "sensitive_response"}),
}
π Behind the Question: HTTP security best practices for serverless APIs. β The Red Flags: API responses without security headers, especially missing HSTS and CSP.
Q229: How do you implement Lambda authorization with Cognito User Pools?β
Technical Answer: API Gateway validates JWT tokens from Cognito automatically. For fine-grained authorization, use Lambda authorizers that inspect Cognito claims (groups, custom attributes, scopes). Cache authorization results in API Gateway to reduce Lambda invocations.
Code/Syntax Justification:
import json
import time
import jwt # PyJWT library
import requests
# Cache JWKS keys
_jwks_cache = {"keys": None, "expires": 0}
def get_jwks(user_pool_id: str, region: str) -> dict:
now = time.time()
if _jwks_cache["keys"] and _jwks_cache["expires"] > now:
return _jwks_cache["keys"]
url = f"https://cognito-idp.{region}.amazonaws.com/{user_pool_id}/.well-known/jwks.json"
keys = requests.get(url).json()["keys"]
_jwks_cache.update({"keys": keys, "expires": now + 3600})
return keys
def authorizer(event, context):
token = event.get("authorizationToken", "").replace("Bearer ", "")
try:
# Decode and validate JWT
header = jwt.get_unverified_header(token)
jwks = get_jwks(os.environ["USER_POOL_ID"], os.environ["AWS_REGION"])
key = next(k for k in jwks if k["kid"] == header["kid"])
claims = jwt.decode(
token,
key=jwt.algorithms.RSAAlgorithm.from_jwk(key),
algorithms=["RS256"],
audience=os.environ["APP_CLIENT_ID"],
)
# Check group-based permissions
groups = claims.get("cognito:groups", [])
if "admin" in groups:
return generate_policy(claims["sub"], "Allow", event["methodArn"], claims)
elif "user" in groups:
return generate_policy(claims["sub"], "Allow", event["methodArn"], claims)
else:
return generate_policy(claims["sub"], "Deny", event["methodArn"])
except Exception as e:
print(f"Auth failed: {e}")
raise Exception("Unauthorized")
π Behind the Question: JWT-based authentication and authorization for serverless APIs. β The Red Flags: Not validating JWT signature, expiration, or audience claim.
Q230: How do you implement Lambda execution environment hardening?β
Technical Answer: Lambda runs in a microVM (Firecracker) with limited access. Additional hardening: disable unused SDK features, set strict timeout, limit memory to minimum needed, use runtime version pinning, enable X-Ray for anomaly detection, and implement application-level guardrails.
Code/Syntax Justification:
import os
import sys
# Disable unnecessary features
os.environ["AWS_SDK_LOAD_CONFIG"] = "0" # Don't load shared config
# Restrict module imports
BLOCKED_MODULES = {"subprocess", "os.system", "ctypes"}
def secure_import_hook(name, *args, **kwargs):
if name in BLOCKED_MODULES:
raise ImportError(f"Module {name} is blocked for security")
return original_import(name, *args, **kwargs)
# Memory-aware processing
def handler(event, context):
# Check memory allocation
import resource
mem_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 # MB
# Guard against memory exhaustion
memory_limit = int(os.environ.get("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"))
if mem_usage > memory_limit * 0.8:
print(f"WARNING: Memory usage at {mem_usage:.0f}MB / {memory_limit}MB")
# Time-aware processing
remaining = context.get_remaining_time_in_millis()
if remaining < 5000:
return {"statusCode": 503, "body": "Insufficient time remaining"}
return process(event)
π Behind the Question: Defense-in-depth for serverless execution environments. β The Red Flags: No resource monitoring or guardrails within the Lambda execution.
Q231: How do you implement secure cross-origin requests (CORS) for Lambda APIs?β
Technical Answer:
Configure CORS at the API Gateway level (preferred) with specific allowed origins, methods, and headers. Never use * for Access-Control-Allow-Origin in production. For preflight requests, API Gateway handles OPTIONS automatically. Validate Origin header in Lambda for sensitive operations.
Code/Syntax Justification:
ALLOWED_ORIGINS = {"https://app.example.com", "https://staging.example.com"}
def cors_handler(handler_func):
"""CORS middleware for Lambda handlers."""
def wrapper(event, context):
origin = event.get("headers", {}).get("origin", "")
# Validate origin
if origin not in ALLOWED_ORIGINS:
allowed_origin = "" # Don't reflect invalid origins
else:
allowed_origin = origin
# Handle preflight
if event.get("httpMethod") == "OPTIONS":
return {
"statusCode": 204,
"headers": {
"Access-Control-Allow-Origin": allowed_origin,
"Access-Control-Allow-Methods": "GET,POST,PUT,DELETE",
"Access-Control-Allow-Headers": "Content-Type,Authorization",
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Credentials": "true",
},
}
response = handler_func(event, context)
response.setdefault("headers", {})
response["headers"]["Access-Control-Allow-Origin"] = allowed_origin
response["headers"]["Access-Control-Allow-Credentials"] = "true"
return response
return wrapper
π Behind the Question: Secure cross-origin resource sharing configuration.
β The Red Flags: Using Access-Control-Allow-Origin: * with credentials or reflecting any origin.
Q232: How do you implement request throttling per authenticated user?β
Technical Answer: Use API Gateway usage plans for per-key throttling, or implement per-user throttling in Lambda with DynamoDB atomic counters. Track request counts per user per time window. Return 429 with Retry-After header when exceeded.
Code/Syntax Justification:
import boto3
import time
import json
dynamodb = boto3.resource("dynamodb")
throttle_table = dynamodb.Table("user_throttles")
def check_user_throttle(user_id: str, tier_limits: dict) -> tuple[bool, dict]:
"""Returns (allowed, info) tuple."""
now = int(time.time())
window = now // 60 # 1-minute windows
try:
result = throttle_table.update_item(
Key={"pk": f"USER#{user_id}", "sk": f"WINDOW#{window}"},
UpdateExpression="SET #cnt = if_not_exists(#cnt, :zero) + :one, #ttl = :ttl",
ExpressionAttributeNames={"#cnt": "request_count", "#ttl": "ttl"},
ExpressionAttributeValues={
":one": 1, ":zero": 0,
":ttl": now + 120,
},
ReturnValues="UPDATED_NEW",
)
count = int(result["Attributes"]["request_count"])
limit = tier_limits.get("requests_per_minute", 60)
return count <= limit, {
"remaining": max(0, limit - count),
"limit": limit,
"reset": (window + 1) * 60,
}
except Exception:
return True, {} # Fail open
def handler(event, context):
user_id = event["requestContext"]["authorizer"]["principalId"]
tier = event["requestContext"]["authorizer"].get("tier", "free")
tier_limits = {"free": {"requests_per_minute": 30}, "pro": {"requests_per_minute": 300}}
allowed, info = check_user_throttle(user_id, tier_limits.get(tier, tier_limits["free"]))
if not allowed:
return {
"statusCode": 429,
"headers": {
"Retry-After": str(info["reset"] - int(time.time())),
"X-RateLimit-Limit": str(info["limit"]),
"X-RateLimit-Remaining": "0",
},
"body": json.dumps({"error": "Rate limit exceeded"}),
}
response = process_request(event)
response.setdefault("headers", {})
response["headers"]["X-RateLimit-Remaining"] = str(info.get("remaining", ""))
return response
π Behind the Question: Per-user rate limiting for tiered API access. β The Red Flags: No per-user throttling β allowing single users to consume all available capacity.
Q233: How do you implement Lambda-based token refresh and session management?β
Technical Answer: Use Cognito for token management with Lambda triggers for custom flows. Implement refresh token rotation (invalidate old refresh tokens on use). Store session metadata in DynamoDB with TTL. Validate tokens on every request via Lambda authorizer with caching.
Code/Syntax Justification:
import boto3
import uuid
import time
import json
cognito = boto3.client("cognito-idp")
dynamodb = boto3.resource("dynamodb")
sessions_table = dynamodb.Table("sessions")
def refresh_token_handler(event, context):
refresh_token = json.loads(event["body"]).get("refresh_token")
try:
result = cognito.initiate_auth(
ClientId=os.environ["COGNITO_CLIENT_ID"],
AuthFlow="REFRESH_TOKEN_AUTH",
AuthParameters={"REFRESH_TOKEN": refresh_token},
)
auth_result = result["AuthenticationResult"]
session_id = str(uuid.uuid4())
# Track session
sessions_table.put_item(Item={
"session_id": session_id,
"user_id": decode_token(auth_result["IdToken"])["sub"],
"created_at": int(time.time()),
"ttl": int(time.time()) + auth_result.get("ExpiresIn", 3600),
})
return {
"statusCode": 200,
"body": json.dumps({
"access_token": auth_result["AccessToken"],
"id_token": auth_result["IdToken"],
"expires_in": auth_result["ExpiresIn"],
"session_id": session_id,
}),
}
except cognito.exceptions.NotAuthorizedException:
return {"statusCode": 401, "body": json.dumps({"error": "Invalid refresh token"})}
π Behind the Question: Secure token lifecycle management. β The Red Flags: Never expiring refresh tokens or not tracking active sessions.
Q234: How do you implement sensitive data redaction in Lambda logs?β
Technical Answer: Redact PII, credentials, and sensitive fields before logging. Use structured logging with a redaction middleware that masks known sensitive keys. Configure CloudWatch Logs data protection policies for automatic redaction of patterns like SSNs, credit cards, and emails.
Code/Syntax Justification:
import json
import re
import logging
SENSITIVE_KEYS = {"password", "ssn", "credit_card", "api_key", "secret", "token", "authorization"}
SENSITIVE_PATTERNS = [
(re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), "***-**-****"), # SSN
(re.compile(r"\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b"), "****-****-****-****"), # CC
(re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+"), "***@***.***"), # Email
]
def redact(data, depth=0):
if depth > 10:
return "[TRUNCATED]"
if isinstance(data, dict):
return {
k: "[REDACTED]" if k.lower() in SENSITIVE_KEYS else redact(v, depth + 1)
for k, v in data.items()
}
if isinstance(data, list):
return [redact(v, depth + 1) for v in data]
if isinstance(data, str):
for pattern, replacement in SENSITIVE_PATTERNS:
data = pattern.sub(replacement, data)
return data
logger = logging.getLogger()
def safe_log(message: str, data: dict = None, level: str = "info"):
log_entry = {"message": message}
if data:
log_entry["data"] = redact(data)
getattr(logger, level)(json.dumps(log_entry))
def handler(event, context):
safe_log("Processing request", {"event": event})
# Logs: {"message": "Processing request", "data": {"event": {"headers": {"authorization": "[REDACTED]"}}}}
return {"statusCode": 200}
π Behind the Question: PII protection in serverless logging. β The Red Flags: Logging full request/response bodies that may contain PII or credentials.
Section 41: Lambda Performance & Optimizationβ
Q235: How do you optimize Lambda memory configuration for cost-performance balance?β
Technical Answer: Lambda CPU scales proportionally with memory. Use AWS Lambda Power Tuning tool to find the optimal memory setting by testing your function at various levels (128MB to 10GB) and measuring duration, cost, and performance. Often, higher memory is cheaper because faster execution reduces GB-seconds.
Code/Syntax Justification:
# Lambda Power Tuning results analysis
def analyze_power_tuning_results(results: list[dict]) -> dict:
"""Find optimal memory/cost configuration."""
for r in results:
r["gb_seconds"] = (r["memory_mb"] / 1024) * (r["duration_ms"] / 1000)
r["cost_per_invocation"] = r["gb_seconds"] * 0.0000166667 + 0.0000002
# Find cheapest
cheapest = min(results, key=lambda r: r["cost_per_invocation"])
# Find fastest
fastest = min(results, key=lambda r: r["duration_ms"])
# Find best value (cost Γ duration balanced)
for r in results:
r["value_score"] = r["cost_per_invocation"] * r["duration_ms"]
best_value = min(results, key=lambda r: r["value_score"])
return {
"cheapest": {"memory": cheapest["memory_mb"], "cost": cheapest["cost_per_invocation"]},
"fastest": {"memory": fastest["memory_mb"], "duration": fastest["duration_ms"]},
"best_value": {"memory": best_value["memory_mb"]},
}
# Typical results for a Python API handler:
# 128MB: 2500ms, $0.000005208
# 256MB: 1200ms, $0.000005000 β often cheapest
# 512MB: 600ms, $0.000005000
# 1024MB: 300ms, $0.000005000
# 1769MB: 180ms, $0.000005300 β fastest under 200ms
π Behind the Question: Data-driven memory/cost optimization. β The Red Flags: Using default 128MB memory without performance testing.
Q236: How do you reduce Lambda deployment package size?β
Technical Answer:
Remove test files, docs, and unused dependencies. Use Lambda layers for shared libraries. Strip debug symbols from compiled extensions. Use pip install --no-cache-dir --target . -r requirements.txt with --platform for Lambda-compatible binaries. Consider container images for >250MB packages.
Code/Syntax Justification:
# Optimized packaging script
#!/bin/bash
set -e
PACKAGE_DIR="package"
rm -rf $PACKAGE_DIR && mkdir $PACKAGE_DIR
# Install only production dependencies with Lambda-compatible binaries
pip install \
--no-cache-dir \
--target $PACKAGE_DIR \
--platform manylinux2014_x86_64 \
--only-binary=:all: \
-r requirements.txt
# Remove unnecessary files
find $PACKAGE_DIR -type d -name "__pycache__" -exec rm -rf {} +
find $PACKAGE_DIR -type d -name "*.dist-info" -exec rm -rf {} +
find $PACKAGE_DIR -type d -name "tests" -exec rm -rf {} +
find $PACKAGE_DIR -name "*.pyc" -delete
find $PACKAGE_DIR -name "*.pyo" -delete
# Strip compiled extensions
find $PACKAGE_DIR -name "*.so" -exec strip {} +
# Copy application code
cp -r src/* $PACKAGE_DIR/
# Create zip
cd $PACKAGE_DIR && zip -r9 ../function.zip .
echo "Package size: $(du -sh ../function.zip | cut -f1)"
π Behind the Question: Deployment package optimization for faster cold starts. β The Red Flags: Including test frameworks, documentation, and unused dependencies in deployment packages.
Q237: How do you implement connection pooling for Lambda database access?β
Technical Answer: Lambda execution environments are reused for warm invocations. Initialize connection pools outside the handler. Use RDS Proxy for managed connection pooling that handles Lambda's bursty connection patterns. Without RDS Proxy, limit concurrent Lambda invocations to match database connection limits.
Code/Syntax Justification:
import os
import psycopg2
from psycopg2 import pool
# Initialize pool outside handler β shared across warm invocations
_connection_pool = None
def get_pool():
global _connection_pool
if _connection_pool is None or _connection_pool.closed:
_connection_pool = pool.SimpleConnectionPool(
minconn=1,
maxconn=2, # Small pool β one Lambda = one client
host=os.environ["DB_HOST"], # RDS Proxy endpoint preferred
port=5432,
database=os.environ["DB_NAME"],
user=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"],
connect_timeout=5,
options="-c statement_timeout=10000", # 10s query timeout
)
return _connection_pool
def handler(event, context):
conn = None
try:
conn = get_pool().getconn()
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM orders WHERE id = %s", (event["order_id"],))
result = cursor.fetchone()
conn.commit()
return {"statusCode": 200, "body": json.dumps(result, default=str)}
except Exception as e:
if conn:
conn.rollback()
raise
finally:
if conn:
get_pool().putconn(conn)
π Behind the Question: Database connection management in serverless. β The Red Flags: Creating new database connections per invocation or not using RDS Proxy.
Q238: How do you optimize Lambda for CPU-bound vs I/O-bound workloads?β
Technical Answer: CPU-bound: increase memory (proportionally increases CPU). 1,769MB gives one full vCPU. For multi-threaded CPU work, use 1,769MB+ to get multiple vCPUs. I/O-bound: use async/concurrent I/O (asyncio, concurrent.futures). Memory increase doesn't help I/O-bound functions β use concurrency instead.
Code/Syntax Justification:
import asyncio
import aiohttp
import concurrent.futures
import json
# I/O-bound: use async for parallel external calls
async def handler_io_bound(event, context):
urls = event.get("urls", [])
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
results = await asyncio.gather(*tasks, return_exceptions=True)
return {"statusCode": 200, "body": json.dumps(results, default=str)}
async def fetch_url(session, url):
async with session.get(url, timeout=aiohttp.ClientTimeout(total=5)) as resp:
return {"url": url, "status": resp.status}
# CPU-bound: use threads for parallel computation
def handler_cpu_bound(event, context):
data_chunks = event.get("chunks", [])
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
futures = [executor.submit(heavy_compute, chunk) for chunk in data_chunks]
results = [f.result() for f in concurrent.futures.as_completed(futures)]
return {"statusCode": 200, "body": json.dumps(results)}
def heavy_compute(chunk):
"""CPU-intensive computation β benefits from more memory/CPU."""
import numpy as np
data = np.array(chunk)
return {"mean": float(data.mean()), "std": float(data.std())}
π Behind the Question: Workload-aware performance optimization. β The Red Flags: Increasing memory for I/O-bound functions or using sequential I/O when parallel is possible.
Q239: How do you implement efficient JSON processing in Lambda?β
Technical Answer:
Use orjson or ujson for 2-5Γ faster JSON serialization/deserialization. For large JSON payloads, use streaming parsers (ijson). Pre-compile response templates. Minimize serialization by returning only needed fields.
Code/Syntax Justification:
# Fast JSON with orjson (2-5x faster than stdlib json)
import orjson
def handler(event, context):
# Fast deserialization
body = orjson.loads(event.get("body", b"{}"))
result = process(body)
# Fast serialization with options
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": orjson.dumps(
result,
option=orjson.OPT_UTC_Z | orjson.OPT_NAIVE_UTC | orjson.OPT_SERIALIZE_NUMPY,
).decode(),
}
# For very large JSON: streaming parser
import ijson
def process_large_json(s3_body):
"""Stream-parse JSON without loading entire file."""
items = ijson.items(s3_body, "records.item")
for item in items:
process_item(item) # Process one at a time, constant memory
π Behind the Question: JSON processing performance in Lambda.
β The Red Flags: Using stdlib json for high-throughput Lambda functions without evaluating faster alternatives.
Q240: How do you implement Lambda warming strategies?β
Technical Answer: Provisioned Concurrency (official, reliable, costs money). Scheduled warm-up pings (free but unreliable β only warms one instance). Pre-warm before known traffic spikes using EventBridge Scheduler + target tracking. Monitor cold start percentage via X-Ray.
Code/Syntax Justification:
# Handler with warmup detection
def handler(event, context):
# Detect and short-circuit warmup pings
if event.get("source") == "aws.scheduler" and event.get("detail-type") == "warmup":
return {"statusCode": 200, "body": "warm"}
if event.get("warmup"):
return {"statusCode": 200, "body": "warm"}
return process_request(event)
# Monitor cold starts via init duration
def log_cold_start_metric():
"""Emit cold start metric for monitoring."""
init_type = os.environ.get("AWS_LAMBDA_INITIALIZATION_TYPE", "")
is_cold = init_type == "on-demand"
if is_cold:
print(json.dumps({
"_aws": {
"CloudWatchMetrics": [{
"Namespace": "LambdaCustom",
"Metrics": [{"Name": "ColdStart", "Unit": "Count"}],
"Dimensions": [["FunctionName"]],
}],
},
"ColdStart": 1,
"FunctionName": os.environ["AWS_LAMBDA_FUNCTION_NAME"],
}))
π Behind the Question: Cold start mitigation strategies. β The Red Flags: Using ping-based warming as the sole cold start strategy for production APIs.
Q241: How do you optimize Lambda for batch processing throughput?β
Technical Answer:
Maximize batch size (up to 10,000 for SQS, 10,000 for Kinesis). Increase parallelization factor for Kinesis (up to 10). Use concurrent processing within each batch invocation. Filter events at the source. Process items in parallel with concurrent.futures.
Code/Syntax Justification:
import concurrent.futures
import json
def handler(event, context):
records = event["Records"]
batch_failures = []
# Process batch items in parallel
with concurrent.futures.ThreadPoolExecutor(max_workers=min(len(records), 10)) as executor:
future_to_record = {
executor.submit(process_record, record): record
for record in records
}
for future in concurrent.futures.as_completed(future_to_record):
record = future_to_record[future]
try:
future.result()
except Exception as e:
print(f"Failed: {record['messageId']}: {e}")
batch_failures.append({"itemIdentifier": record["messageId"]})
print(f"Batch: {len(records)} total, {len(batch_failures)} failed")
return {"batchItemFailures": batch_failures}
def process_record(record):
"""Process individual record β may involve I/O."""
body = json.loads(record["body"])
# External call, DB write, etc.
result = call_external_service(body)
save_to_database(body, result)
π Behind the Question: Maximizing throughput in batch Lambda processing. β The Red Flags: Processing batch items sequentially when they're independent.
Q242: How do you implement Lambda response caching?β
Technical Answer: Use API Gateway caching for GET endpoints (built-in, configurable TTL). For custom caching, use ElastiCache (Redis) or DynamoDB with TTL. Cache key should include relevant query parameters and user context. Implement cache invalidation on data changes.
Code/Syntax Justification:
import boto3
import json
import hashlib
import time
dynamodb = boto3.resource("dynamodb")
cache_table = dynamodb.Table("api_cache")
def cached_handler(ttl_seconds: int = 300):
def decorator(func):
def wrapper(event, context):
# Build cache key from request
method = event.get("httpMethod", "GET")
path = event.get("path", "/")
params = json.dumps(event.get("queryStringParameters") or {}, sort_keys=True)
cache_key = hashlib.sha256(f"{method}:{path}:{params}".encode()).hexdigest()
# Check cache
if method == "GET":
cached = cache_table.get_item(Key={"cache_key": cache_key}).get("Item")
if cached and int(cached.get("ttl", 0)) > int(time.time()):
response = json.loads(cached["response"])
response["headers"] = response.get("headers", {})
response["headers"]["X-Cache"] = "HIT"
return response
# Execute handler
response = func(event, context)
# Cache successful GET responses
if method == "GET" and response.get("statusCode") == 200:
cache_table.put_item(Item={
"cache_key": cache_key,
"response": json.dumps(response),
"ttl": int(time.time()) + ttl_seconds,
})
response.setdefault("headers", {})["X-Cache"] = "MISS"
return response
return wrapper
return decorator
@cached_handler(ttl_seconds=60)
def handler(event, context):
# Expensive query
data = query_database(event["queryStringParameters"])
return {"statusCode": 200, "body": json.dumps(data)}
π Behind the Question: Response caching for Lambda API performance. β The Red Flags: No caching for frequently accessed, rarely changing data.
Q243: How do you optimize Lambda cold starts for Python specifically?β
Technical Answer:
Python-specific optimizations: (1) Use slim base packages. (2) Avoid heavy imports at module level β lazy-load pandas, numpy, boto3 resources. (3) Pre-compile .pyc files in the package. (4) Use __slots__ in frequently instantiated classes. (5) Minimize __init__.py imports.
Code/Syntax Justification:
# Module-level: only import lightweight essentials
import os
import json
# Lazy-load heavy dependencies
_boto3 = None
_pandas = None
def get_boto3():
global _boto3
if _boto3 is None:
import boto3
_boto3 = boto3
return _boto3
def get_pandas():
global _pandas
if _pandas is None:
import pandas as pd
_pandas = pd
return _pandas
# Pre-initialize only what's always needed
TABLE_NAME = os.environ.get("TABLE_NAME", "")
# Use __slots__ for memory-efficient objects
class OrderItem:
__slots__ = ["id", "name", "price", "quantity"]
def __init__(self, id, name, price, quantity):
self.id = id
self.name = name
self.price = price
self.quantity = quantity
def handler(event, context):
# boto3 loaded only on first invocation
table = get_boto3().resource("dynamodb").Table(TABLE_NAME)
# pandas loaded only if needed
if event.get("generate_report"):
pd = get_pandas()
df = pd.DataFrame(event["data"])
return {"report": df.describe().to_dict()}
return {"statusCode": 200}
π Behind the Question: Language-specific cold start optimization. β The Red Flags: Importing pandas/numpy/scipy at module level when they're not needed for every invocation.
Q244: How do you implement efficient logging in high-throughput Lambda?β
Technical Answer: Use structured JSON logging for machine parsing. Batch log writes using print() with JSON (CloudWatch auto-captures stdout). Minimize log volume at INFO level; use DEBUG only when needed. Use log sampling for high-throughput functions. Leverage EMF for metrics without API calls.
Code/Syntax Justification:
import json
import os
import random
import time
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO")
LOG_SAMPLE_RATE = float(os.environ.get("LOG_SAMPLE_RATE", "1.0"))
class StructuredLogger:
def __init__(self, service: str):
self.service = service
self.context = {}
def set_context(self, **kwargs):
self.context.update(kwargs)
def _should_log(self) -> bool:
return random.random() < LOG_SAMPLE_RATE
def _emit(self, level: str, message: str, **extra):
if not self._should_log() and level not in ("ERROR", "WARN"):
return
entry = {
"timestamp": time.time(),
"level": level,
"service": self.service,
"message": message,
**self.context,
**extra,
}
print(json.dumps(entry, default=str))
def info(self, message, **extra): self._emit("INFO", message, **extra)
def error(self, message, **extra): self._emit("ERROR", message, **extra)
def warn(self, message, **extra): self._emit("WARN", message, **extra)
def debug(self, message, **extra):
if LOG_LEVEL == "DEBUG":
self._emit("DEBUG", message, **extra)
logger = StructuredLogger("order-service")
def handler(event, context):
logger.set_context(request_id=context.aws_request_id)
logger.info("Processing request", path=event.get("path"))
try:
result = process(event)
logger.info("Request complete", duration_ms=result.get("duration"))
return result
except Exception as e:
logger.error("Request failed", error=str(e), traceback=traceback.format_exc())
raise
π Behind the Question: Logging best practices for serverless at scale. β The Red Flags: Unstructured print() statements or logging every field of every request.
Q245: How do you implement efficient binary data processing in Lambda?β
Technical Answer:
Use base64 encoding for API Gateway binary payloads. For S3 objects, stream using range requests instead of loading full files. Use io.BytesIO for in-memory binary manipulation. Set appropriate Content-Type and isBase64Encoded in responses.
Code/Syntax Justification:
import base64
import io
import gzip
import json
def handler(event, context):
# Receive binary data via API Gateway
if event.get("isBase64Encoded"):
body = base64.b64decode(event["body"])
else:
body = event.get("body", "").encode()
# Process binary (e.g., gzip compressed data)
if event.get("headers", {}).get("content-encoding") == "gzip":
body = gzip.decompress(body)
data = json.loads(body)
result = process(data)
# Return compressed response
response_body = json.dumps(result).encode()
compressed = gzip.compress(response_body)
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
"body": base64.b64encode(compressed).decode(),
"isBase64Encoded": True,
}
π Behind the Question: Binary data handling in serverless APIs. β The Red Flags: Not supporting compressed request/response bodies for bandwidth optimization.
Q246: How do you implement Lambda function composition for microservices?β
Technical Answer: Use event-driven composition (SQS/SNS/EventBridge between functions) for loose coupling. Use Step Functions for orchestrated workflows. Avoid direct Lambda-to-Lambda calls. Share data via DynamoDB or S3, not by passing large payloads between functions.
Code/Syntax Justification:
# Event-driven composition via EventBridge
import boto3
import json
events = boto3.client("events")
# Service A: publish domain event
def order_service_handler(event, context):
order = create_order(event)
# Publish event β subscribers decide what to do
events.put_events(Entries=[{
"Source": "order-service",
"DetailType": "OrderCreated",
"Detail": json.dumps({
"order_id": order["id"],
"user_id": order["user_id"],
"total": order["total"],
"items": order["items"],
}),
"EventBusName": "main-bus",
}])
return {"statusCode": 201, "body": json.dumps(order)}
# Service B: react to event (separate Lambda)
def inventory_handler(event, context):
"""Triggered by EventBridge rule matching OrderCreated."""
detail = event["detail"]
for item in detail["items"]:
reserve_inventory(item["product_id"], item["quantity"])
# Service C: react to same event (separate Lambda)
def notification_handler(event, context):
"""Triggered by EventBridge rule matching OrderCreated."""
detail = event["detail"]
send_confirmation_email(detail["user_id"], detail["order_id"])
π Behind the Question: Microservices composition patterns with serverless. β The Red Flags: Direct synchronous Lambda-to-Lambda calls creating a distributed monolith.
Q247: How do you implement Lambda performance baselines and regression detection?β
Technical Answer: Track p50/p90/p99 duration, cold start frequency, error rate, and memory utilization over time. Set CloudWatch anomaly detection alarms that learn normal patterns and alert on deviations. Compare metrics before and after deployments. Use X-Ray service maps for dependency latency.
Code/Syntax Justification:
Resources:
LatencyAnomalyAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: lambda-latency-anomaly
MetricName: Duration
Namespace: AWS/Lambda
Dimensions:
- Name: FunctionName
Value: !Ref MyFunction
Statistic: p99
Period: 300
EvaluationPeriods: 3
ThresholdMetricId: anomaly_band
ComparisonOperator: GreaterThanUpperThreshold
Metrics:
- Id: duration
MetricStat:
Metric:
Namespace: AWS/Lambda
MetricName: Duration
Dimensions:
- Name: FunctionName
Value: !Ref MyFunction
Period: 300
Stat: p99
- Id: anomaly_band
Expression: "ANOMALY_DETECTION_BAND(duration, 2)"
AlarmActions:
- !Ref AlertTopic
# Post-deployment performance validation
def validate_deployment(function_name: str, baseline: dict) -> bool:
cw = boto3.client("cloudwatch")
now = datetime.utcnow()
current = get_metric_stats(cw, function_name, now - timedelta(minutes=30), now)
checks = {
"p99_ok": current["p99"] < baseline["p99"] * 1.5, # Allow 50% increase max
"error_rate_ok": current["error_rate"] < baseline["error_rate"] + 0.01,
"cold_start_ok": current["cold_start_pct"] < 10,
}
if not all(checks.values()):
print(f"Performance regression detected: {checks}")
return False
return True
π Behind the Question: Performance monitoring and regression prevention. β The Red Flags: No performance baselines or anomaly detection for Lambda functions.
Q248: How do you optimize Lambda for ARM64 (Graviton2) architecture?β
Technical Answer:
Graviton2 (ARM64) provides 20% better price-performance for most workloads. Switch by changing Architectures: [arm64] in configuration. Ensure all Lambda layers and native dependencies are compiled for ARM64. Test thoroughly β some Python C extensions may not have ARM builds.
Code/Syntax Justification:
Resources:
OptimizedFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Architectures:
- arm64 # Graviton2: 20% cheaper, ~20% faster
MemorySize: 512
Layers:
- !Ref SharedDepsLayerArm64 # ARM64-compiled layer
# Build ARM64-compatible dependencies
pip install \
--platform manylinux2014_aarch64 \
--target package/ \
--only-binary=:all: \
-r requirements.txt
# Verify architecture compatibility
python -c "
import platform
print(f'Architecture: {platform.machine()}')
print(f'Python: {platform.python_version()}')
"
π Behind the Question: Architecture optimization for cost savings. β The Red Flags: Not evaluating Graviton2 for Lambda functions β leaving 20% cost savings on the table.
Q249: How do you implement efficient S3 transfer patterns in Lambda?β
Technical Answer:
Use multipart upload for files >100MB. Use transfer acceleration for cross-region uploads. Stream processing with Body.read(chunk_size) for memory efficiency. Use presigned URLs for client-direct uploads (bypass Lambda entirely for large files).
Code/Syntax Justification:
import boto3
from boto3.s3.transfer import TransferConfig
s3 = boto3.client("s3")
# Multipart upload configuration
config = TransferConfig(
multipart_threshold=8 * 1024 * 1024, # 8MB
max_concurrency=10,
multipart_chunksize=8 * 1024 * 1024,
)
def handler(event, context):
action = event.get("action")
if action == "generate_upload_url":
# Client-direct upload (Lambda doesn't handle the data)
presigned = s3.generate_presigned_post(
Bucket=os.environ["UPLOAD_BUCKET"],
Key=f"uploads/{event['filename']}",
Conditions=[
["content-length-range", 1, 100 * 1024 * 1024], # Max 100MB
{"Content-Type": event.get("content_type", "application/octet-stream")},
],
ExpiresIn=3600,
)
return {"statusCode": 200, "body": json.dumps(presigned)}
if action == "stream_process":
# Stream large file without loading into memory
obj = s3.get_object(Bucket=event["bucket"], Key=event["key"])
line_count = 0
for chunk in obj["Body"].iter_lines():
line_count += 1
if line_count % 10000 == 0:
process_batch(chunk)
return {"processed_lines": line_count}
π Behind the Question: Efficient large file handling with serverless. β The Red Flags: Routing large file uploads through Lambda instead of using presigned URLs.
Q250: How do you implement Lambda middleware patterns?β
Technical Answer: Use decorator-based middleware for cross-cutting concerns: authentication, validation, logging, error handling, CORS, and caching. Stack decorators from outermost (first to execute) to innermost. Lambda Powertools provides built-in middleware decorators.
Code/Syntax Justification:
import json
import time
import traceback
from functools import wraps
def error_handler(func):
@wraps(func)
def wrapper(event, context):
try:
return func(event, context)
except ValueError as e:
return {"statusCode": 400, "body": json.dumps({"error": str(e)})}
except PermissionError as e:
return {"statusCode": 403, "body": json.dumps({"error": str(e)})}
except Exception as e:
print(json.dumps({"level": "ERROR", "error": str(e), "traceback": traceback.format_exc()}))
return {"statusCode": 500, "body": json.dumps({"error": "Internal server error"})}
return wrapper
def timing(func):
@wraps(func)
def wrapper(event, context):
start = time.time()
result = func(event, context)
duration = (time.time() - start) * 1000
if isinstance(result, dict):
result.setdefault("headers", {})["X-Response-Time"] = f"{duration:.0f}ms"
return result
return wrapper
def require_auth(func):
@wraps(func)
def wrapper(event, context):
auth = event.get("requestContext", {}).get("authorizer", {})
if not auth.get("principalId"):
return {"statusCode": 401, "body": json.dumps({"error": "Unauthorized"})}
return func(event, context)
return wrapper
# Stack middleware (outermost runs first)
@error_handler
@timing
@require_auth
def handler(event, context):
return {"statusCode": 200, "body": json.dumps({"data": "protected"})}
π Behind the Question: Clean separation of cross-cutting concerns in Lambda. β The Red Flags: Mixing authentication, logging, error handling, and business logic in one function body.
Q251: How do you implement Lambda environment variable management across stages?β
Technical Answer:
Use SSM Parameter Store with naming conventions (/app/stage/key) for stage-specific configuration. Resolve at deploy time via CloudFormation resolve:ssm: or at runtime via SDK. Use .env files for local development, SAM parameter overrides for deployment.
Code/Syntax Justification:
Parameters:
Stage:
Type: String
Default: dev
AllowedValues: [dev, staging, prod]
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Environment:
Variables:
STAGE: !Ref Stage
TABLE_NAME: !Sub "orders-${Stage}"
# Resolve SSM parameter at deploy time
API_KEY: !Sub "{{resolve:ssm:/myapp/${Stage}/api-key}}"
# Secure parameter (resolved at deploy time, encrypted at rest)
DB_PASSWORD: !Sub "{{resolve:ssm-secure:/myapp/${Stage}/db-password}}"
import os
# Runtime resolution for frequently changing config
def get_config(key: str) -> str:
stage = os.environ.get("STAGE", "dev")
ssm = boto3.client("ssm")
param = ssm.get_parameter(
Name=f"/myapp/{stage}/{key}",
WithDecryption=True,
)
return param["Parameter"]["Value"]
π Behind the Question: Configuration management across environments. β The Red Flags: Hardcoding stage-specific values or using the same config across dev/staging/prod.
Q252: How do you implement Lambda-based data transformation with Kinesis Firehose?β
Technical Answer:
Kinesis Firehose invokes a Lambda transformation function on each batch of records before delivery to S3/Redshift/OpenSearch. The function receives base64-encoded records, transforms them, and returns with result: "Ok", "Dropped", or "ProcessingFailed" per record.
Code/Syntax Justification:
import base64
import json
def handler(event, context):
output = []
for record in event["records"]:
# Decode input
payload = base64.b64decode(record["data"]).decode()
try:
data = json.loads(payload)
# Transform
transformed = {
"event_type": data.get("type"),
"user_id": data.get("userId"),
"timestamp": data.get("ts"),
"properties": data.get("props", {}),
"processed_at": datetime.utcnow().isoformat(),
}
# Filter out internal events
if data.get("type", "").startswith("internal_"):
output.append({
"recordId": record["recordId"],
"result": "Dropped",
"data": record["data"],
})
continue
# Encode output (must be base64, newline-delimited for Athena)
output_data = json.dumps(transformed) + "\n"
output.append({
"recordId": record["recordId"],
"result": "Ok",
"data": base64.b64encode(output_data.encode()).decode(),
})
except Exception as e:
print(f"Transform failed for {record['recordId']}: {e}")
output.append({
"recordId": record["recordId"],
"result": "ProcessingFailed",
"data": record["data"],
})
return {"records": output}
π Behind the Question: Stream transformation with managed delivery. β The Red Flags: Not handling transformation failures per-record or not adding newlines for Athena compatibility.
Q253: How do you implement cost allocation tags for Lambda functions?β
Technical Answer: Tag all Lambda functions with cost allocation tags (team, project, environment, cost-center). Activate tags in AWS Cost Explorer for per-function and per-team cost visibility. Use tag-based IAM policies for access control. Enforce tagging via SCP or Config Rules.
Code/Syntax Justification:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Tags:
Environment: prod
Team: payments
Project: order-processing
CostCenter: CC-1234
Owner: payments-team@company.com
DataClassification: confidential
# Enforce tagging via Config Rule
def config_rule_handler(event, context):
"""Check Lambda functions have required tags."""
config = boto3.client("config")
lambda_client = boto3.client("lambda")
required_tags = {"Environment", "Team", "Project", "CostCenter"}
non_compliant = []
functions = lambda_client.list_functions()["Functions"]
for func in functions:
tags = lambda_client.list_tags(Resource=func["FunctionArn"])["Tags"]
missing = required_tags - set(tags.keys())
if missing:
non_compliant.append({
"function": func["FunctionName"],
"missing_tags": list(missing),
})
return {"non_compliant_count": len(non_compliant), "details": non_compliant}
π Behind the Question: Cost visibility and accountability for serverless. β The Red Flags: Lambda functions without cost allocation tags β impossible to attribute costs.
Q254: How do you implement Lambda-based canary deployments with automatic rollback?β
Technical Answer: Use CodeDeploy with Lambda aliases for gradual traffic shifting. Configure pre/post-traffic hooks for smoke tests. Set CloudWatch alarms on error rate and latency as rollback triggers. CodeDeploy automatically rolls back if alarms fire during deployment.
Code/Syntax Justification:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
AutoPublishAlias: live
DeploymentPreference:
Type: Linear10PercentEvery1Minute # 10% more traffic every minute
Alarms:
- !Ref ErrorRateAlarm
- !Ref LatencyP99Alarm
Hooks:
PreTraffic: !Ref PreTrafficValidation
PostTraffic: !Ref PostTrafficValidation
ErrorRateAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum
Period: 60
EvaluationPeriods: 1
Threshold: 5
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref MyFunction
- Name: Resource
Value: !Sub "${MyFunction}:live"
LatencyP99Alarm:
Type: AWS::CloudWatch::Alarm
Properties:
MetricName: Duration
Namespace: AWS/Lambda
ExtendedStatistic: p99
Period: 60
EvaluationPeriods: 2
Threshold: 5000 # 5 seconds
ComparisonOperator: GreaterThanThreshold
π Behind the Question: Safe deployment practices for serverless production. β The Red Flags: All-at-once deployments without rollback capability or deployment validation.
Section 42: Lambda Multi-Account & Cross-Region Patternsβ
QN: How do you implement cross-account Lambda invocation via EventBridge?β
Technical Answer: Cross-account invocation requires a resource-based policy on the target Lambda function and a Bus-to-Bus routing configuration. You create an EventBridge rule in Account A that targets the Default (or custom) event bus in Account B. Account B's bus must have a policy allowing events:PutEvents from Account A. Finally, a rule in Account B triggers the Lambda function based on the incoming event pattern.
Code/Syntax Justification:
// Policy on Account B Event Bus
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAccountA",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::ACCOUNT_A_ID:root" },
"Action": "events:PutEvents",
"Resource": "arn:aws:events:us-east-1:ACCOUNT_B_ID:event-bus/default"
}
]
}
π Behind the Question: Tests understanding of decentralized event-driven architectures and IAM resource-based policies. β The Red Flags: Suggesting hard-coded IAM users or making the Lambda public.
QN: What is the "Global Endpoint" pattern for Lambda + API Gateway?β
Technical Answer: It involves deploying API Gateway and Lambda in two or more regions and using Route 53 with health checks or Global Accelerator. Route 53 routes traffic to the healthy region. The Lambda functions should ideally be "stateless" or use a global data store like DynamoDB Global Tables to ensure consistent data across regions.
Code/Syntax Justification:
Route 53 Failover Routing: Primary (Region A) -> Secondary (Region B). Health checks monitor the API endpoint in Region A; if it fails, DNS records update to point to Region B.
π Behind the Question: Knowledge of high availability and multi-region disaster recovery. β The Red Flags: Assuming data sync happens automatically between regions without Global Tables or custom logic.
QN: How do you handle Lambda execution in a "Landing Zone" multi-account environment?β
Technical Answer: Use AWS Control Tower and AWS Organizations. Centralize logs in a "Log Archive" account via Kinesis Data Firehose or cross-account CloudWatch log destination. Centralize security monitoring in a "Security" account using GuardDuty and Security Hub, which analyze Lambda execution logs and VPC flow logs for anomalies.
Code/Syntax Justification:
aws logs put-destination: Creates a destination in the Log account.
aws logs put-subscription-filter: Applied to Lambda log groups in member accounts to stream logs to the central destination.
π Behind the Question: Understanding of enterprise-scale AWS management and compliance. β The Red Flags: Suggesting manual log export via the console for hundreds of accounts.
QN: Explain the constraints of cross-region Lambda@Edge.β
Technical Answer: Lambda@Edge functions are authored in us-east-1 but replicated globally. Constraints include: No environment variables (must use hardcoded config or S3 fetch), 128MB-1536MB memory limit, 5-second timeout for viewer triggers, and limited access to the response body.
Code/Syntax Justification:
# Lambda@Edge cannot use os.environ['DB_URL']
# Accessing config from a hardcoded S3 URL is common
import json
def handler(event, context):
request = event['Records'][0]['cf']['request']
# Logic here...
return request
π Behind the Question: Knowledge of CloudFront extensibility and its specific limitations. β The Red Flags: Attempting to use large libraries or long-running database queries in Edge functions.
QN: How do you implement a "Warm Standby" for a Lambda-based API?β
Technical Answer: Deploy the full stack (APIGW + Lambda + DynamoDB) in two regions. Region A is active. Region B is "Warm" (Provisioned Concurrency set to 1 or higher) to eliminate cold starts during a failover. Route 53 ARC (Application Recovery Controller) can be used to manage the routing shift with high confidence.
Code/Syntax Justification:
aws lambda put-provisioned-concurrency-config: Used in the standby region to ensure immediate readiness.
π Behind the Question: Differentiating between cold standby, warm standby, and active-active architectures. β The Red Flags: Relying on default concurrency in the standby region, leading to a "thundering herd" cold start during failover.
QN: What is the "Event Bus Centralization" pattern?β
Technical Answer: In a multi-account setup, every account has its own Event Bus, but all "public" events are forwarded to a Central Event Bus in a shared services account. This central bus acts as the "Broker" that routes events back out to subscriber accounts. This reduces the number of cross-account permissions required between individual microservices.
Code/Syntax Justification: Service A (Account 1) -> Event Bus A -> Rule (Target: Central Bus) -> Central Bus (Account 2) -> Rule (Target: Event Bus B) -> Service B (Account 3).
π Behind the Question: Knowledge of scalable event-driven patterns in large organizations. β The Red Flags: Creating mesh-like point-to-point cross-account permissions for every service.
QN: How do you manage secrets across multiple AWS accounts for Lambda?β
Technical Answer: Use AWS Secrets Manager with resource-based policies. Account A (Secret Owner) grants secretsmanager:GetSecretValue to the IAM Role of the Lambda in Account B. Alternatively, use a central "Secrets Account" and have Lambdas assume a cross-account role specifically to fetch secrets, though resource-based policies are more performant.
Code/Syntax Justification:
// Secret Policy in Account A
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::ACCOUNT_B:role/LambdaRole" },
"Action": "secretsmanager:GetSecretValue",
"Resource": "*"
}
π Behind the Question: Secure handling of credentials in complex account structures. β The Red Flags: Replicating secrets manually via CI/CD (risk of desync and leak).
QN: How does "CloudFront Functions" differ from "Lambda@Edge" for multi-region use?β
Technical Answer: CloudFront Functions are lightweight JavaScript (not Node.js) functions that run at the Edge location (218+ locations) with <1ms execution time and very low cost. Lambda@Edge runs at Regional Edge Caches (approx 13 locations), supports full Node/Python, and handles heavier logic but with higher latency.
Code/Syntax Justification:
Use CloudFront Functions for header manipulation, URL rewrites. Use Lambda@Edge for SEO/Server-side rendering or complex auth.
π Behind the Question: Cost and performance optimization at the edge. β The Red Flags: Using Lambda@Edge for simple header additions that CloudFront Functions could handle 10x cheaper.
QN: How do you handle cross-account Lambda triggers from S3?β
Technical Answer: S3 in Account A triggers Lambda in Account B. Two steps: 1. The Lambda resource policy must allow s3.amazonaws.com with a SourceArn check on Account A's bucket. 2. The S3 bucket notification must be configured. Note: S3 requires the Lambda function to be in the same region, even if in a different account.
Code/Syntax Justification:
aws lambda add-permission --function-name MyFunc \
--statement-id s3-cross-account --action "lambda:InvokeFunction" \
--principal s3.amazonaws.com --source-arn arn:aws:s3:::account-a-bucket
π Behind the Question: Understanding resource-based permissions vs identity-based.
β The Red Flags: Forgetting to add the lambda:InvokeFunction permission, leading to silent failures in S3 notifications.
QN: What is the impact of AWS Organizations Service Control Policies (SCPs) on Lambda?β
Technical Answer: SCPs define the maximum available permissions for an account. If an SCP denies lambda:CreateFunction, even an Administrator in that account cannot create one. Common use: Deny Lambda functions that are not attached to a VPC or deny creating functions in non-approved regions.
Code/Syntax Justification:
// SCP to enforce VPC-only Lambdas
{
"Effect": "Deny",
"Action": "lambda:CreateFunction",
"Condition": { "Null": { "lambda:VpcIds": "true" } }
}
π Behind the Question: Understanding organizational-level security boundaries. β The Red Flags: Thinking IAM "Allow" can override an SCP "Deny".
QN: How do you troubleshoot cross-account EventBridge routing?β
Technical Answer: Check the "Trigger" metrics in the source account and "Invocations" in the destination. Common failures: Missing events:PutEvents permission on the target bus, incorrect event pattern in the source rule, or the target bus being in a different region (EventBridge cross-account targets must be in the same region unless using specific cross-region rules).
Code/Syntax Justification:
Check FailedInvocations metric on the source rule. If > 0, the event reached the rule but couldn't reach the target bus.
π Behind the Question: Practical debugging of distributed systems. β The Red Flags: Assuming CloudWatch logs in the source account will show the destination account's errors.
QN: Explain "Multi-Region Active-Active" Lambda with DynamoDB.β
Technical Answer: Deploy API Gateway and Lambda in Region A and B. Both are active. Route 53 Latency-based routing sends users to the closest region. DynamoDB Global Tables replicate data bi-directionally. Conflict resolution is usually "Last Writer Wins," so the Lambda code must handle potential data consistency issues (e.g., using atomic increments).
Code/Syntax Justification:
dynamodb update-table --replica-updates create...: Configures the global table.
π Behind the Question: High-tier availability architecture. β The Red Flags: Not handling the "Read-after-write" consistency delay between regions (usually < 1 sec but non-zero).
QN: How do you implement cross-account access for Lambda to an Aurora RDS Cluster?β
Technical Answer: The Lambda VPC and Aurora VPC must be connected via VPC Peering or Transit Gateway. The Aurora Security Group must allow the CIDR block of the Lambda's subnets. For credentials, the Lambda assumes a role in the Aurora account or uses a cross-account Secrets Manager policy.
Code/Syntax Justification:
VPC Peering: Required for IP connectivity. Security Group Inbound Rule: Protocol TCP, Port 5432, Source: Lambda_Subnet_CIDR.
π Behind the Question: Networking and security in multi-account environments. β The Red Flags: Trying to connect over the public internet to a private RDS instance.
QN: What is the "Cell-Based Architecture" for Lambda?β
Technical Answer: A scalability pattern where the application is split into multiple independent "cells" (e.g., Account 1, Account 2...). A "Thin" routing layer (Global Accelerator or a Lambda) determines which cell should handle a specific user. This limits the "Blast Radius" of a failure or a regional service limit hit.
Code/Syntax Justification: Request -> Global Router -> Cell 4 (Account 4) -> (APIGW + Lambda + DB).
π Behind the Question: Advanced scalability and reliability engineering. β The Red Flags: Putting all users into a single region/account and hitting the 1000-concurrency default limit.
QN: How do you automate cross-account Lambda deployments?β
Technical Answer: Use a Central CI/CD account (e.g., with CodePipeline). The pipeline assumes a "CrossAccountDeploymentRole" in the Target Account. The role must have permissions for CloudFormation, Lambda, IAM, and S3. The S3 bucket holding the Lambda zip must have a policy allowing access from the Target Account's role.
Code/Syntax Justification:
AssumeRole: The core mechanism for cross-account CI/CD actions.
π Behind the Question: DevOps/SRE knowledge in AWS environments. β The Red Flags: Storing IAM Access Keys of target accounts in the CI/CD environment variables.
QN: How do you handle "Split-View DNS" for Lambda in a multi-account VPC setup?β
Technical Answer: Use Route 53 Private Hosted Zones (PHZ). Associate the PHZ in Account A (the service provider) with the VPC in Account B (the consumer). This allows the Lambda in Account B to resolve internal-service.local to the Private IP of an Internal Load Balancer or VPC Endpoint in Account A.
Code/Syntax Justification:
aws route53 associate-vpc-with-hosted-zone: Authorizes the cross-account association.
π Behind the Question: Internal networking and service discovery. β The Red Flags: Relying on public DNS records for internal-only VPC communication.
QN: What is the "Aggregator" pattern in multi-account serverless?β
Technical Answer: A central Lambda function uses AssumeRole to query multiple accounts (e.g., fetching billing or audit data). The function must use sts:AssumeRole in a loop (or concurrently) and create a new Boto3 client for each account.
Code/Syntax Justification:
def get_client(account_id):
role_arn = f"arn:aws:iam::{account_id}:role/ReadOnlyRole"
creds = sts.assume_role(RoleArn=role_arn, RoleSessionName="Agg")['Credentials']
return boto3.client('ec2',
aws_access_key_id=creds['AccessKeyId'],
aws_secret_access_key=creds['SecretAccessKey'],
aws_session_token=creds['SessionToken']
)
π Behind the Question: Automation and monitoring at scale. β The Red Flags: Not caching credentials, leading to STS rate-limiting if many accounts are queried frequently.
QN: How do you ensure compliance of Lambda functions across all accounts?β
Technical Answer: Use AWS Config Rules (Aggregator). Define a custom Lambda function in the central account that evaluates the configuration of Lambdas in all member accounts (e.g., "Must have tagging," "No public access"). If non-compliant, it triggers a remediation Lambda (e.g., auto-deleting the public trigger).
Code/Syntax Justification:
AWS Config: Source of truth for resource state. Config Aggregator: Views all accounts in one place.
π Behind the Question: Governance and automated remediation. β The Red Flags: Attempting to manually audit hundreds of accounts via the console.
Section 43: Lambda Event-Driven Saga & Workflow Patternsβ
QN: Explain the "Choreography" vs "Orchestration" Saga patterns in Lambda.β
Technical Answer: In Choreography, each Lambda emits an event (e.g., to EventBridge) and other Lambdas listen and react. There is no central coordinator. In Orchestration, a Step Functions state machine coordinates the flow, explicitly calling each Lambda and handling transitions.
Code/Syntax Justification:
- Choreography: Lambda A -> EventBridge -> Lambda B.
- Orchestration: Step Functions -> Task(Lambda A) -> Task(Lambda B).
π Behind the Question: Distributed systems architecture knowledge. β The Red Flags: Using Choreography for complex flows with 5+ steps where visibility and debugging become a nightmare ("Event Hell").
QN: How do you implement a "Compensating Transaction" in a Lambda Saga?β
Technical Answer: If Step 2 of a process fails, you must undo Step 1. In Step Functions, you use a Catch block on the task. The Catch block transitions to a "Compensating Lambda" (e.g., RefundPayment if ShipItem failed). This ensures eventual consistency.
Code/Syntax Justification:
"ProcessPayment": {
"Type": "Task",
"Resource": "...",
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "RefundPayment" }]
}
π Behind the Question: Error handling in distributed transactions. β The Red Flags: Simply stopping on error, leaving the system in an inconsistent state (e.g., payment taken but no order created).
QN: What is the "Transactional Outbox" pattern for Lambda?β
Technical Answer: To ensure a database update and an event emission are atomic, you write the event to an "Outbox" table in the same database transaction as the business logic. A separate "Relay" (a Lambda triggered by DynamoDB Streams or a poller) reads the Outbox table and publishes to EventBridge/SQS.
Code/Syntax Justification:
Transaction: Update Order + Insert into OutboxTable. Trigger: OutboxStream -> Lambda -> SNS/EventBridge.
π Behind the Question: Reliability and atomicity in event-driven systems.
β The Red Flags: Updating the DB and then calling events.put_events() in the same Lambda (if the event call fails after DB commit, data is out of sync).
QN: How do you handle "Idempotency" in a Lambda-based Saga?β
Technical Answer: Every step must be idempotent. Use a unique RequestID or OrderID as a key. Before processing, check a "Lease" or "ProcessedEvents" table (DynamoDB). If the ID exists, return the cached response. The aws-lambda-powertools library provides a decorator for this.
Code/Syntax Justification:
from aws_lambda_powertools.utilities.idempotency import idempotent
@idempotent(persistence_store=persistence_layer)
def handler(event, context):
return process_order(event['order_id'])
π Behind the Question: Preventing duplicate side effects (e.g., double charging a card). β The Red Flags: Assuming SQS "exactly-once" delivery (it's actually "at-least-once").
QN: Explain the "Routing Slip" pattern in serverless.β
Technical Answer: Instead of hardcoding the next step, the event contains a "Slip" (a list of tasks). Each Lambda performs its task, pops itself off the slip, and forwards the event to the next destination in the list. Useful for dynamic workflows where steps vary per request.
Code/Syntax Justification:
Event: {"tasks": ["Validate", "Charge", "Notify"], "payload": {...}}. Lambda 1 pops "Validate," performs it, sends {"tasks": ["Charge", "Notify"]...} to the next step.
π Behind the Question: Advanced messaging patterns. β The Red Flags: Using this for simple static flows where Step Functions would be much easier to monitor.
QN: How do you manage "Long-running" Sagas that last days or weeks?β
Technical Answer: Use "Standard" Step Functions (not "Express"). Standard workflows can run for up to one year. Use the Wait state or Task.waitForTaskToken pattern. The Lambda sends a token to an external system and the workflow pauses until that system calls SendTaskSuccess with the token.
Code/Syntax Justification:
sfn.send_task_success(taskToken=token, output=json.dumps(res)).
π Behind the Question: Handling human-in-the-loop or slow async processes. β The Red Flags: Trying to keep a Lambda alive (max 15 mins) or polling a database in a loop for days.
QN: What is "Event Shifting" (or Event Transformation) in a Saga?β
Technical Answer: Each Lambda should consume an event in one schema and emit an event in another (Domain-Driven Design). Example: OrderCreated -> PaymentLambda -> PaymentProcessed. The PaymentLambda shouldn't just pass the OrderCreated event along; it should emit its own domain event.
Code/Syntax Justification:
Input: Order schema. Output: Payment schema. Use EventBridge Pipes to perform simple transformations without Lambda code where possible.
π Behind the Question: Clean architecture and decoupling. β The Red Flags: Passing the entire massive "God Object" event through every single service.
QN: How do you handle "Zombie" Sagas (Partially finished processes)?β
Technical Answer: Implement timeouts in Step Functions for every task and the total workflow. Use a "Dead Letter" approach where timed-out Sagas are moved to a manual review queue. Use CloudWatch Alarms on the ExecutionsFailed or ExecutionsTimedOut metrics.
Code/Syntax Justification:
"TimeoutSeconds": 300 on a Task state.
π Behind the Question: Operational resilience. β The Red Flags: Having no timeouts, leading to workflows that stay "Running" forever in the console.
QN: Explain "Parallel Fork/Join" in Lambda workflows.β
Technical Answer: In Step Functions, use the Parallel state to trigger multiple Lambdas simultaneously (e.g., ValidateCredit and CheckInventory). The workflow waits for ALL to finish (Join) before moving to the next state. If one fails, the entire Parallel state fails unless Catch is used.
Code/Syntax Justification:
"Type": "Parallel",
"Branches": [ { "StartAt": "TaskA", ... }, { "StartAt": "TaskB", ... } ]
π Behind the Question: Optimizing latency in complex workflows. β The Red Flags: Running tasks sequentially in code that could be run in parallel, increasing total execution time.
QN: What is the "Dynamic Map" state in Step Functions?β
Technical Answer: It allows you to run a set of steps for each item in an array (fan-out). Example: Processing 100 images in an S3 bucket. You can control the MaxConcurrency to avoid overwhelming downstream databases or hitting Lambda concurrency limits.
Code/Syntax Justification:
"Type": "Map", "ItemsPath": "$.items", "MaxConcurrency": 10.
π Behind the Question: Scalable data processing. β The Red Flags: Setting high concurrency on a Map state that hits a legacy RDS instance, causing it to crash.
QN: How do you implement "Service Discovery" for Lambda-to-Lambda calls in a Saga?β
Technical Answer: Avoid direct lambda.invoke(). Use EventBridge for async "Choreography" (Discovery is via the Bus). For sync calls, use API Gateway Private Endpoints or Cloud Map. Alternatively, use Step Functions where the "Discovery" is simply the ARN of the Lambda in the state machine definition.
Code/Syntax Justification: Hardcoding ARNs in code is an anti-pattern. Use environment variables or Step Functions JSON.
π Behind the Question: Decoupling and maintainability. β The Red Flags: Hardcoding Lambda ARNs in Python code across 20 different repos.
QN: How do you test a 10-step Lambda Saga?β
Technical Answer: 1. Unit tests for each Lambda. 2. Step Functions Local to test the state machine logic without hitting AWS. 3. Contract testing (e.g., Pact) to ensure the event emitted by Lambda A matches what Lambda B expects. 4. End-to-end integration in a "Sandbox" environment.
Code/Syntax Justification:
docker run amazon/aws-stepfunctions-local: Runs a local mock of SFN.
π Behind the Question: Quality assurance in complex distributed systems. β The Red Flags: "Testing in production" or only testing individual Lambdas in isolation.
QN: Explain the "Claim Check" pattern in event-driven Sagas.β
Technical Answer: If an event payload is too large for EventBridge (limit 256KB) or SQS, upload the data to S3. The event sent to the bus contains only the S3 bucket and key. The consumer Lambda uses the key to "claim" the data from S3.
Code/Syntax Justification:
Event: {"data_ref": "s3://my-bucket/orders/123.json"}.
π Behind the Question: Handling large payloads in serverless. β The Red Flags: Trying to compress massive JSON into a 256KB limit and failing when it grows.
QN: What is "Event Ordering" (FIFO) in Sagas?β
Technical Answer: SQS FIFO ensures messages are processed in the order they were sent within a MessageGroupId. This is critical for Sagas where OrderCreated must be processed before OrderCancelled. Step Functions handles ordering naturally as it moves through states.
Code/Syntax Justification:
QueueName.fifo + MessageGroupId.
π Behind the Question: Preventing race conditions in distributed state. β The Red Flags: Using standard SQS for order processing where a "Cancel" might arrive and be processed before the "Create."
QN: How do you monitor the "Health" of a complex Saga?β
Technical Answer: Use ServiceLens and X-Ray. They provide a visual map of the entire flow, showing latency and error rates at each hop. Use Step Functions Execution History to see exactly where a specific execution failed and what the input/output was at that step.
Code/Syntax Justification:
Enable Tracing: Active in Lambda and Step Functions.
π Behind the Question: Observability and MTTR (Mean Time To Recovery). β The Red Flags: Searching through raw CloudWatch log groups for hours to find one failed order.
QN: What is the "Fan-In" pattern for Lambda?β
Technical Answer: Multiple event sources (S3, SQS, API) all trigger the same Lambda function. This is useful for centralizing logic like "Log Ingestion" or "Data Transformation." The Lambda must inspect the event object to determine the source type and parse it accordingly.
Code/Syntax Justification:
if 'Records' in event and 's3' in event['Records'][0]:
# Handle S3
elif 'httpMethod' in event:
# Handle API Gateway
π Behind the Question: Code reuse vs single responsibility. β The Red Flags: Writing a 2000-line "Mega-Lambda" that handles 50 different event types, making it impossible to test.
QN: How do you handle "Versioning" in an event-driven Saga?β
Technical Answer: Use "Expand and Contract." Consumers must handle both v1 and v2 event schemas simultaneously during a migration. Alternatively, use EventBridge Content-Based Filtering to route v1 events to old Lambdas and v2 to new ones.
Code/Syntax Justification:
// EventBridge Rule for v2 only
{ "detail": { "version": ["2.0"] } }
π Behind the Question: Zero-downtime deployments and schema evolution. β The Red Flags: Breaking the event schema and deploying the new producer, causing all existing consumers to crash.
QN: Explain "External Signal" in Step Functions Sagas.β
Technical Answer: A workflow can wait for an external action (e.g., an admin clicking "Approve" in a UI). The UI calls the SendTaskSuccess API with a taskToken. This is more efficient than the Lambda polling a database for the status.
Code/Syntax Justification:
"Resource": "arn:aws:states:::lambda:invoke.waitForTaskToken".
π Behind the Question: Efficient human-workflow integration.
β The Red Flags: Using time.sleep() inside a Lambda to wait for a user.
Section 44: Lambda Production Incident Response & Debuggingβ
QN: How do you debug a "Timed Out" Lambda in production?β
Technical Answer: 1. Check if the timeout is because of a downstream dependency (RDS, API). 2. Check X-Ray for "Segment" duration. 3. Check for "Recursive Loops" (e.g., S3 -> Lambda -> S3). 4. Temporarily increase memory; Lambda scales CPU linearly with memory, so more memory might fix a CPU-bound timeout.
Code/Syntax Justification:
Increase Timeout to 15m and Memory to 10GB for a one-off trace if needed.
π Behind the Question: Practical troubleshooting skills. β The Red Flags: Assuming the code is "slow" without checking if it's actually stuck waiting for a socket.
QN: What do you do when you see "ConcurrentInvocationLimitExceeded"?β
Technical Answer: This is a 429 Throttle. 1. Identify which function is "hogging" the account limit (1000 by default). 2. Set Reserved Concurrency on critical functions to guarantee they have capacity. 3. Request a limit increase via AWS Support. 4. Implement exponential backoff in the caller.
Code/Syntax Justification:
aws lambda put-function-concurrency --function-name MyFunc --reserved-concurrent-executions 100.
π Behind the Question: Managing account-wide resource limits. β The Red Flags: Thinking a 429 only happens if the individual function is busy (it can be any function in the account).
QN: How do you find the "Request ID" for a failed Lambda call?β
Technical Answer: If the call was from APIGW, the Request ID is in the X-Amzn-RequestId header. In CloudWatch Logs, every log line is prefixed with the Request ID. You can use CloudWatch Logs Insights to find the ID based on an error message or timestamp.
Code/Syntax Justification:
fields @timestamp, @message, @requestId | filter @message like /Error/ | sort @timestamp desc.
π Behind the Question: Correlating logs across services. β The Red Flags: Manually clicking through 50 log streams to find one error.
QN: Explain "Log Tailing" for Lambda.β
Technical Answer: Using the AWS CLI aws logs tail or tools like sam logs --tail. It allows you to see logs in real-time in your terminal as the Lambda executes. This is the fastest way to debug "Dev" or "Staging" environments.
Code/Syntax Justification:
aws logs tail /aws/lambda/my-function --follow.
π Behind the Question: Developer productivity tools. β The Red Flags: Constantly refreshing the AWS Console Log Group page.
QN: How do you debug "Permission Denied" errors when the IAM policy looks correct?β
Technical Answer: 1. Check for Service Control Policies (SCPs) at the Org level. 2. Check for Permissions Boundaries on the Lambda Role. 3. Check for Resource-based policies on the target (e.g., an S3 bucket or KMS key). 4. Use IAM Policy Simulator to test the specific action.
Code/Syntax Justification:
Verify the Action, Resource, AND Condition blocks. Often, a condition like StringEquals: { "aws:PrincipalOrgID": "..." } is the culprit.
π Behind the Question: Deep understanding of the IAM evaluation logic.
β The Red Flags: Blindly adding AdministratorAccess to "fix" it.
QN: What is the "Cold Start" metric in CloudWatch?β
Technical Answer: It's not a direct metric. You find it by looking at the REPORT line in logs: Init Duration: 250ms. In X-Ray, it shows as an Initialization subsegment. You can also use a custom metric by parsing logs to track Cold Start frequency vs total invocations.
Code/Syntax Justification:
@initDuration field in Logs Insights.
π Behind the Question: Performance monitoring. β The Red Flags: Thinking every "Invocations" metric hit includes a cold start.
QN: How do you handle a "Poison Pill" message in an SQS-Lambda trigger?β
Technical Answer: A poison pill is a message that causes the Lambda to crash or timeout every time it's retried, blocking the queue. Solution: Configure a Redrive Policy with a Dead Letter Queue (DLQ). After maxReceiveCount (e.g., 3) failures, SQS moves the message to the DLQ automatically.
Code/Syntax Justification:
RedrivePolicy: { "deadLetterTargetArn": "...", "maxReceiveCount": 3 }.
π Behind the Question: Queue management and fault tolerance.
β The Red Flags: Setting maxReceiveCount to 100, causing a loop that wastes money and logs.
QN: How do you debug VPC-related Lambda failures (e.g., Task timed out after 3.01 seconds)?β
Technical Answer: Usually, this is a networking timeout. Check: 1. Does the Lambda need internet access? If so, it must be in a private subnet with a route to a NAT Gateway. 2. Is the Security Group allowing outbound traffic to the destination (e.g., port 5432 for RDS)? 3. Are the NACLs blocking traffic?
Code/Syntax Justification: Lambda in Public Subnet + No NAT = No Internet. This is the #1 VPC-Lambda error.
π Behind the Question: VPC networking fundamentals. β The Red Flags: Putting a Lambda in a public subnet and expecting it to have an Elastic IP (it doesn't).
QN: How do you use "CloudWatch Logs Insights" to find the most expensive Lambdas?β
Technical Answer: Query the REPORT lines, sum the Billed Duration, and multiply by the memory tier price.
Code/Syntax Justification:
stats sum(@billedDuration) as TotalBilledDuration by @log
| sort TotalBilledDuration desc
π Behind the Question: Cost management and operational intelligence. β The Red Flags: Only looking at invocation counts, ignoring that one 15-minute Lambda costs more than 10,000 100ms Lambdas.
QN: What is the "Zombie Execution" problem in Lambda?β
Technical Answer: When a Lambda times out, the underlying execution environment (container) might be reused. If there were un-awaited async tasks or open file handles, they might "wake up" in the next invocation, causing unpredictable behavior. Fix: Ensure all promises are awaited and cleanup happens in finally blocks.
Code/Syntax Justification: In Python, avoid starting background threads that aren't joined before the handler returns.
π Behind the Question: Understanding the Lambda runtime lifecycle.
β The Red Flags: Using threading.Thread without joining, leading to "ghost" logs in subsequent requests.
QN: How do you debug "Lambda function not found" during a deployment?β
Technical Answer: This often happens in CloudFormation/CDK when a trigger (like an EventBridge rule) is created before the Lambda function exists or in a different region. Check the DependsOn attribute in your template to ensure the function is created first.
Code/Syntax Justification:
DependsOn: MyLambdaFunction in the CloudFormation Resource block.
π Behind the Question: Infrastructure as Code (IaC) dependencies. β The Red Flags: Manually creating resources to "fix" a broken CloudFormation stack.
QN: How do you perform "Live Debugging" of a Python Lambda?β
Technical Answer: AWS does not support a live debugger (like PDB) in the actual production environment. You must use local emulation with sam local invoke or python-lambda-local. To debug production, you rely on "Log-based debugging" or "Tracing" (X-Ray).
Code/Syntax Justification:
sam local invoke -d 5858: Connects a debugger to a local container.
π Behind the Question: Understanding the limits of serverless debugging.
β The Red Flags: Claiming you can ssh into a Lambda to run gdb.
QN: How do you detect "Memory Leaks" in Lambda?β
Technical Answer: Monitor the Max Memory Used in CloudWatch Logs Insights over time for a single "warm" container (filter by @logStream). If the usage grows with every invocation until it hits the limit, you have a leak.
Code/Syntax Justification:
stats max(@maxMemoryUsed) by bin(30m) β look for an upward slope.
π Behind the Question: Long-term stability of serverless apps. β The Red Flags: Thinking a memory leak doesn't matter because "Lambda is ephemeral" (containers can stay warm for hours).
QN: Explain "Runtime.ImportModuleError" in production.β
Technical Answer: The Lambda failed to find your code or a dependency. Causes: 1. Incorrect Handler configuration (e.g., app.handler but file is named main.py). 2. Dependencies not included in the ZIP/Layer. 3. Incorrect permissions on the ZIP file (must be world-readable).
Code/Syntax Justification:
Check ls -l on your ZIP contents. All files should be 755 or 644.
π Behind the Question: Packaging and deployment issues.
β The Red Flags: Assuming the AWS environment has all Python libraries pre-installed (it only has boto3 and botocore).
QN: What is "Binary Support" in Lambda + API Gateway?β
Technical Answer: To handle images/PDFs, APIGW must be configured with "Binary Media Types" (e.g., image/png). The Lambda must return a base64 encoded string and set isBase64Encoded: true in the response object.
Code/Syntax Justification:
return {
"isBase64Encoded": True,
"statusCode": 200,
"headers": { "Content-Type": "image/png" },
"body": base64.b64encode(image_data).decode('utf-8')
}
π Behind the Question: Handling non-JSON data.
β The Red Flags: Sending raw binary in the body field, which results in a corrupted file or APIGW error.
QN: How do you debug "Large Payload" errors (413 Request Entity Too Large)?β
Technical Answer: APIGW has a 10MB limit. Lambda has a 6MB limit. If you exceed this, you must use the S3 Pre-signed URL pattern. The client uploads to S3, then sends the S3 key to the Lambda.
Code/Syntax Justification:
s3.generate_presigned_url('put_object', ...)
π Behind the Question: Architectural limits of serverless. β The Red Flags: Trying to increase the 6MB Lambda limit (it's a hard limit).
QN: How do you audit "Who invoked this Lambda?"β
Technical Answer: Use AWS CloudTrail. Look for Invoke (for sync) or CreateEventSourceMapping (for async). If it's another AWS service, CloudTrail will show the userAgent and principalId.
Code/Syntax Justification:
Search CloudTrail events for EventSource: lambda.amazonaws.com.
π Behind the Question: Security and auditing. β The Red Flags: Thinking CloudWatch logs show the IAM caller's identity (they don't, unless you log it manually).
QN: What is "Execution Environment Reuse" and how can it cause bugs?β
Technical Answer: AWS keeps the Lambda container alive for subsequent requests. Global variables are preserved. Bug: If you initialize a database connection globally but don't handle its closure/timeout, you might get "Broken Pipe" errors. Or, if you append to a global list, it will grow forever.
Code/Syntax Justification:
db_conn = None # Global
def handler(event, context):
global db_conn
if not db_conn: db_conn = create_conn()
π Behind the Question: Advanced state management. β The Red Flags: Using global variables to store user-specific data (huge security risk and data leak).
QN: How do you troubleshoot "Slow" Lambda initialization?β
Technical Answer: Initialization happens "Outside the handler." Causes: 1. Importing massive libraries (e.g., pandas, tensorflow) unnecessarily. 2. Heavy logic in the global scope (e.g., fetching 10 secrets from Secrets Manager). Solution: Use Lazy Loading inside the handler or use Lambda Layers for large libraries.
Code/Syntax Justification:
Move import pandas inside the function if it's only used in one rare path.
π Behind the Question: Cold start optimization. β The Red Flags: Doing heavy I/O or network calls in the global scope without caching.
QN: How do you handle "Recursive Loops" (e.g., Lambda writes to S3, which triggers the same Lambda)?β
Technical Answer: 1. Immediate Detection: AWS has a "Recursive Loop Detection" for SQS and Lambda that will stop the function and alert you. 2. Prevention: Use different prefixes/suffixes for input vs output (e.g., uploads/ vs processed/). 3. Condition check: The Lambda should check metadata to see if it has already processed the file.
Code/Syntax Justification:
if event['object']['key'].startswith('processed/'): return
π Behind the Question: Avoiding infinite loops and massive bills. β The Red Flags: Having no exit condition, leading to a "bill shock" of thousands of dollars in minutes.
This document covers 310 comprehensive Q&As on AWS Lambda and serverless Python, spanning multi-account/region patterns, event-driven Sagas, production incident response, security hardening, error handling, resilience, architecture decisions, observability, multi-tenancy, testing, cost optimization, performance engineering, and production deployment patterns.