CloudFront.testFunction throws internal error

0

I’m having a Cloudfront function that is working perfectly, as I expect. I have extended the deployment script to include a test, which was working fine in 2024. I’ve now applied some minor changes to the function, deployed it, and it works again all fine. But the tests are not working anymore, throwing an internal error (with the previous as well as the new version of the function code).

Here’s a version of the code that is simplified, but still generates the error.

import {CloudFront} from '@aws-sdk/client-cloudfront'

const cloudfront = new CloudFront({
  apiVersion: '2023-05-03',
  region: 'eu-central-1',
})

const Name = 'Bot_detector'

const EventObject = Buffer.from(
  JSON.stringify({
    version: '1.0',
    context: {eventType: 'viewer-request'},
    request: {method: 'GET', uri: '/'},
  })
)

const functionRef = await cloudfront.describeFunction({Name})

await cloudfront.testFunction({
  IfMatch: functionRef.ETag,
  Stage: 'DEVELOPMENT',
  Name,
  EventObject,
})

In this test code, the error throws on invocation of testFunction as prints the following.

/path/to/repository/node_modules/.pnpm/@smithy+smithy-client@3.1.1/node_modules/@smithy/smithy-client/dist-cjs/index.js:839
  const response = new exceptionCtor({
                   ^


CloudFrontServiceException [InternalError]: UnknownError
    at throwDefaultError (/path/to/repository/node_modules/.pnpm/@smithy+smithy-client@3.1.1/node_modules/@smithy/smithy-client/dist-cjs/index.js:839:20)
    at <anonymous> (/path/to/repository/node_modules/.pnpm/@smithy+smithy-client@3.1.1/node_modules/@smithy/smithy-client/dist-cjs/index.js:848:5)
    at de_CommandError (/path/to/repository/node_modules/.pnpm/@aws-sdk+client-cloudfront@3.592.0/node_modules/@aws-sdk/client-cloudfront/dist-cjs/index.js:6810:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at <anonymous> (/path/to/repository/node_modules/.pnpm/@smithy+middleware-serde@3.0.0/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20)
    at <anonymous> (/path/to/repository/node_modules/.pnpm/@smithy+core@2.2.0/node_modules/@smithy/core/dist-cjs/index.js:165:18)
    at <anonymous> (/path/to/repository/node_modules/.pnpm/@smithy+middleware-retry@3.0.3/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38)
    at <anonymous> (/path/to/repository/node_modules/.pnpm/@aws-sdk+middleware-logger@3.577.0/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:34:22)
    at testFunction (/path/to/repository/cloudfront/deploy.mts:128:26)
    at async Promise.all (index 0) {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 500,
    requestId: '1858a535-8df8-4fd3-bf61-865870165bd4',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 3,
    totalRetryDelay: 163
  },
  Type: 'Receiver',
  Code: 'InternalError'
}

Happens on @aws-sdk/client-cloudfront versions 3.592 and 3.777, running on Node 18.13.

asked a month ago49 views
2 Answers
2

It may related to several factors, please try at the following:

  1. Validate the Event Object Ensure that the event object is correctly formatted and adheres to the expected structure for CloudFront functions. The event object must be Base64-encoded binary data and follow the guidelines in the CloudFront documentation:
{
  "version": "1.0",
  "context": {
    "eventType": "viewer-request"
  },
  "request": {
    "method": "GET",
    "uri": "/",
    "headers": {
      "host": {
        "value": "example.com"
      }
    }
  }
}

Ensure that headers, cookies, and query strings are in lowercase as required.

  1. Check the Function's Stage Verify that the function's stage is set to DEVELOPMENT or LIVE correctly. If the function is unpublished or in an invalid stage, it might cause errors.

  2. Update SDK and Dependencies Ensure that you're using the latest version of the AWS SDK for JavaScript. Sometimes, bugs in older versions can cause unexpected errors:

npm update @aws-sdk/client-cloudfront

Also, check for compatibility between Node.js version 18.13 and the SDK.

  1. Debugging the Function Use the FunctionExecutionLogs returned by testFunction to identify issues within the function's code. If the logs are empty, it might indicate a problem with how the function is being invoked.

  2. Retry Logic The error metadata indicates multiple retry attempts. Implement exponential backoff in your code to handle transient errors:

const retryOptions = {
  maxRetries: 5,
  retryDelayOptions: { base: 200 }
};
const cloudfront = new CloudFront({ region: 'eu-central-1', retryOptions });
  1. Test with CLI If the SDK continues to throw errors, test the function using the AWS CLI to isolate the issue:
aws cloudfront test-function \
  --name Bot_detector \
  --if-match <ETag> \
  --event-object fileb://event.json \
  --stage DEVELOPMENT

This can help determine if the issue is with the SDK or the function itself.

EXPERT
answered a month ago
  • Before post my question, I’ve entered it into Chat GPT and got exactly the same suggestions that you posted as “expert” here. However good the suggestions may sound, they vary between being already answered in the original question, not leading anywhere, or even being plain wrong information.

    1. CloudFront.testFunction expects the typing Uint8Array<ArrayBufferLike> for the property EventObject.
    2. Stage is correct
    3. As posted in the answer, the issue also arises with the latest SDK version
    4. You can see in the original post that no error object is returned. And yes, obviously there is an issue with how the function is being invoked, hence my question here.
    5. The issue is consistent, no matter how often I retry.
    6. Just prints out An error occurred (InternalError) when calling the TestFunction operation (reached max retries: 2):

    Since I am myself able to promt LLM, and to save everyone’s resources, please refrain from posting machine-generated answers.

0

In my case the problem was that my response object was missing the "statusDescription" field. I suggest generating an event using the UI and copying the JSON into your test.

answered 7 days 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