# Deploying Pyton Lambda in Serverless but Lambda can't find it

**URL:** <https://forum.serverless.com/t/deploying-pyton-lambda-in-serverless-but-lambda-cant-find-it/19000>\
**Category:** Serverless Framework\
**Created:** [June 16, 2023, 1:52am UTC](https://forum.serverless.com/t/deploying-pyton-lambda-in-serverless-but-lambda-cant-find-it/19000 "2023-06-16T01:52:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vesnathan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/vesnathan/32/7161_2.png) [@vesnathan](https://forum.serverless.com/u/vesnathan)\
**Post date:** [June 16, 2023, 1:52am UTC](https://forum.serverless.com/t/deploying-pyton-lambda-in-serverless-but-lambda-cant-find-it/19000/1 "2023-06-16T01:52:46Z")

</div>

```auto
serverless.yaml

provider:
  name: aws
  runtime: python3.8

...

plugins:
  - serverless-python-requirements

...
functions:
    handler: lambda_function.lambda_handler
    events:
      - http:
          path: ftaQaInference
          method: post
          cors: true

```

The filepath is definately correct. In src/backend/ lambda\_function.py I have

```auto
import json
import boto3
import torch
from transformers import RobertaTokenizer, RobertaForSequenceClassification

# Set up the S3 client
s3 = boto3.client('s3')

# Load the model from S3
bucket_name = 'sagemaker-fta'
model_key = 'model.pt'
model_path = '/'
s3.download_file(bucket_name, model_key, model_path)

# Load the model
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaForSequenceClassification.from_pretrained(model_path)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
model.eval()

def lambda_function(event, context):
    # Extract the input from the event
    input_data = json.loads(event['body'])
    question = input_data['question']
    document_text = input_data['document_text']
    answer = input_data['answer']

    # Preprocess the input
    input_text = f"{question} {document_text} {answer}"
    encoding = tokenizer.encode_plus(
        input_text,
        add_special_tokens=True,
        truncation=True,
        padding='max_length',
        max_length=128,
        return_tensors='pt'
    )
    input_ids = encoding['input_ids'].to(device)
    attention_mask = encoding['attention_mask'].to(device)

    # Perform inference
    with torch.no_grad():
        logits = model(input_ids, attention_mask=attention_mask).logits
        probabilities = torch.softmax(logits, dim=1)
        predicted_labels = torch.argmax(probabilities, dim=1)

    # Prepare the response
    response_body = {
        'predicted_label': predicted_labels.item(),
        'probabilities': probabilities.tolist()[0]
    }
    response = {
        'statusCode': 200,
        'body': json.dumps(response_body)
    }

    return response

```

And a requirements.txt file with:

```auto
boto3==1.26.154
torch==2.0.1

```

When I invoke the function, in CloudWatch I get:

```auto
[ERROR] ModuleNotFoundError: No module named 'lambda_function'
Traceback (most recent call last):
  File "/var/task/serverless_sdk/ __init__.py", line 144, in wrapped_handler
    return user_handler(event, context)
  File "/var/task/s_ftaQaInference.py", line 25, in error_handler
    raise e
  File "/var/task/s_ftaQaInference.py", line 20, in <module>
    user_handler = serverless_sdk.get_user_handler('lambda_function.lambda_handler')
  File "/var/task/serverless_sdk/ __init__.py", line 56, in get_user_handler
    user_module = import_module(user_module_name)
  File "/var/lang/lib/python3.7/importlib/ __init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked

```

Can anyone spot what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![woodjosh256](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/woodjosh256/32/7642_2.png) [@woodjosh256](https://forum.serverless.com/u/woodjosh256)\
**Post date:** [September 10, 2023, 4:17am UTC](https://forum.serverless.com/t/deploying-pyton-lambda-in-serverless-but-lambda-cant-find-it/19000/2 "2023-09-10T04:17:50Z")

</div>

Did you ever figure this out? I’ve been struggling for so long trying to import another package in my lambda function.

---

<div class="post-metadata">

**Author:** ![vesnathan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/vesnathan/32/7161_2.png) [@vesnathan](https://forum.serverless.com/u/vesnathan)\
**Post date:** [September 11, 2023, 3:34am UTC](https://forum.serverless.com/t/deploying-pyton-lambda-in-serverless-but-lambda-cant-find-it/19000/3 "2023-09-11T03:34:38Z")

</div>

Nah couldn’t get it to work. Ended up using AWS SAM CLI and Docker container.
