laravel-bridge icon indicating copy to clipboard operation
laravel-bridge copied to clipboard

Reading the AWS Request ID

Open harmenjanssen opened this issue 1 year ago • 9 comments

Hi,

I was wondering if it's possible to get at the original Lambda Request ID within your Laravel app?

I found the Context class which has a reference to the ID, but I'm unsure if that's accessible from within my Laravel app. (tried dependency-injecting it, but that was not the answer)

Thanks a lot in advance!

harmenjanssen avatar Dec 06 '23 14:12 harmenjanssen

Yes: https://github.com/brefphp/laravel-bridge/blob/2bff7e67ed11f833329cfd5340f7d3e606347e54/config/bref.php#L31

tillkruss avatar Dec 06 '23 16:12 tillkruss

Hmm, alright, I have to be more specific I guess: at this point I am not getting the X-Request-ID header in my requests. I turned request_context on, but that doesn't give me any information about the AWS request.

I'm logging all headers coming in to my controller via the request, but it's not there. Is there a way to get at the current context either in a controller, or middleware, or somewhere in that chain?

harmenjanssen avatar Dec 07 '23 06:12 harmenjanssen

I am having the same problem. @harmenjanssen can you reopen the issue as ATM it's not clear how to use this config option :/

MantasVaitkunas avatar Dec 20 '23 14:12 MantasVaitkunas

Does your request object not have the X-Request-ID header?

tillkruss avatar Dec 20 '23 17:12 tillkruss

Does your request object not have the X-Request-ID header?

Nope, it does not.

harmenjanssen avatar Dec 20 '23 19:12 harmenjanssen

Mhhh, turns out my apps aren't actually using this anymore. @georgeboot Do we have any way to access the AWS request id?

tillkruss avatar Dec 20 '23 20:12 tillkruss

Afk atm, but there should be a $_SERVER var called HTTP_X_REQUEST_ID I believe

georgeboot avatar Dec 20 '23 20:12 georgeboot

As @georgeboot said, I had a look into $_SERVER and found out that $_SERVER['LAMBDA_INVOCATION_CONTEXT'] has necessary info:

$requestId = null;
if (isset($_SERVER['LAMBDA_INVOCATION_CONTEXT'])) {
    $lambdaContext = json_decode($_SERVER['LAMBDA_INVOCATION_CONTEXT']);
    $requestId = $lambdaContext?->awsRequestId;
}

MantasVaitkunas avatar Dec 21 '23 10:12 MantasVaitkunas

I feel like we should expose the invocation context a little better, maybe even attach some of the data to the Request object.

tillkruss avatar Dec 21 '23 17:12 tillkruss