Search code examples
aws-lambdaaws-xray

How can I find the trace ID my lambda is under?


I want to be able to easily find the trace ID (and segment ID) for my lambda's execution in logs.

I realise this is available on the REPORT logs that the lambda does automatically, but that logs doesn't fit my custom format for indexing and aggregation. This means it is cumbersome to get from logs about errors and such to the trace ID.

Is there a way to access the trace ID (and segment ID) from within the lambda? Looks like it's not on the context passed to the handler, and I can't see what I need in the XRay SDK. I see there are questions about changing the trace id, but I don't want to do that - just find out what it is so I can add it to all my logs.

I'm using C# .NET lambdas, although that's probably not important.

Thanks!


Solution

  • There is a environment variable set in your Lambda that contains the Trace ID:

    _X_AMZN_TRACE_ID
    

    So you just need to read that environment variable in your code:

    string traceId = Environment.GetEnvironmentVariable("_X_AMZN_TRACE_ID");
    

    Source: AWS Documentation.