Structured Logging and Correlation IDs
Learn to produce searchable, structured JSON logs and to trace a request across multiple functions using correlation IDs.
Structured Logging and Correlation IDs is a free Serverless AWS Lambda Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Serverless AWS Lambda Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Plain Logs Do Not Scale
Free-text log lines are hard to filter at scale. Structured logging writes each entry as JSON with consistent fields you can query.
Anatomy of a Structured Log
A good log entry has a level, a message, and contextual fields like request id and user id.
{
"level": "INFO",
"message": "order placed",
"orderId": 42,
"requestId": "abc-123"
}Emitting JSON Logs
Serialize a dictionary to JSON and print it. CloudWatch Logs Insights can then query individual fields.
import json, sys
def log(level, message, **fields):
entry = {'level': level, 'message': message}
entry.update(fields)
print(json.dumps(entry), file=sys.stdout)
log('INFO', 'order placed', orderId=42)Querying with Logs Insights
Logs Insights lets you filter and aggregate JSON fields with a query language.
fields @timestamp, orderId
| filter level = 'ERROR'
| sort @timestamp desc
| limit 20The Correlation ID
A correlation ID is a unique value attached to a request and passed to every downstream service, so you can trace one request end to end.
Generating One
Create the ID at the entry point if the incoming request does not already carry one.
import uuid
def get_correlation_id(event):
headers = event.get('headers') or {}
return headers.get('x-correlation-id') or str(uuid.uuid4())Propagating It
Pass the correlation ID forward in message attributes, HTTP headers, or event payloads so the next function logs the same ID.
sns.publish(
TopicArn=topic,
Message=body,
MessageAttributes={
'correlationId': {'DataType': 'String', 'StringValue': cid}
}
)Log Levels
Use levels (DEBUG, INFO, WARN, ERROR) and make the threshold configurable via an environment variable so production stays quiet but debuggable.
Never Log Secrets
Structured logs are searchable, which makes accidental secret logging dangerous. Redact tokens, passwords, and PII before logging.
Control Log Retention
Logs cost money to store. Set a retention period on each log group so old logs expire automatically.
aws logs put-retention-policy \
--log-group-name /aws/lambda/orders \
--retention-in-days 30Tie Logs to Traces
Include the X-Ray trace id in your structured logs so you can jump from a log line straight to the distributed trace for that request.
Quick Check
Test your logging knowledge.
Recap
You learned structured JSON logging, querying with Logs Insights, threading correlation IDs across services, redacting secrets, and setting retention.
Frequently asked questions
Is the “Structured Logging and Correlation IDs” lesson free?
Yes — the full text of “Structured Logging and Correlation IDs” is free to read here on the web, and the Serverless AWS Lambda Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Serverless AWS Lambda Development course, upgrade to CoddyKit PRO.
What will I learn in “Structured Logging and Correlation IDs”?
Learn to produce searchable, structured JSON logs and to trace a request across multiple functions using correlation IDs. You practise Serverless AWS Lambda Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Serverless AWS Lambda Development?
No prior experience is required. Serverless AWS Lambda Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Structured Logging and Correlation IDs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Serverless AWS Lambda Development lesson?
Yes. Every Serverless AWS Lambda Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Advanced IAM Policies and Permissions
- Secrets Management with AWS Secrets Manager
- Distributed Tracing with AWS X-Ray
- Structured Logging and Correlation IDs