How to upgrade CloudFront Node.js from v1 to v2

From Node.js runtime version 24 onward, callback-based function handlers have been removed and replaced by the async/await pattern. According to the supported runtimes list of AWS, the Node.js runtimes prior to this version will all be deprecated in April 2027.

This guide will help you navigate through changes included in version 2 of the DataDome CloudFront Node.js module for a successful upgrade.

Overview of the changes

  • Migrate callback-based function to async/await pattern
  • Migrate from CommonJS to ES Module

Choose your migration path

Depending on your use case, follow one of the migration process.

How to migrate a single file in the Lambda@Edge function?

If you only have a datadome.js file in your Lambda function, you just have to follow the upgrade section of the documentation to use the latest version of the module.

How to migrate multiple files in the Lambda@Edge function?

If you have multiple files in your Lambda function aside the DataDome's module, you need to:

  • follow the documentation about how to use an existing Lambda@Edge function to retrieve the latest version of the module and use it inside your code
  • migrate your code from callback-based function signature for the async/await pattern function signature:
    • The responses should be returned
    • The errors should be thrown
exports.handler = (event, context, callback) => {
  // Your code here
  try {
    callback(null, {
      statusCode: 200,
      body: JSON.stringify({
        message: "Operation completed successfully"
      })
    });
  } catch (error) {
    callback(error);
  }
}
exports.handler = async (event, context) => {
  // Your code here
  try {
    return {
      statusCode: 200,
      body: JSON.stringify({
        message: "Operation completed successfully"
      })
    };
  } catch (error) {
    throw error;
  }
}
  • change the module format from CommonJS to ES Module as shown in this AWS documentation.
    It includes:
    • renaming your .js files to .mjs files to specify that you are using ES Module
    • changing your module.exports occurrences to export keyword
    • changing your const module = require('module-name') occurrences to import module from 'module-name'

How to migrate when using CloudFormation?

If you are managing your Lambda function with CloudFormation, you just have to follow the upgrade section of the documentation to use the latest version of the module.

What to do after migrating to the async/await pattern?

Update your Node.js runtime version

📘

If you are using CloudFormation to manage your Lambda function, it will already use the latest runtime version.

Since your code is now using async/await pattern, you can now upgrade your Node.js runtime's version to the latest one:

  • On your Lambda function, click on the "Edit" button on the "Runtime settings" part
  • Choose the latest Node.js runtime and save
  • Click on the "Actions" button and "Deploy to Lambda@Edge"