3

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

Surbhi gupta
  • 31
  • 1
  • 3
  • I used this to run the function locally: sam local invoke --layer-cache-basedir ./.aws-sam HelloWorldFunction - Not really sure if it's best practice but it's what worked so far. – Alex Nov 14 '22 at 04:21

2 Answers2

0
  • 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
amittn
  • 2,249
  • 1
  • 12
  • 19
  • >sam local invoke --no-event --layer-cache-basedir Getnext --force-image-build GetNextFunction Invoking getnext.getnext_handler (python3.6) 2019-10-09 06:45:40 Found credentials in shared credentials file: ~/.aws/credentials Workflowlayer is a local Layer in the template Building image... Requested to skip pulling images ... Mounting C:\Users\skumari02\Documents\a_source\workflowapis\workflowapi\Getnext as /var/task:ro,delegated inside runtime container START RequestId: 21f943b9-3fab-45e6-a3f6-4e53e8f2e74a Version: $LATEST Unable to import module 'getnext': No module named 'apiconfig' – Surbhi gupta Oct 09 '19 at 01:36
  • Thanks @amittn ! But i already tried these two flags and a new dockerfile is getting generated at the --layer-cache-basedir path, but still got the same error. Also,Even though i have not provided --skip-pull-image flag it is still showing requested to skip pull images. I am not able to find if i am doing something wrong – Surbhi gupta Oct 09 '19 at 01:38
-1

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.

dz902
  • 4,782
  • 38
  • 41