dockeraws-api-gatewayasp.net-core-2.1request-headers

Getting AWS API Gateway Request Id in backend api


We have setup of apis as below

Development : ASP NET Core 2.1 C#
Backend api deployment : In AWS through docker
Gateway : AWS API Gateway which contains custom lambda Authorizer

Requirement : To get the API Gateway request id in backend api for logging and request tracking.

As the request passes through the API gateway, it generates a request id. This is set in response header in "x-amzn-RequestId" by the gateway and can be visible in client (like postman). I could find that this id is also available in the lambda authorizer in the object of APIGatewayCustomAuthorizerRequest - .RequestContext.RequestId, which I could use for the authorizer logging.

Now as the request is authorized, actual api is invoked. I want the same request id in this api for logging purpose. Eventually - to identify the journey of request through the gateway-aurhorizer-api having same request and generate the stats.

In the api code, we have also implemented OnActionExecuting() of APIControllerActionFilter. I want to know if the gateway request id is available here by any means ? Or any way to pass the id from the gateway to the actual api ?


Solution

  • In API Gateway console, you can add HTTP headers in the Integration Request section. So you can add a custom header and map it back to the requestId.

    enter image description here

    or in Mapping Templates you can do this

    #set($context.requestOverride.header.requestId = $context.requestId)
    

    This will then be passed downstream by API Gateway.

    From a test using Beeceptor.com

    enter image description here