question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@aws-cdk/aws-lambda-nodejs doesn't build functions with native modules

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

15reactions
makotoshimazucommented, Mar 30, 2021

FWIW, let me share my workaround of this issue for future reference. I didn’t intend to use pg-native, so set it to the externalModules settings as follows.

    const lambda = new NodejsFunction(this, 'Function', {
      entry: 'lambda/src/index.ts',
      handler: 'handler',
      allowAllOutbound: true,
      bundling: {
        // pg-native is not available and won't be used. This is letting the
        // bundler (esbuild) know pg-native won't be included in the bundled JS
        // file.
        externalModules: ['pg-native']
      }
    });
4reactions
eladbcommented, Feb 19, 2020

@jogold I think we should add support for building the function inside a Lambda docker image (like/with “sam build”). What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS 14.x - Native AWS Lambda Import/Export Support
It appears that since yesterday there finally is native support for the ES6 module syntax in Node 14 lambdas - see ...
Read more >
AWS CDK - aws-lambda-nodejs Module (updated)
Whenever you run a cdk deploy or cdk synth , this module will bundle your Lambda functions and output them to your cdk....
Read more >
Package your NodeJS Lambda functions individually with ...
Learn about packaging your NodeJS-based AWS Lambda functions with esbuild for improved cold start times and reduced deployment package size.
Read more >
aws-cdk-lib.aws_lambda_nodejs module - Amazon.com
The NodejsFunction construct creates a Lambda function with automatic transpiling and ... This is usually the case with modules using native dependencies.
Read more >
AWS Lambda Nodejs: Simplified 101 - Learn - Hevo Data
Use npm to include libraries that aren't part of the AWS SDK for JavaScript in your deployment package if your function requires them....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found