Intermittent 'UND_ERR_SOCKET' Error Using AWS Parameter and Secrets Lambda extension

0

Hello, I've implemented the AWS Parameter and Secrets Lambda Extension into a fairly straightforward handler but I'm running into an intermittent error when the secret is retrieved from the cache. I'm unsure how to troubleshoot this issue given the layer's functionality is largely hidden and the failure is intermittent .

The Lambda is created and deploy via CDK and the layer was implemented following these docs.

Environment: nodejs20.x, "aws-cdk": "2.136.0"

Lambda config info: memorySize: 256,, ParamsAndSecretsVersions.V1_0_103, timeout: Duration.seconds(25)

Layer arn: arn:aws:lambda:eu-west-1:015030872274:layer:AWS-Parameters-and-Secrets-Lambda-Extension:11

// handler.ts 
import { createLogger } from 'logger';

export const main = async (input): Promise<any> => {
  const LOGGER = createLogger('getCreds', input?.traceId,);
  try {
    const secretResponse = await fetch(process.env.PARAMS_EXTENSION_ENDPOINT, {
      method: 'GET',
      headers: { 'X-Aws-Parameters-Secrets-Token': process.env.AWS_SESSION_TOKEN },
    });
    const responseJson = await secretResponse.json();
    const creds = JSON.parse(responseJson.SecretString);
    return creds ;
  } catch (error) {
    LOGGER.error(error)
    return Promise.reject();
  }
};

Logs displaying the Layer configuration, which then goes on to return the secret successfully in the same execution.

[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 PARAMETERS_SECRETS_EXTENSION_LOG_LEVEL is debug. Log level set to debug.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG PARAMETERS_SECRETS_EXTENSION_CACHE_ENABLED is true.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG PARAMETERS_SECRETS_EXTENSION_CACHE_SIZE is 500 objects.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG SECRETS_MANAGER_TTL is 5m0s
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG SSM_PARAMETER_STORE_TTL is 5m0s.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG SECRETS_MANAGER_TIMEOUT_MILLIS is 5s.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG SSM_PARAMETER_STORE_TIMEOUT_MILLIS is 0s.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG PARAMETERS_SECRETS_EXTENSION_MAX_CONNECTIONS is 3.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG PARAMETERS_SECRETS_EXTENSION_HTTP_PORT is 2773.
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 INFO Systems Manager Parameter Store and Secrets Manager Lambda Extension 1.0.103
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 DEBUG Creating a new cache with size 500
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 INFO Serving on port 2773
EXTENSION	Name: AWSParametersAndSecretsLambdaExtension	State: Ready	Events: [INVOKE, SHUTDOWN]
START RequestId: xyz Version: $LATEST
[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:36:46 INFO ready to serve traffic

Following invokes may go on to then return the secret successfully a number of times from cache but occasionally this following issue is seen -

[AWS Parameters and Secrets Lambda Extension] 2024/04/19 14:22:27 INFO ready to serve traffic
{
    "errorType": "Error",
    "errorMessage": "handled",
    "stack": [
        "Error: handled",
        "    at Object.intoError (file:///var/runtime/index.mjs:46:16)",
        "    at Object.textErrorLogger [as logError] (file:///var/runtime/index.mjs:684:56)",
        "    at postError (file:///var/runtime/index.mjs:801:27)",
        "    at done (file:///var/runtime/index.mjs:833:11)",
        "    at fail (file:///var/runtime/index.mjs:843:11)",
        "    at file:///var/runtime/index.mjs:872:20"
    ]
}
{
   "level": "error",
        "error": {
            "cause": {
                "name": "SocketError",
                "code": "UND_ERR_SOCKET",
                "socket": {
                    "localAddress": "127.0.0.1",
                    "localPort": 41530,
                    "bytesWritten": 3747,
                    "bytesRead": 948
                }
            }
        }
}

Any advice would be much appreciated.

David
asked 3 months ago409 views
1 Answer
1

Hi,

It seems like there was an issue with the NodeJS fetch implementation: internal undici:fetch throws error where node-fetch and curl succeed. Although it has been closed as fixed, there is an interesting thread on StackOverflow suggesting otherwise: https://stackoverflow.com/a/77909959.

Try using a different HTTP client (node-fetch, axios, etc.) and see if it resolves the issue.

Regards

AWS
answered 3 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.

Guidelines for Answering Questions