“other side closed” – AWS Lambda with Parameters and Secrets Extension

Secrets lambda extensions is optional and AWS maintained layer that provide fast access to secrets manager or parameter store. In fact, it is just locally available http server that you can query with your favorite http client to get your app’s secrets. By default it is available on port 2773.

Recently I have come across following error by trying to get my parameter store value with JS fetch() method:

{
   "errorType":"TypeError",
   "errorMessage":"fetch failed",
   "cause":{
      "errorType":"SocketError",
      "errorMessage":"other side closed",
      "code":"UND_ERR_SOCKET",
      "name":"SocketError",
      "socket":{
         "localAddress":"127.0.0.1",
         "localPort":33424,
         "remoteAddress":"127.0.0.1",
         "remotePort":2773,
         "remoteFamily":"IPv4",
         "bytesWritten":1201,
         "bytesRead":0
      },
      "stack":[
         "SocketError: other side closed",
         "    at Socket.onSocketEnd (node:internal/deps/undici/undici:9790:26)",
         "    at Socket.emit (node:events:529:35)",
         "    at endReadableNT (node:internal/streams/readable:1368:12)",
         "    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)"
      ]
   },
   "stack":[
      "TypeError: fetch failed",
      "    at Object.fetch (node:internal/deps/undici/undici:11576:11)",
      "    at async getSecret (file:///var/task/index.js:104:20)",
      "    at async Runtime.handler (file:///var/task/index.js:13:13)"
   ]
}

Code that triggered this error:

await fetch("https://" + event["request"]["headers"]["host"] + "/graphql", {
  method: "POST",
  body: JSON.stringify({
    query: mutation,
    operationName: "MyMutation",
  }),
  headers: {
    "Content-Type": "application/json",
    "x-api-key": event["request"]["headers"]["x-api-key"],
  },
});

To debug this problem, start with setting PARAMETERS_SECRETS_EXTENSION_LOG_LEVEL lambda environment variable to debug. More about the available variables.

In my case I simply provided more memory to my lambda function to 256MB from 128MB. And that solved my problem!