Local AWS Lambda debugging with layers

Viewed 1199

I created a SAM application and want to debug it locally. The app has some dependencies which are specified in a Lambda Layer. This lambda layer is implemented and tested with other funtions - it runs ok.

I specified the need of this layer in templates.json:

Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7
      Layers:  arn:aws:l......:2

But when I Debug Locally I get the error that the dependencies do not exist in the Docker container that is being mounted locally. In this case is sqlalchemy:

Fetching lambci/lambda:python3.7 Docker container image......
Mounting C:\.....\Temp\aws-toolkit-vscode\...\output\awsToolkitSamLocalResource as /var/task:ro,delegated inside runtime container
START RequestId: ...... Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'app___vsctk___debug': No module named 'sqlalchemy'

If I specify the dependencies in requirements.txt it works fine, but I understand that if I do this, the dependencies will be included in the deployment package, which I dont want.

How can I debug under a docker image that collects the dependencies of a layer?

1 Answers

According to the docs,

Layers are extracted to the /opt directory in the function execution environment. Each runtime looks for libraries in a different location under /opt, depending on the language. Structure your layer so that function code can access libraries without additional configuration.

You can try to simulate this locally with your docker image by mounting the content of your layer into the proper subfolder of /opt. For Python, the right place to mount seems to be /opt/python.

Related