简体   繁体   English

在 python lambda 安装库

[英]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.我想在我的 Python lambda function 中使用请求库。我已将库安装在 package 文件夹中。

在此处输入图像描述

I am creating a zip like this.我正在创建这样的 zip。 But how can I include the library from /packages into the lambda too?但是我怎样才能将 /packages 中的库也包含到 lambda 中呢? 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:我在 src/trigger_bitbucket_pipeline_from_s3.py 中的 lambda function 现在非常简单:

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:目前,我的 Lambda function 给出了这个错误:

[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.我还尝试将库安装在根 src/ 文件夹中而不是包中。 However, I still got the same error so I guess I am zipping incorrectly perhaps?但是,我仍然遇到同样的错误,所以我想我可能压缩不正确?

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/将您的trigger_bitbucket_pipeline_from_s3.py文件放在 package/ 中的根文件夹中

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

Hope it helps Thank you希望对你有帮助谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 lambda 层中安装库并在 lambda 层自定义脚本中使用它 - Cannot install a library in lambda layer and use it in lambda layer custom script 如何在 python 3.8 的 AWS lambda 上安装 Pillow? - How to install Pillow on AWS lambda for python 3.8? AWS lambda (python): Dockerfile 安装 psycopg2? - AWS lambda (python): Dockerfile to install psycopg2? 无法在 AWS 中使用 Python 的 Paramiko 库 Lambda Function - Unable to use Python's Paramiko library in AWS Lambda Function 如何为 python 安装 azure.storage.blob 库? - How to install azure.storage.blob library for python? lambda python function 带有外部库 psycopg2 - 没有名为 psycopg2 的模块 - lambda python function with external library psycopg2 - No module named psycopg2 使用 python pyodbc 库创建 AWS Lambda function 抛出错误“模块‘pyodbc’没有属性‘connect’ - Creating AWS Lambda function using python pyodbc library throw an error "module 'pyodbc' has no attribute ' connect' Lambda 未加载加密共享库 - Lambda Not Loading Cryptography Shared Library 从用 Node.js 编写的 AWS Lambda 函数调用 Python boto3 库 - Call Python boto3 library from AWS Lambda function written in Node.js 如何使用 ctypes.util.find_library 导入 AWS lambda(python)中的 so 库? - How to use ctypes.util.find_library to import .so libraries in AWS lambda (python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM