'install library in python lambda

This is how my folder structure looks like. I want to use the requests library in my Python lambda function. I have installed the library within the package folder.

enter image description here

I am creating a zip like this. But how can I include the library from /packages into the lambda too? 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,
    }

Currently, my Lambda function gives this error:

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

I also tried installing the library in the root src/ folder instead of packages. However, I still got the same error so I guess I am zipping incorrectly perhaps?



Solution 1:[1]

You can try following commands which worked for me.

pip install --target ./package <module name>
pip install --target ./package requests
cd package

Place your trigger_bitbucket_pipeline_from_s3.py file to root folder in package/

zip -r ../my-deployment-package.zip .

Hope it helps Thank you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Smit Parmar