Manish Pal
Manish Pal

Reputation: 371

How to use dependencies in sam local environment?

I am able to run lambda function locally using sam local start-api. Thats work pretty fine for me. Now I want to use libraries such as pandas etc.but it giving me error:

Invalid lambda response received: Invalid API Gateway Response Keys: {'errorMessage', 'stackTrace', 'errorType'} in {'errorMessage': 
"Unable to import module 'read_pharma': No module named 'pandas'", 'errorType': 'Runtime.ImportModuleError', 'stackTrace': []}       

Below is my code:

import boto3, json
import pandas as pd

def lambda_handler(event, context):
    return{
          'statusCode': 200, 
          'message': 'Hello World'
}

I already tried creating a virtual environment, but no luck. How can I use dependencies here?

Here is my directory:

Backend-Directory
    organisation_manag
        abc.py
        xyz.py
    user_manag
        pqr.py
        ust.py
    requirements.txt
    template.yaml

Upvotes: 1

Views: 3089

Answers (1)

xhg
xhg

Reputation: 1875

If you created your sam app using sam init, there should be a requirements.txt file in the project. Once you put pandas in it, you can run

sam build
# or sam build --use-container

sam local....

The sam build will handle the installation of dependencies.

Update

Since you updated your question description, it seems the question you had is how to share requirements.txt across two functions.

Assuming your original function handlers are abc.handler and pqr.handler

You can make your template like this:

FuncA:
  Properties:
    CodeUri: .
    Handler: organisation_manag.abc.handler

FuncB:
  Properties:
    CodeUri: .
    Handler: organisation_manag.pqr.handler

Upvotes: 1

Related Questions