# AWS Lambda with Webpack throws error

**URL:** https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575
**Category:** APM
**Tags:** nodejs, aws-lambda
**Created:** [November 6, 2023, 10:08pm UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575 "2023-11-06T22:08:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Myths](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myths/32/127291_2.png) [@Myths](https://discuss.elastic.co/u/Myths)
#### Post date: [November 6, 2023, 10:08pm UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575/1 "2023-11-06T22:08:12Z")

</div>

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

> **[Monitoring AWS Lambda Node.js Functions | APM Node.js Agent Reference \[4.x\] |...](https://www.elastic.co/guide/en/apm/agent/nodejs/current/lambda.html)**

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 ?

```auto
	2023-11-06T21:39:03.556+00:00
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	} 

```

---

<div class="post-metadata">

### Author: ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)
#### Post date: [November 6, 2023, 10:48pm UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575/2 "2023-11-06T22:48:14Z")

</div>

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](https://www.elastic.co/guide/en/apm/agent/nodejs/current/lambda.html) _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](https://www.elastic.co/guide/en/apm/agent/nodejs/current/starting-the-agent.html#start-bundlers) 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.

---

<div class="post-metadata">

### Author: ![Myths](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myths/32/127291_2.png) [@Myths](https://discuss.elastic.co/u/Myths)
#### Post date: [November 7, 2023, 8:29am UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575/3 "2023-11-07T08:29:23Z")

</div>

Hi,

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 ?

Thanks

---

<div class="post-metadata">

### Author: ![Myths](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myths/32/127291_2.png) [@Myths](https://discuss.elastic.co/u/Myths)
#### Post date: [November 7, 2023, 11:14am UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575/4 "2023-11-07T11:14:35Z")

</div>

This is part of the webpack config that we have

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

```

Is this going to be an problem ?

---

<div class="post-metadata">

### Author: ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)
#### Post date: [November 7, 2023, 6:49pm UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575/5 "2023-11-07T18:49:07Z")

</div>

> [@Myths](#):
>
> Can i specify the package in web-pack externals and get it working we do specify **aws-sdk** in the externals for example?

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 5, 2023, 6:49pm UTC](https://discuss.elastic.co/t/aws-lambda-with-webpack-throws-error/346575/6 "2023-12-05T18:49:21Z")

</div>

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