监控与调试 Lambda 函数
学习如何在生产环境中使用 CloudWatch、X-Ray 和结构化日志观测、记录、跟踪和排查 AWS Lambda 函数。
监控与调试 Lambda 函数 是 CoddyKit 上的免费 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AWS for Backend Developers (EC2, S3, RDS, Lambda) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Observability Matters
Serverless functions are short-lived and invisible — you cannot SSH into them. Observability is how you understand what your Lambda is doing.
The three pillars are logs, metrics, and traces.
Logging with CloudWatch
Anything your function writes to stdout/stderr goes to CloudWatch Logs automatically. Each function gets its own log group.
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event));
return { statusCode: 200, body: 'OK' };
};Structured Logging
Plain text logs are hard to query. Log JSON objects so you can filter on fields later.
- Include a requestId
- Include severity and context
console.log(JSON.stringify({
level: 'INFO',
requestId: context.awsRequestId,
message: 'Order processed',
orderId: 42
}));Built-in Lambda Metrics
Lambda publishes metrics to CloudWatch out of the box:
- Invocations — how often it ran
- Errors — failed executions
- Duration — execution time
- Throttles — rejected due to concurrency limits
Setting Alarms on Errors
Create a CloudWatch alarm so you get notified when error rates spike, instead of finding out from angry users.
aws cloudwatch put-metric-alarm \
--alarm-name lambda-errors \
--metric-name Errors \
--namespace AWS/Lambda \
--threshold 1 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1Distributed Tracing with X-Ray
AWS X-Ray traces a request as it flows through Lambda, DynamoDB, S3, and other services. It reveals where time is spent and which downstream call is slow.
Enable Active tracing in the function configuration.
Cold Starts
A cold start happens when Lambda spins up a fresh execution environment. It adds latency to the first request.
Watch Init Duration in your logs to measure cold start impact.
Reducing Cold Starts
Ways to reduce cold start pain:
- Use Provisioned Concurrency to keep environments warm
- Keep deployment packages small
- Avoid heavy initialization at module load
Handling Errors Gracefully
Wrap risky code in try/catch and return meaningful errors. Unhandled exceptions count as Lambda errors and may trigger retries.
exports.handler = async (event) => {
try {
return await process(event);
} catch (err) {
console.error('Processing failed', err);
throw err;
}
};Dead Letter Queues
For asynchronous invocations that keep failing, configure a Dead Letter Queue (DLQ) using SQS or SNS. Failed events land there so you can inspect and reprocess them.
Putting It Together
A well-monitored Lambda has:
- Structured JSON logs
- CloudWatch alarms on Errors and Duration
- X-Ray tracing enabled
- A DLQ for failed async events
Quick Check
Test your debugging knowledge.
Recap
You learned to monitor and debug Lambda:
- CloudWatch Logs capture stdout/stderr
- Metrics and alarms alert on errors
- X-Ray traces distributed calls
- DLQs capture failed async events
Good observability turns invisible serverless failures into solvable problems.
常见问题解答
「监控与调试 Lambda 函数」课时是免费的吗?
是的 — 「监控与调试 Lambda 函数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程的其余内容,请升级到 CoddyKit PRO。 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程共包含 4 节课。
「监控与调试 Lambda 函数」这节课中我会学到什么?
学习如何在生产环境中使用 CloudWatch、X-Ray 和结构化日志观测、记录、跟踪和排查 AWS Lambda 函数。 你通过在浏览器中直接运行的动手代码来练习 AWS for Backend Developers (EC2, S3, RDS, Lambda),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AWS for Backend Developers (EC2, S3, RDS, Lambda) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「监控与调试 Lambda 函数」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课中编写并运行代码吗?
能。每节 AWS for Backend Developers (EC2, S3, RDS, Lambda) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 什么是 AWS Lambda?
- 构建您的第一个 Lambda 函数
- Lambda 触发器与集成
- 监控与调试 Lambda 函数