DataDog/datadog-lambda-js

Webpack not able to bundle datadog-lambda-js

mohitm94 opened this issue · 10 comments

Expected Behavior

datadog-lambda-js handler doesn't throw any errors

Actual Behavior

webpack is not able to package datadog-lambda-js handler due to dependencies on lambda runtime.

Steps to Reproduce the Problem

  1. Follow the steps mentioned here. https://docs.datadoghq.com/serverless/installation/nodejs/?tab=custom#using-the-package
  2. Run serverless package command.
  3. Webpack fails with an error.

Specifications

  • Datadog Lambda Layer version: N.A
  • Node version: 14.x

Stacktrace

ERROR in ./node_modules/datadog-lambda-js/dist/handler.js 8:11-52
Module not found: Error: Can't resolve '/var/runtime/UserFunction' in '/Users/mohitm/dev/git/serverless-request-management/node_modules/datadog-lambda-js/dist'
resolve '/var/runtime/UserFunction' in '/Users/mohitm/dev/git/serverless-request-management/node_modules/datadog-lambda-js/dist'
using description file: /Users/mohitm/dev/git/serverless-request-management/node_modules/datadog-lambda-js/package.json (relative path: ./dist)
  root path /Users/mohitm/dev/git/serverless-request-management
    using description file: /Users/mohitm/dev/git/serverless-request-management/package.json (relative path: ./var/runtime/UserFunction)
      no extension
        /Users/mohitm/dev/git/serverless-request-management/var/runtime/UserFunction doesn't exist
      .js
        /Users/mohitm/dev/git/serverless-request-management/var/runtime/UserFunction.js doesn't exist
      .json
        /Users/mohitm/dev/git/serverless-request-management/var/runtime/UserFunction.json doesn't exist
      .wasm
        /Users/mohitm/dev/git/serverless-request-management/var/runtime/UserFunction.wasm doesn't exist
      as directory
        /Users/mohitm/dev/git/serverless-request-management/var/runtime/UserFunction doesn't exist
  No description file found in /var/runtime or above
  no extension
    /var/runtime/UserFunction doesn't exist
  .js
    /var/runtime/UserFunction.js doesn't exist
  .json
    /var/runtime/UserFunction.json doesn't exist
  .wasm
    /var/runtime/UserFunction.wasm doesn't exist
  as directory
    /var/runtime/UserFunction doesn't exist

webpack compiled with 1 error and 2 warnings in 366 ms

Webpack config

const slsw = require('serverless-webpack');
const CopyPlugin = require('copy-webpack-plugin');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  externals: ['dd-trace', 'datadog-lambda-js', nodeExternals()],
  externalsPresets: {
    node: true
  },
  optimization: {
    minimize: false
  },
  entry: slsw.lib.entries,
  target: 'node',
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  devtool: 'inline-cheap-module-source-map'
};

Webpack serverless config

webpack:
    webpackConfig: 'webpack.config.js' # Name of webpack configuration file
    packager: 'npm' # Packager that will be used to package your external modules
    excludeFiles: tests/ # Provide a glob for files to ignore
    concurrency: 6
    serializedCompile: true
    includeModules:
      forceExclude:
        - dd-trace
        - datadog-lambda-js

function definition in serverless.yml

functions:
  auth:
    handler: node_modules/datadog-lambda-js/dist/handler.handler
    environment:
      DD_LAMBDA_HANDLER: functions/auth.handler
    timeout: 30

@mohitm94 are you using https://github.com/DataDog/serverless-plugin-datadog? also see https://docs.datadoghq.com/serverless/installation/nodejs/?tab=serverlessframework. E.g., you do not need

handler: node_modules/datadog-lambda-js/dist/handler.handler
environment:
  DD_LAMBDA_HANDLER: functions/auth.handler

@tianchu I was using the https://github.com/DataDog/serverless-plugin-datadog before this. We wanted to try a different solution because it impacts our cold start time significantly.

I tried to follow the steps here. that mentions not using the layer
https://docs.datadoghq.com/serverless/installation/nodejs/?tab=custom#using-the-package

and it resulted in the errors that I reported.

I also tried using serverless-plugin-datadog with addLayers: false but that didn't seem to work either.

I got this error in my function then.

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'handler'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "stack": [
    "Runtime.ImportModuleError: Error: Cannot find module 'handler'",
    "Require stack:",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1063:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)",
    "    at Module.load (internal/modules/cjs/loader.js:928:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:769:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

Do have any working example or documentation that I can follow that doesn't include adding the layer? as it severely impacts our cold start times.

@mohitm94 From what I recall webpack did have issue with bundling dd-trace, which was a known issue. Not sure if it has been fixe, but you can try.

If you are not going to use layer, you should set addLayers: false, which I believe you already have.

Two potential reasons for you getting that error, can you try both?

  1. Can you remove them from forceExclude and try again? This instruction was assuming you are using the plugin and the layer.
forceExclude:
  - dd-trace
  - datadog-lambda-js
  1. The handler function name in the webpack bundle is probably minified to something else? is it still functions/auth.handler?
DD_LAMBDA_HANDLER: functions/auth.handler

Hey @tianchu, Sorry for the late reply. Tried removing them forceExlcude and didn't make any difference. still got the same error. Also, the handler name is the same after webpack as well.

Trying to check different permutations and combinations, but no success so far.

Bump. I'm experience the same issue all of a sudden

We're seeing the same error when using esbuild and the datadog plugin using serverless framework (nodejs, typescript lambda function)

Same, let us know if you prefer us to open a support case instead.

Can anyone help create a hello-world application for us to reproduce the issue? Also does https://docs.datadoghq.com/serverless/troubleshooting/serverless_tracing_and_webpack help?

We've resolved this, so it's not an issue anymore. It was an issue on our side on how we configured to exclude dd-trace and datadog-lambda-js from esbuild bundling. I might open a PR to update the Datadog documentation with an esbuild example on Monday.

I'm closing the issue due to the lack of activity. Feel free to reopen if needed.