Skip to content

Resolving "socket hang up" and "ETIMEDOUT" Errors in AWS Lambda with Node.js and Axios

1

Hello AWS re:Post community,

I am currently using AWS Lambda with Node.js to send messages through the FCM HTTP v1 API.

I'm using axios for HTTP requests, and my Lambda function is configured with 512 MB of memory.

Occasionally, during periods of high message volume, I encounter "socket hang up" errors, leading to failed message transmissions.

Alongside this, I sometimes see the following error: connect ETIMEDOUT 216.239.36.57:443.

I discovered a relevant issue in the axios GitHub repository (https://github.com/axios/axios/issues/6113) and adjusted my axios configuration based on the suggestions there. Here is my current axios setup:

const axios = require('axios').default;
const http = require('http');
const https = require('https');

axios.defaults.httpAgent = new http.Agent({
    keepAlive: false,
    maxSockets: 100,
});

axios.defaults.httpsAgent = new https.Agent({
    keepAlive: false,
    maxSockets: 100,
});

module.exports = axios;

Despite these adjustments, the issue persists. Could anyone advise on additional strategies to avoid these "socket hang up" and "ETIMEDOUT" errors in this context? Any insights or further configuration recommendations would be greatly appreciated.

Thank you!

1 Answer
0

Hi Alan,

Thanks for sharing your challenge with "socket hang up" and "ETIMEDOUT" errors in your Lambda setup. These issues can be frustrating, especially when working with high message volumes. Let’s explore some strategies to help you tackle this!


Clarifying the Issue

You're experiencing connection issues when using AWS Lambda with Node.js and Axios to send messages via the FCM HTTP v1 API. Despite configuring Axios with http.Agent and https.Agent (e.g., keepAlive: false and maxSockets: 100), errors persist during high traffic, causing failed transmissions. Let's break this down and address it step-by-step.


Key Terms

  • Socket hang up: The server or client closes the connection unexpectedly before the transaction completes.
  • ETIMEDOUT: A timeout error, usually caused by network delays or slow server responses.
  • Axios: A promise-based HTTP client for Node.js.
  • AWS Lambda: A serverless compute service triggered by events to execute code.

The Solution (Our Recipe)

Steps at a Glance:

  1. Optimize Lambda timeout settings to prevent premature termination.
  2. Use keepAlive: true in http.Agent and https.Agent for better connection management.
  3. Allocate more memory to Lambda for enhanced processing power.
  4. Implement retry logic with exponential backoff for transient errors.
  5. Activate detailed CloudWatch logs to monitor and troubleshoot connections.

Step-by-Step Guide:

  1. Optimize Lambda Timeout Settings: Match the Lambda timeout to the expected maximum API response time. For example, if the API's response time is 20 seconds, ensure the Lambda timeout is slightly higher (e.g., 25 seconds).

  1. Enable keepAlive: Update Axios to use keepAlive: true, which helps maintain persistent connections and minimizes the creation of new sockets:
    axios.defaults.httpAgent = new http.Agent({
        keepAlive: true,
        maxSockets: 100,
    });
    axios.defaults.httpsAgent = new https.Agent({
        keepAlive: true,
        maxSockets: 100,
    });
  2. Allocate More Memory: Increase Lambda's memory allocation (e.g., to 1024 MB or higher). More memory improves both computational power and network handling.

  1. Add Retry Logic: Use a retry library like axios-retry or implement custom retry logic with exponential backoff to handle transient errors gracefully:
    const axiosRetry = require('axios-retry');
    axiosRetry(axios, { retries: 3, retryDelay: axiosRetry.exponentialDelay });

  1. Enable Verbose CloudWatch Logs: Log request and response details in CloudWatch to pinpoint connection bottlenecks. Detailed logs provide insights into whether the issue originates from your API, the Lambda function, or the network.

Closing Thoughts

Alan, I hope these steps provide clarity and actionable solutions to resolve the connection issues. Feel free to share updates or additional questions—we’re here to help you get this running smoothly. Wishing you the best of luck with your Lambda function and a seamless API integration!


Take care and happy coding! 🚀


Cheers, Aaron 😊

answered a year 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.