aws-sam-cli: Cannot use 3d party libraries

Hello,

I am unable to run a local lambda that relies on third party libraries, for example the redis one. The error I get is:

Unable to import module ‘index’

For troubleshooting I:

  • Started sam local with debug enabled (sam local start-api--template notification.sam.yml --debug-port 5858)
  • Sent a curl request
  • Attached VSCode debugger which was probably not needed.
  • Found the docker container that spawned (docker ps and there was only one)
  • Attached an interactive shell to that container (docker exec -it <machine_alias> bash)
  • And tried to find the node_modules for my lambda, it wasn’t in the execution folder (where my index.js was) and couldn’t find it anywhere else.

I also tried with the static-dir flag (sam local start-api--template notification.sam.yml --debug-port 5858 --static-dir node_modules) but didn’t see any difference.

I think sam local takes all *.js files in the lambda root but ignores the package.json file and the node_modules folder. Can someone please let me know if Im doing something wrong or if this perhaps is expected behavior?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 19 (7 by maintainers)

Most upvoted comments

No disrespect to the AWS SAM team, but I’d recommend checking out serverless.com. It’s an open source wrapper around serverless platforms (including AWS), and it makes it incredibly easy to test Lambda functions and API Gateway endpoints. It has no issues with third party libraries. It actually uses webpack to bundle your code, so you can even use ES6 and TypeScript if you’d like.

aws-lambda-builders package: https://github.com/awslabs/aws-lambda-builders/pull/44 now has a workflow for building node serverless apps, this will eventually makes it way into sam cli and be supported here.

Currently sam build only supports python runtimes.

Where is your node_modules with respect to the CodeUri?

Usually, if CodeUri points to a directory that contains your node_modules, it gets directly mounted in the Docker container. But if you don’t have that structure, then you need some build process (gulp?) that moves code around in right structure for you. This is required anyway because Lambda cloud will expect the same structure as SAM Local.

@jfuss YES! Thank you that worked by omitting --template-file template.yaml

@ryanwhitehouse You are doing unnecessary work. When using build, your template should point to the source. sam build will produce a template behind the scenes. This template contains updated data to point the Code/CodeUri to the built location. sam local [invoke|start-api|start-lambda] and sam package are aware of this and default to the built template if available. Therefore there is no need to manually copy and no need for a DistributionUri.

We’re debugging this at the moment and we’re finding the sam build step is failing (likely another buy with sam installing pip packages without the --user parameter?) but sam-build --use-container works.

There’s a ./.aws-sam/build directory we can cd into and run the sam package step from there which packages our python code correctly that we can push to S3.

@Brett55 https://github.com/awslabs/aws-sam-cli/issues/978#issue-406800072

TLDR: When using build, don’t pass the template into the package command

@ryanwhitehouse You are doing unnecessary work. When using build, your template should point to the source. sam build will produce a template behind the scenes. This template contains updated data to point the Code/CodeUri to the built location. sam local [invoke|start-api|start-lambda] and sam package are aware of this and default to the built template if available. Therefore there is no need to manually copy and no need for a DistributionUri.