6 views
This guide focuses on Lambda Log Buffering for production systems, with practical trade-offs for reliability, security, and cost.
CloudWatch Logs charges for every ingested GB. Debug spam at INFO on hot Lambdas is a silent bill — until you sample blindly and lose the one correlation ID that would have closed the sev. Buffer, sample by level, and always keep correlation + error paths loud.
⚡ TL;DR: Use Powertools Logger with buffer-on-INFO / flush-on-ERROR; sample successful requests; never dropcorrelation_id/cold_start/ error stacks. Pair with Lambda Powertools Node Structured Logs and OpenTelemetry for LLMs.
Lambda Log Buffering: production guidance
typescript
// handler.ts
import { Logger } from "@aws-lambda-powertools/logger";
import { injectLambdaContext } from "@aws-lambda-powertools/logger/middleware";
import middy from "@middy/core";
const logger = new Logger({
serviceName: "checkout",
logLevel: "INFO",
// Powertools buffer: hold debug/info until error or explicit flush
});
export const handler = middy(async (event, ctx) => {
logger.appendKeys({
correlationId: event.headers?.["x-correlation-id"] ?? ctx.awsRequestId,
tenantId: event.requestContext?.authorizer?.tenantId,
});
logger.info("start", { path: event.rawPath });
try {
const result = await checkout(event);
// ✅ Sample successes: 1% INFO after success
if (Math.random() < 0.01) logger.info("success_sample", { resultCode: result.code });
return result;
} catch (err) {
// ✅ Errors always flush full buffer + stack
logger.error("checkout_failed", { err });
throw err;
}
}).use(injectLambdaContext(logger, { clearState: true }));
Sampling policy that on-call accepts
| Level | Success path | Error / timeout / DLQ |
|---|---|---|
| DEBUG | off in prod | flush last N buffered |
| INFO | 1% sample | 100% |
| WARN | 100% | 100% |
| ERROR | 100% | 100% + stack |
typescript
❌ Sampling ERROR to “save money.” ✅ Cutting DEBUG/INFO volume while preserving every failed invoke’s correlation trail — same discipline as Node.js Event Loop Lag observability.
// lib/sample.ts
export function shouldLogSuccess(sampleRate = 0.01) {
return Math.random() < sampleRate;
}
EMF metrics beat log-counting
typescript
Prefer Embedded Metric Format for counts/latency; reserve logs for narrative context. That combination usually cuts ingestion more than aggressive sampling alone.
import { Metrics, MetricUnit } from "@aws-lambda-powertools/metrics";
const metrics = new Metrics({ namespace: "Checkout", serviceName: "checkout" });
metrics.addMetric("CheckoutOk", MetricUnit.Count, 1);
metrics.publishStoredMetrics();
Closing checklist
✅ Dos – ✅ Powertools Logger + correlation keys on every line – ✅ Buffer INFO; flush on ERROR/timeout – ✅ Sample successful INFO; never sample ERROR – ✅ EMF for cardinality-safe counters – ✅ Alarm on ingestion GB/day per log group ❌ Don’ts – ❌ Don’tconsole.log unstructured strings in hot paths
– ❌ Don’t strip awsRequestId / tenant IDs when sampling
– ❌ Don’t keep DEBUG on in prod “just in case”
– ❌ Don’t create per-invoke log streams manually
Related reading
- Lambda Powertools Node: Structured Logs
- OpenTelemetry for LLMs
- Node.js Event Loop Lag p99
- Lambda Cold Starts on Node 20
Watch: 100 Days of AI on YouTube
Short videos from CheatCoders, one AI engineering topic per day.
Most viewed
- Python Decorators Explained: From Simple Wrappers to Production Patterns
- Claude Code vs Cursor vs GitHub Copilot: Choose by Workflow, and Set Up One Repo That Works With All Three
- AI Agent Frameworks in 2025: LangGraph vs CrewAI vs AutoGen vs Raw API
- Python String Methods: Every str Method With Real Production Examples
- Css Animation Performance: Performance-Optimized Motion for Modern UIs
Newly added
- AWS Systems Manager Parameter Store: Hierarchical Runtime Config for Coding Agents
- Amazon Cognito Identity Pools: Federate Coding-Agent Tool Callers Without Long-Lived Keys
- Amazon Athena for Coding Agents: Query Tool Audit Logs and Eval Traces on S3 Without a Warehouse
- AWS Batch for Coding Agents: Parallel Overnight PR Reviews and Long-Running Jobs Without Lambda Timeouts
- Amazon Bedrock Prompt Management: Version and A/B Test Coding-Agent System Prompts
Deep-dive PDF
Get the expanded guide for this post — extra diagrams-style checklists, failure modes, and a production walkthrough. Free when you subscribe to CheatCoders.
Already subscribed? or open the subscribe page.
Discover more from CheatCoders
Subscribe to get the latest posts sent to your email.
