CtrlK
BlogDocsLog inGet started
Tessl Logo

aws-lambda-best-practices

AWS Lambda best practices for function design, configuration, and operations. Use when designing Lambda functions, choosing memory/timeout settings, planning concurrency and scaling, setting up monitoring, processing streams, or securing Lambda workloads. Triggers on tasks involving Lambda function creation, handler design, cold start optimization, idempotency, event source mapping, reserved/provisioned concurrency, or deciding whether Lambda is the right compute choice. Does not cover language-specific SDK patterns, CloudFormation/Terraform resource definitions, or Lambda@Edge.

72

Quality

90%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

Passed

No findings from the security scan

SKILL.md
Quality
Evals
Security

AWS Lambda Best Practices

Opinionated, language-agnostic conventions for designing and operating Lambda functions at scale. For SDK/API usage, infrastructure-as-code definitions, or language-specific handler patterns, consult the relevant AWS SDK or Terraform documentation instead.

Is Lambda the Right Choice?

Lambda excels at event-driven, short-lived, stateless workloads. Evaluate fit before committing.

RequirementLambda fitBetter alternative
Event-driven, bursty trafficExcellent--
Execution < 15 minutesGoodECS/Fargate for longer tasks
Stateless request/responseExcellent--
Persistent connections (WebSockets)PoorAPI Gateway WebSocket + ECS
Predictable, steady-state high throughputEvaluate costECS/Fargate or EC2
GPU or specialized hardwareNot supportedEC2 or SageMaker
Large deployment artifact (> 10 GB)Container image limitECS/Fargate
Sub-millisecond latencyNot achievable -- invocation overhead is milliseconds even on warm startsEC2 or containers

Function Design

One function per responsibility. Scope by event source or business operation. Keep the handler thin: validate input, delegate to business logic (pure functions), return a structured response. Initialize SDK clients and connections at module level -- Lambda reuses the execution environment across invocations.

Don't: build a Lambda monolith (all routes in one function), inline business logic in the handler, or create connections inside the handler.

Idempotency is mandatory. Lambda guarantees at-least-once invocation -- your function will receive duplicates. Use an idempotency key (request ID, message ID) with DynamoDB or Powertools to prevent reprocessing.

Don't: write orchestration logic in Lambda. If your function has more if/else/retry than business logic, move the workflow to Step Functions. Never have Lambda synchronously invoke another Lambda -- use SQS or Step Functions to decouple.

Don't: write to the same resource that triggered the function -- this causes recursive invocation loops with exponential cost. If detected, set reserved concurrency to 0 immediately.

See references/function-design.md for execution environment reuse details, cold start factors, idempotency implementation, connection management, and environment variables. See references/anti-patterns.md for why each anti-pattern feels right but isn't, impact analysis, and migration strategies.

Function Configuration

Memory and CPU -- they are linked

Lambda allocates CPU proportional to memory. At 1,769 MB, a function gets one full vCPU.

MemoryCPUBest for
128-512 MBFractional vCPUSimple transforms, routing
512-1,769 MBUp to 1 vCPUAPI handlers, moderate processing
1,769-3,008 MB1-2 vCPUData processing, image manipulation
3,008-10,240 MB2-6 vCPUML inference, heavy computation

Use AWS Lambda Power Tuning to find the optimal price/performance balance.

Architecture -- default to arm64

arm64 (Graviton) costs 20% less per GB-second than x86_64 with equal or better performance. Use it unless a native dependency, layer, or container base image requires x86_64.

Timeout -- set deliberately, not defensively

Set to p99 duration + 20-50% buffer. For SQS triggers, set SQS Visibility Timeout >= 6x function timeout. API Gateway's integration timeout is 29 seconds by default (raisable above that only for Regional/private REST APIs via quota increase).

Key quotas

ResourceLimitNotes
Max execution time15 minutesHard limit
Payload (sync)6 MB request / 6 MB response200 MB for streamed responses
Payload (async)1 MB--
Deployment package (.zip)50 MB zipped / 250 MB unzippedUse S3 for larger uploads
Container image10 GB--
/tmp storage512 MB - 10,240 MBConfigurable
Environment variables4 KB totalAggregate across all variables
Concurrent executions1,000 defaultSoft limit, increase via Service Quotas
Layers5 per function--

See references/function-configuration.md for memory/CPU tuning strategies, architecture selection, IAM policy guidance, SQS integration, and quota management.

Function Scalability

Concurrency model

Concurrency = (requests per second) x (average duration in seconds)

100 RPS with 200ms average duration = 20 concurrent executions.

Choosing a concurrency control

ScenarioControlHow to size
Most functions, no special needsUnreserved (default)Shares account pool (1,000 default)
Critical function that must always have capacityReserved concurrencyPeak concurrent executions x 1.3
Protect downstream from overload (DB, API)Reserved concurrencyMatch downstream's safe throughput
User-facing API with latency SLAProvisioned concurrencyEliminates cold starts; use auto-scaling
Emergency stop for runaway functionReserved concurrency = 0Halts all invocations immediately

Lambda scales automatically -- your dependencies may not. Use RDS Proxy for relational databases, reserved concurrency to cap scaling to third-party rate limits, and on-demand mode for DynamoDB under Lambda workloads.

Don't: let Lambda overwhelm your database. A traffic spike -> 1,000 concurrent functions -> 1,000 simultaneous DB connections -> connection exhaustion -> all functions timeout -> cascade failure. This is the most common Lambda production incident.

See references/function-scalability.md for scaling rate, provisioned concurrency scheduling, throttle tolerance patterns, upstream/downstream protection, and cascade failure prevention.

Metrics and Alarms

Prefer Embedded Metric Format (EMF) over PutMetricData API calls for custom metrics -- zero latency overhead. Use Powertools for AWS Lambda to handle EMF formatting. Use PutMetricData only when you need high-resolution (1-second) metrics or immediate availability.

Key metrics to alarm on

MetricAlarm conditionWhat it catches
Errors> 0 for N minutesFunction failures
Throttles> 0Hitting concurrency limits
Durationp99 > thresholdLatency degradation
ConcurrentExecutions> 80% of reservedApproaching ceiling
IteratorAge (streams)> 30,000 msFalling behind on stream processing
DeadLetterErrors> 0DLQ delivery failures

Use structured JSON logging, correlation IDs across services, X-Ray tracing, and Cost Anomaly Detection.

See references/metrics-and-alarms.md for EMF details, structured logging guidance, alarm configuration, and cost anomaly detection setup.

Working with Streams

Batch tuning is the critical lever -- larger batches amortize overhead, batching windows buffer small batches. Always enable ReportBatchItemFailures in production to retry only failed records instead of the entire batch. Every stream-processing function must be idempotent -- at-least-once delivery is guaranteed.

Stream typeScaling leverConcurrency model
KinesisAdd shards1 invocation per shard (default); up to 10 with parallelization factor
DynamoDB StreamsAdd partitions (indirect)1 invocation per shard
SQSAutomaticLambda scales pollers up to concurrency limit

See references/stream-events.md for batch tuning parameters, partial batch response implementation, Kinesis shard management, IteratorAge monitoring, and SQS FIFO considerations.

Security

IAM -- least privilege, always. One narrowly scoped execution role per function. No wildcards in production. Use resource-based policies to control who can invoke your function.

PracticeWhy
Code signingVerify deployment artifact integrity
VPC placementRequired for private resources; minor cold start impact (Hyperplane ENIs)
Security Hub controlsAutomated CSPM checks against Lambda configurations
GuardDuty Lambda ProtectionMonitors for threats (crypto mining, C2 communication)
Secrets Manager / Parameter StoreNever hardcode secrets

See references/security.md for VPC configuration trade-offs, code signing setup, secrets management patterns, data protection, and governance strategies.

Common Mistakes

MistakeFix
Initializing SDK clients inside the handlerMove to module/global scope for execution environment reuse
Setting timeout to 15 minutes "just in case"Set to p99 duration + reasonable buffer; load test
Ignoring cold starts for user-facing APIsUse provisioned concurrency, SnapStart (Java/Python/.NET), or optimize package size
Not designing for idempotencyUse idempotency keys with DynamoDB or Powertools
Recursive invocations (function triggers itself)Set reserved concurrency to 0 immediately; fix the trigger/output separation
Hardcoding resource namesUse environment variables for bucket names, table names, endpoints
Wildcard IAM permissionsGrant specific actions on specific resources
Not monitoring IteratorAge on streamsAlarm at 30s; add shards or increase parallelization
Retrying the full batch on partial failureEnable ReportBatchItemFailures
Over-provisioning memory without testingUse Lambda Power Tuning to find optimal setting

Reference Files

  • references/anti-patterns.md -- Lambda monolith, Lambda as orchestrator, synchronous chains, recursive loops, synchronous waiting -- why each feels right, impact analysis, migration strategies
  • references/function-design.md -- Execution environment reuse, cold start factors, idempotency implementation, connection management, RDS Proxy, environment variables
  • references/function-configuration.md -- Memory/CPU tuning strategies, arm64/Graviton selection, timeout alignment, quotas, IAM policies, SQS integration, cleanup
  • references/function-scalability.md -- Concurrency estimation, reserved vs provisioned concurrency, scaling rate, upstream/downstream protection, cascade failure prevention
  • references/metrics-and-alarms.md -- CloudWatch metrics, EMF implementation, structured logging, alarm configuration, Cost Anomaly Detection, X-Ray tracing
  • references/stream-events.md -- Batch tuning, partial batch response, Kinesis/DynamoDB Streams/SQS scaling, IteratorAge monitoring, SQS FIFO high-throughput mode
  • references/security.md -- IAM least privilege, code signing, VPC trade-offs, secrets management, data protection, Security Hub, GuardDuty, governance
Repository
provectus/awos-recruitment
Last updated
First committed

Is this your skill?

If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.