Runtime.ImportModuleError: Unable to import module (lambda)
Asked Answered
S

2

3

When I check the cloud watch logs of my Lambda function, I see theses errors:

[ERROR] Runtime.ImportModuleError: Unable to import module 'trigger_bitbucket_pipeline_from_s3': No module named 'requests'

File structure:

/bin
    --trigger_bitbucket_pipeline_from_s3.zip
/src
   --trigger_bitbucket_pipeline_from_s3.py
   --/requests (lib folder)
   

lambda.tf

Lambda.tf:

data "archive_file" "lambda_zip" {
  type             = "zip"
  source_file      = "${path.module}/src/trigger_bitbucket_pipeline_from_s3.py"
  output_file_mode = "0666"
  output_path      = "${path.module}/bin/trigger_bitbucket_pipeline_from_s3.zip"
}

resource "aws_lambda_function" "processing_lambda" {
  filename         = data.archive_file.lambda_zip.output_path
  function_name    = "triggering_pipleline_lambda"
  handler          = "trigger_bitbucket_pipeline_from_s3.lambda_handler"
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
  role             = aws_iam_role.processing_lambda_role.arn

  runtime = "python3.9"

}

My lambda function in src/trigger_bitbucket_pipeline_from_s3.py is pretty straightforward for now:

import logging
import requests

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):

    logger.info(f'## EVENT: {event}')


    return {
        'statusCode': 200,
    }

What am I doing wrong? I have already double checked file names.

Swaddle answered 18/5, 2022 at 21:1 Comment(0)
E
1

That is because there is no module named 'requests' in lambda, remembrer lambda is serverless so you need to configure all your dependencies before you run it.

One way to solve this is to install that dependency locally in your project:

pip install requests -t ./

Then create again the .zip file (with the dependency in it) and upload to your lambda function.

And other way to solve it is to use a custom layer in AWS lambda that contains the relevant 'requests' site-packages you require. Example:

https://dev.to/razcodes/how-to-create-a-lambda-layer-in-aws-106m

Endocranium answered 19/5, 2022 at 0:53 Comment(2)
when you says create again the .zip file (with the dependency in it) what do you actually mean? The zip file is automatically created when I run terraform apply. In this case however, no new changes are made to my terraform infrastructure even though I installed the library as you suggested aboveSwaddle
@Swaddle maybe the following medium post will help you a bit: alek-cora-glez.medium.com/…Endocranium
B
0

You typically receive this error when your Lambda environment can't find the specified library in the Python code. This is because Lambda isn't prepackaged with all Python libraries.

To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python code for Lambda.

Make sure that you put the library that you import for Python inside the /python folder.

In your local environment install all library files into the python folder by running the following:

pip install librarywhatyouneed -t python/

There are dependencies to create all python libraries prepackaged and zip the python with all dependencies and put into the layer associated with add Layer lambda created on AWS.

enter image description here

Barham answered 2/2, 2023 at 12:28 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.