Serverless Best Practices
A condensed summary of the 25 most important serverless practices for Node.js teams on AWS Lambda - drawn from every page in this section.
How to Use This List
- Add CloudWatch alarm on
Durationp99 andErrorsrate per function alias - Load test with intentional cold starts before committing to Lambda for an API
- Pair with Runtime Ops Best Practices when splitting Lambda vs ECS
-
Stateless handlers: No in-memory session state across invocations - Serverless Basics.
-
Externalize state: DynamoDB, RDS, ElastiCache, S3 - not global variables for user data.
-
Init-phase client reuse: DynamoDB/S3/SSM clients in module scope - @aws-sdk v3.
-
Parse env with Zod at init: Fail fast on misconfiguration before taking traffic.
-
Async handlers only: No callback-style handlers in new TypeScript code - Lambda Handler Patterns.
-
callbackWaitsForEmptyEventLoop = false: When using DB pools or HTTP keep-alive agents. -
Return 4xx, do not throw: Validation errors should not retry async invocations.
-
Idempotent SQS consumers: Dedupe with DynamoDB conditional writes on
messageIdor business key. -
SQS partial batch failures: Report
batchItemFailuresfor poison messages only. -
Use SDK v3 modular imports: Never add
aws-sdkv2 to new projects. -
esbuild bundle application code: Minify + tree shake - Cold Start Mitigation.
-
Target zip under 5-15 MB: Investigate bloat with visualizer.
-
nodejs24.xruntime: Match local Node 24 LTS. -
Prefer arm64 (Graviton): Benchmark native deps in CI matrix.
-
Right-size memory: More memory grants more CPU; speeds init and execution.
-
Timeout from p99 + margin: Include downstream calls; avoid hard kills mid-write.
-
VPC only when required: Accept ENI cold start cost or use RDS Proxy / Data API.
-
Provisioned concurrency on aliases: Not on
$LATEST; cost-control with autoscaling policies. -
Structured JSON logs: Include
awsRequestIdand business correlation ids. -
No PII in logs: Redact bodies; log ids only.
-
Secrets from SSM/Secrets Manager: Cached at init; never in env in console screenshots.
-
Framework adapters for migration only: Plan slim handlers for hot paths - serverless-express / aws-lambda-fastify.
-
IaC for every function: SAM, CDK, or Terraform - no click-ops drift.
-
Separate functions per trigger type: HTTP vs SQS vs schedule - do not multiplex unrelated events.
-
Revisit Lambda vs ECS quarterly: Steady traffic may be cheaper on Fargate - Platform Deploy Basics.
FAQs
What is the single highest-impact serverless practice?
Reuse AWS SDK clients and parsed configuration in module scope (init phase). It cuts per-invocation overhead on every warm call.
When should we leave Lambda?
When p99 latency, WebSockets, execution time over 15 minutes, or baseline cost exceeds always-on containers at your traffic level.
One Lambda or many?
Many small functions when bundles, timeouts, or IAM permissions differ. One function with internal router only for small APIs and prototypes.
How do we test?
Unit test handlers with fixture events. Integration test with SAM local or deploy to a dev stack. Do not rely only on console test button.
Does Lambda replace CI/CD?
No. Same quality gates and artifact promotion as containers - CI/CD Best Practices.
Related
- Serverless Basics - first Lambda
- Lambda Handler Patterns - typed handlers
- Cold Start Mitigation - latency tuning
- ECS Fargate & Cloud Run - container alternative
- Docker Best Practices - when using Lambda container images
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.