Skip to content

https hanging on cold start of lambda (node 22.x)

0

I am troubleshooting https failure(s) in a lambda project. When it starts (cold start, no reserved concurrency), it successfully contacts auth0 for a bearer token, then attempts to use that to authenticate a simple request to a graphQL API (same request payload and connection works in postman).

I have tried basic fetch, used node-fetch, used axios post, and even just some raw https, but each time the lambda runs it says it is starting the request (from a log I am writing), and then the lambda shuts down with no further logging (that would occur after the await of the axios.post invocation) The lambda is configured with a 30 second timeout, but terminates with a 200 status code after about 3-4 seconds.

const response = await axios_1.default.post(endpoint, query, { headers: { "Content-Type": "application/json", "Authorization": Bearer ${token}, "x-api-type": "partner" }, timeout: Number(process.env.AXIOS_TIMEOUT_MS) || 15000 });

NOTE: From the lambda UI, quickly re-clicking test after the first frozen https request / end of execution with status 200, it just throws a 404 from the graphQL request. First, re-clicking has no cold start, seems the request actually fires. Now to see if the 404 is a rate limit response from the graphQL endpoint or something more nefarious (hooray undocumented API changes)

asked 2 months ago54 views
2 Answers
0
Accepted Answer

Ok, so hours wasted, but.. the invocation of the function containing the axios.post.... was not awaiting. So the symptom of test->hang, test->exception seems like it was just coming back into the context of the unhandled promise resolution. Ego says delete the question, but maybe someone else comes along and is being as obtuse as I was. :) Now to roll back to simple fetch so I dont have extra dependencies. Go forth and noob no more.

answered 2 months ago
0

Based on your description, this appears to be a Lambda cold start issue where the function is terminating prematurely without completing the HTTP request. Here are several potential causes and solutions:

VPC Configuration Issues If your Lambda function is attached to a VPC, this can introduce significant latency and connection issues during cold starts. Without proper VPC configuration (NAT Gateway or VPC endpoints), your function won't be able to reach external endpoints like Auth0 or your GraphQL API. Verify your VPC configuration or consider removing the VPC attachment if it's not required for your use case.

Execution Environment Initialization During cold starts, Lambda goes through several initialization phases including container provisioning, runtime initialization, and dependency loading. The Node.js 22 runtime needs to fully initialize before making external requests. Ensure your HTTP client libraries are properly initialized before use.

Request Signing and Headers When making requests to AWS services like AppSync (if that's your GraphQL API), you need to properly sign requests using AWS Signature Version 4. Your current setup with a simple Bearer token might not be sufficient if you're calling an AWS service. You would need to use the AWS SDK's signature utilities to sign the request with appropriate headers including Content-Type, host, and the authorization signature.

Timeout and Error Handling The 3-4 second termination with a 200 status suggests the Lambda might be completing successfully but your async operation isn't being awaited properly. Ensure your handler function is properly async and that all promises are being awaited. Also verify that your error handling isn't silently catching and suppressing errors.

Recommendations:

  1. Add comprehensive error handling with try-catch blocks around your axios call
  2. Log the full error object if the request fails
  3. Check CloudWatch Logs for any initialization errors
  4. Verify network connectivity from your Lambda function
  5. If calling AWS AppSync, implement proper request signing
  6. Consider using AWS X-Ray to trace the request flow and identify where it's hanging
  7. Test with a simpler HTTP endpoint first to isolate whether it's a cold start issue or specific to your GraphQL API connection
    Sources
    Understanding and Remediating Cold Starts: An AWS Lambda Perspective | AWS Compute Blog
    Building a real-time WebSocket client in AWS AppSync - AWS AppSync GraphQL
answered 2 months ago
AWS
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.