aws-cdk: @aws-cdk/aws-lambda-nodejs doesn't build functions with native modules
I’m trying to get a lambda set up with @aws-cdk/aws-lambda-nodejs.
I applied the change mentioned in #6274, and now I have a build error:
Error: Failed to build file at [my lambda function]: Error: 🚨 [project path]/node_modules/pg/lib/native/client.js:11:21: Cannot resolve dependency ‘pg-native’
Reproduction Steps
Use "pg": "7.18.1", then in the source of the lambda:
import * as pg from 'pg';
Error Log
Error: Failed to build file at [my lambda function]: Error: 🚨 [project path]/node_modules/pg/lib/native/client.js:11:21: Cannot resolve dependency ‘pg-native’
Environment
- CLI Version : 1.24.0
- Framework Version: 1.24.0
- OS : OS X 10.15.3
- Language : Typescript
Other
I’m familiar with how to work around this error in Webpack, but given that Parcel is a zero config tool, I’m not sure what to do about this error.
I think connecting to RDS databases from Node Lambdas will be a common use case, so this should be supported out of the box if possible, or at least have a documented solution.
This is 🐛 Bug Report
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 19 (8 by maintainers)
Commits related to this issue
- feat(lambda-nodejs): external and install modules Add support for: * external modules: modules that should not be bundled. Defaults to 'aws-sdk'. Can be used for modules already included in a Layer. ... — committed to jogold/aws-cdk by jogold 4 years ago
- feat(lambda-nodejs): external and install modules Add support for: * external modules: modules that should not be bundled. Defaults to 'aws-sdk'. Can be used for modules already included in a Layer. ... — committed to jogold/aws-cdk by jogold 4 years ago
- feat(lambda-nodejs): external and install modules Add support for: * external modules: modules that should not be bundled. Default to `aws-skd`. * install modules: modules that should not be bundled ... — committed to jogold/aws-cdk by jogold 4 years ago
- feat(lambda-nodejs): external and install modules Add support for: * external modules: modules that should not be bundled. Defaults to `aws-sdk`. * install modules: modules that should not be bundled... — committed to jogold/aws-cdk by jogold 4 years ago
- feat(lambda-nodejs): external and install modules (#8681) Add support for: * external modules: modules that should not be bundled. Defaults to `aws-sdk`. * install modules: modules that should not... — committed to aws/aws-cdk by jogold 4 years ago
FWIW, let me share my workaround of this issue for future reference. I didn’t intend to use
pg-native, so set it to theexternalModulessettings as follows.@jogold I think we should add support for building the function inside a Lambda docker image (like/with “sam build”). What do you think?
I’m with @thekevinbrown here.
I think:
parcelis too primitive and lacks configuration. It is great if you are building a simple web app, but it falls apart on large, backend-oriented projects.Overall DX here is very sluggish and full of errors.
I think a better approach would be to go for Webpack or Rollup, with a sane default configuration but allow consumers to supply config overrides.
You can see this being used successfully in projects like Nuxt. Where
nuxt.config.tsfile exposes props (simple) and hooks (advanced) for configuration of Webpack internals, but it is not necessary to touch them for most projects, and everything just works.Likewise
@aws-cdk/aws-lambda-nodejscould automatically fix many issues, like exclusion ofaws-sdk, and ignoringpg-nativemodule.I use a slightly different project structure provided by the sam cli and ran into this issue as well. I have a template.yaml file. Is it somehow possible to add this externalModules option there?
My template.yaml: