AWS Lambda with Webpack throws error

I am trying to use the aws lambda layer to communicate with APM

We use webpack for bundling , I am getting below error. I have tried including the elastic-apm-node in package.json with no luck .

Just wondering if there is a way to get this working ? Or it is not supported ?

Error: Cannot find module 'elastic-apm-node/start'

	Error: Cannot find module 'elastic-apm-node/start'
	2023-11-06T21:39:03.556+00:00	Require stack:
	2023-11-06T21:39:03.556+00:00	- internal/preload
	2023-11-06T21:39:03.556+00:00	at Function.Module._resolveFilename (internal/modules/cjs/loader.js:931:15)
	2023-11-06T21:39:03.556+00:00	at Function.Module._load (internal/modules/cjs/loader.js:774:27)
	2023-11-06T21:39:03.556+00:00	at internalRequire (internal/modules/cjs/loader.js:161:19)
	2023-11-06T21:39:03.556+00:00	at Module._preloadModules (internal/modules/cjs/loader.js:1273:5)
	2023-11-06T21:39:03.556+00:00	at loadPreloadModules (internal/bootstrap/pre_execution.js:475:5)
	2023-11-06T21:39:03.556+00:00	at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:72:3)
	2023-11-06T21:39:03.556+00:00	at internal/main/run_main_module.js:7:1 {
	2023-11-06T21:39:03.556+00:00	code: 'MODULE_NOT_FOUND',
	2023-11-06T21:39:03.556+00:00	requireStack: [ 'internal/preload' ]
	2023-11-06T21:39:03.556+00:00	} 

Hello @Myths.

If you are specifying the elastic-apm-node/start module via the NODE_OPTIONS environment variable, as described at Monitoring AWS Lambda Node.js Functions | APM Node.js Agent Reference [4.x] | Elastic and have the arn:aws:lambda:<REGION>:267093732750:layer:elastic-apm-node-ver-4-1-0:1 lambda layer install, then I would expect that to work.

My best first guess is that you may not have the layer installed.

Some notes/warnings:

  • I haven't tested this with a webpack-bundled Lambda function myself.
  • You explicitly do not want the "elastic-apm-node" package listed in your dependencies and bundled with webpack. For the Lambda case, the elastic-apm-node should come from the Lambda layer. Having a second elastic-apm-node bundled into the bundle will confuse things.
  • (Also, elastic-apm-node cannot work inside a bundle. See notes about that at Starting the agent | APM Node.js Agent Reference [4.x] | Elastic However, I don't think this is the issue you are hitting.)

If you are unable to get it working, are you able to show me a small reproduction git repo with a trivial Lambda function module that is bundled with webpack as you are using it? I could then try to run it myself in Lambda to see if I can reproduce.


Thanks for your reply.

Well its definitely installed we are using serverless and i can see post deployment of the function that layer is available for the function. And i can see the environment variable present as NODE_OPTIONS: -r elastic-apm-node/start

I did try in my initial attempt to not specify elastic-apm-node . But then included it based on the logs. I have removed it but cant seem to get this error sorted.

Sorry I am kind of confused on this point

elastic-apm-node cannot work inside a bundle

we do use webpack to bundle the Lambdas we have Mono Repo based repository ? Can i specify the package in web-pack externals and get it working we do specify aws-sdk in the externals for example?

We are also using telemetry logs extension based layer that extracts logs and sends it to ELK stack. Wondering if that can be an issue ?


This is part of the webpack config that we have

  output: {
    libraryTarget: 'commonjs',
    filename: '[name].js',
  target: 'node',
  externals: [nodeExternals(), { 'aws-sdk': 'commonjs aws-sdk' }],

Is this going to be an problem ?

Using webpack's "externals" config should work, yes.

Are you able to put together a small reproduction case that I could try to play with? That would likely be the fastest way for me to try to find the issue.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.