2
votes

When using 'Sam local invoke'to invoke a lambda locally , which relies on a layer built locally as well, the function cannot find the module which is part of the layer.

I am getting the error "unable to import package" error while invoking my lambda locally using 'sam local invoke FUNCTIONNAME'. Those packages are already present in layer(zipped folder). Is their any way to test these lamdas locally which are having python dependency zipped into layer.

START RequestId: 083247f2-3011-428c-a529-50eba6d668f2 Version: $LATEST Unable to import module 'getnext': No module named 'apiconfig' END RequestId: 083247f2-3011-428c-a529-50eba6d668f2 REPORT RequestId: 083247f2-3011-428c-a529-50eba6d668f2 Duration: 12 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB

'apiconfig' module is already present in layer.zip

Expectation is all the layer modules should get downloaded at some temp location while doing 'sam local invoke' so that it can take all the packages and run the lamda, and hence i tried using --layer-cache-basedir DIRECTORY flag too but still getting the same error, although the docker file is getting generated at DIRECTORY path. Also,Even though i have not provided --skip-pull-image flag it is still showing requested to skip pull images. Added --force-image-build flag too error

2

2 Answers

0
votes
  • To support Lambda layers, SAM CLI replicates the AWS layer process locally by downloading all associated layers and caching them on your development machine. This happens the first time you run sam local invoke or the first time you execute your Lambda functions using sam local start-lambda or sam local start-api
  • Two specific flags in SAM CLI are helpful when you’re working with Lambda layers locally –layer-cache-basedir and –force-image-build aws docs
0
votes

For Googlers:

  • Requested to skip pull images does not matter in this case
  • Make sure your local layer is a directory, not a zip file
  • Make sure the modules exists in /opt folder by using code within lambda function
  • Make sure you are referring to the right path in template.yaml

I did the above in sequence and found it was a silly mistake.