This occurs when using Got 10 in combination with AWS XRay on AWS Lambda. Note: Got 9 works fine with the provided snippet.
I also opened an issue on the got repository.
Whenever XRay capture is enabled, the lambda doesn't return any response, and the actual request is made against https://localhost. Subsequent executions take longer and longer (even up to 2 minutes) until finally one execution will show a bunch of LAMBDA_RUNTIME_ERROR.
Edit: the lambda execution actually dies midway, while without XRay enabled, it successfully completes.
Get contents of google.com and return the specified response (body: 'ok')
require('aws-xray-sdk-core').captureHTTPsGlobal(require('https'))
const got = require('got')
export async function handler() {
await got.get(`https://google.com`)
return {
statusCode: 200,
body: 'ok',
}
}

@chanchiem should this also receive the bug label? It is killing my executions midway, while it worked fine without XRay enabled
Hi @MickVanDuijn,
Thank you for raising this issue. I was able to reproduce it and the specific fault I got on the X-Ray console was:
Error: connect ECONNREFUSED 127.0.0.1:443
聽 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128)
That makes me think this issue might be related to the one in #217, and it seems like that host implies the HTTP request can't even get out to the internet. I'm investigating that issue with Lambda, and I'll add this issue to that investigation.
Thanks @willarmiros ! What's also strange is that the requested URL turned into localhost, I don't see that on #217
Hi @MickVanDuijn,
I believe I've found the root cause of the issue. The got library overwrites existing headers in requests it makes, however the X-Ray SDK requires its specific header to be attached to all outgoing HTTP requests in order for it to trace them. You can read about the structure of the header here. You can create your header string by using the X-Ray SDK's APIs to get the Trace ID, segment ID, and sampling decision, or you should be able to extract it from the event.headers object in your Lambda handler.
As for forcing the header into the got request, you'll have to look into their documentation for that. I hope this helps and please let me know if this resolves your issue!
As @szmarczak stated (https://github.com/sindresorhus/got/issues/1022#issuecomment-580379806), the http/https patcher doesn't support the newer versions of the https.request syntax. I created #247 to fix that 馃檪
Thanks @MickVanDuijn! I'll follow up on the PR.
Fix released in v3.0.0-alpha.2
Most helpful comment
As @szmarczak stated (https://github.com/sindresorhus/got/issues/1022#issuecomment-580379806), the
http/httpspatcher doesn't support the newer versions of thehttps.requestsyntax. I created #247 to fix that 馃檪