You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Lambda服务配置X-Ray?Node.js函数X-Ray无数据排查

Answer

Yes, even though Node.js Lambda functions support X-Ray auto-instrumentation, there are a few key steps you need to complete to get traces showing up in the X-Ray console. Let's walk through each one clearly:

1. Enable X-Ray Tracing for Your Lambda Functions

First, you need to explicitly turn on active tracing for your functions via the Serverless Framework config. You can set this globally for all functions or per-function in your serverless.yml:

service: your-service-name

provider:
  name: aws
  runtime: nodejs8.10
  tracing:
    lambda: true # Enables X-Ray for all functions in the service
    apiGateway: true # Add this if your functions are triggered by API Gateway

functions:
  imageProcessingFunction:
    handler: handler.processImage
    # Optional: Override global settings here if needed
    # tracing:
    #   lambda: true

2. Grant IAM Permissions for X-Ray

Your Lambda execution role needs permissions to send trace data to X-Ray. Add either the managed policy or a custom permission set to your serverless.yml:

provider:
  name: aws
  iamRoleStatements:
    - Effect: Allow
      Action:
        - xray:PutTraceSegments
        - xray:PutTelemetryRecords
      Resource: "*"

3. Wrap AWS SDK Clients for Service Tracing

While basic Lambda execution can be auto-traced, calls to SNS, Rekognition, and S3 won't show up in traces unless you wrap the AWS SDK with the X-Ray module. First install the dependency:

npm install aws-xray-sdk

Then initialize it in your Lambda handler:

// Wrap the AWS SDK to trace all service calls
const AWS = require('aws-xray-sdk').captureAWS(require('aws-sdk'));

// Initialize your service clients as usual
const sns = new AWS.SNS();
const rekognition = new AWS.Rekognition();
const s3 = new AWS.S3();

exports.processImage = async (event) => {
  // Your function logic here—all AWS service calls will now be traced
};

4. Troubleshooting Tips

If you still don't see data after setting this up:

  • Check your Lambda's CloudWatch Logs for X-Ray-related errors (like permission issues or SDK wrapping mistakes)
  • Make sure you've invoked the function at least a few times after making changes—traces aren't generated until the function runs
  • Verify that the X-Ray console is filtered to the correct region and time range

Once you've implemented these steps, you should start seeing full trace data in the X-Ray console, including segments for Lambda execution and each downstream service call.

内容的提问来源于stack exchange,提问作者Pawel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:20:04