Skip to content

SAM Python - layer dependency modules missing from local container

0

I am attempting to locally debug a python Lambda function that uses layers for both a custom python core utility file and associated dependencies.

My project structure looks like:

.aws-sam
├── CoreUtilityLayer 
│   └── python
│   │   └── core.py
├── DependencyLayer
│   └── requirements.txt
├── LambdaFunction
│   └── app.py
└── template.yaml
└── build.toml

After running sam build, .aws-sam indeed contains a /build folder containing CoreUtilityLayer/python with core.py and DependencyLayer/python with folders of all the dependencies (ie /boto3, /requests, etc). The issue that I'm seeing locally is that none of the dependencies from the DependencyLayer/requirements.txt file are being copied over to the /opt or /opt/python folder of the locally running container which I inspect via docker during a breakpoint. The /opt/python folder DOES contain core.py.

I don't believe my template file is the issue, but can share snippets of that if helpful. I'll note that I am using the Metadata.BuildMethod=python3.13. I don't believe it's an issue because I can sam build/sam deploy and the LambdaFunction works as expected when it is live in AWS.

Any thoughts on what I'm missing? Thanks in advance.

1 Answer
0

The issue you're experiencing with missing dependencies in the local container during debugging is likely related to how AWS SAM handles layers locally. When running and debugging applications locally with AWS SAM, layers are handled differently compared to when they're deployed in the AWS cloud environment.

Here are a few points to consider:

  1. Local Layer Caching: When you run your application locally, AWS SAM downloads and caches the layers on your local host. This caching mechanism might be causing the issue you're seeing.

  2. Layer Content Location: For Python Lambda functions, the layer content should be loaded into the /opt directory of the execution environment. Specifically, the dependencies should be in the following paths:

    • /opt/python
    • /opt/python/lib/python3.x/site-packages
  3. BuildMethod: You mentioned using Metadata.BuildMethod=python3.13. This is correct for building the layer, but it doesn't affect how the layers are loaded locally.

  4. Local Debugging: When debugging locally, AWS SAM might not perfectly replicate the AWS Lambda environment, especially when it comes to layers.

To troubleshoot this issue:

  1. Ensure your template.yaml file correctly defines the layers and their content locations.

  2. Try using the sam local invoke command with the --debug flag to get more detailed information about how SAM is handling the layers.

  3. Check if the dependencies are being correctly packaged into your layer .zip file. The dependencies should be in the python/lib/python3.x/site-packages directory within the .zip file.

  4. Consider using a custom Makefile to build your layer if you're not already doing so. This can give you more control over how the dependencies are packaged.

  5. If the issue persists, you might need to manually copy the dependencies into the appropriate directory within the container during local debugging. This isn't ideal, but it could be a workaround while you investigate further.

Remember, even if your function works correctly when deployed to AWS, local debugging can sometimes behave differently due to the complexities of replicating the AWS Lambda environment on a local machine.

If these steps don't resolve the issue, you may need to reach out to AWS support or consult the AWS SAM documentation for any recent updates or known issues related to local debugging with layers.

Sources
Debug your serverless application with AWS SAM - AWS Serverless Application Model
Working with layers for Python Lambda functions - AWS Lambda
Locally invoke Lambda functions with AWS SAM - AWS Serverless Application Model
Building Lambda layers in AWS SAM - AWS Serverless Application Model

answered 2 years ago

AWS
SUPPORT ENGINEER

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.