简体   繁体   English

无法在 lambda 层中安装库并在 lambda 层自定义脚本中使用它

[英]Cannot install a library in lambda layer and use it in lambda layer custom script

I am deploying a lambda function using CDK python.我正在使用 CDK python 部署一个 lambda function。

This is my stack for the lambdas:这是我的 lambda 堆栈:

import os
from aws_cdk import (
    aws_stepfunctions as _aws_stepfunctions,
    aws_stepfunctions_tasks as _aws_stepfunctions_tasks,
    aws_lambda,
    App, Duration, Stack,
    aws_ec2 as ec2,
    aws_sns as sns,
    aws_sns_subscriptions as sns_subs,
    aws_iam as iam,
)


class LambdaStack(Stack):
    def __init__(self, app: App,
                 id: str,
                 upload_image_bucket,
                 **kwargs) -> None:
                     
        super().__init__(app, id, **kwargs)
        
        
        schema_response_layer = aws_lambda.LayerVersion(self, 'lambda-layer',
                  code = aws_lambda.AssetCode('lambdas/lambda_layers/schema_response_layer/'),
                  compatible_runtimes = [aws_lambda.Runtime.PYTHON_3_9],
                  layer_version_name="schema_response_layer"
        )
        
        
        policy_textract = iam.PolicyStatement( # Restrict to listing and describing tables
                        actions=[   "textract:AnalyzeDocument",
                                    "textract:DetectDocumentText",
                                    "textract:GetDocumentAnalysis",
                                    "textract:GetDocumentTextDetection",
                                    "textract:AnalyzeExpense"],
                        resources=["*"]
        )

        store_image = aws_lambda.Function(
            self, 'store_imagey',
            function_name="storage_image_test_1",
            runtime=aws_lambda.Runtime.PYTHON_3_9,
            code=aws_lambda.Code.from_asset('lambdas/lambda_functions/store_image'),
            handler='store_image.store_image_handler',
            environment={
                            'BUCKET_NAME': upload_image_bucket.bucket_name,
                         },
            initial_policy=[policy_textract],
            layers=[schema_response_layer]
        )
        
        upload_image_bucket.grant_read_write(store_image)
        
        self.store_image_ld = store_image

as you can see, I am creating a lambda layer, that I want to use in my store_image function.如您所见,我正在创建一个 lambda 层,我想在我的 store_image function 中使用它。

I can import this lambda layer without problems using this import:我可以使用此导入毫无问题地导入此 lambda 层:

from response_schema import Response

This is my layer python code:这是我的层 python 代码:

from pydantic import BaseModel, Field, validator

class Headers(BaseModel):
    content_type: str = "application/json"
    access_control: str =  "*"
    allow = "GET, OPTIONS, POST"
    access_control_allow_methods: str  = "*"
    access_control_allow_headers: str  = "*"
    
    
class Response(BaseModel):
    status_code: str = "200"
    body: str
    headers: Headers = Headers()

I am getting the following error:我收到以下错误:

Runtime.ImportModuleError: Unable to import module 'store_image': No module named 'pydantic'

I don't know how to install the pydantic library in my lambda layer and use this library in the lambda layer code.我不知道如何在我的 lambda 层中安装 pydantic 库并在 lambda 层代码中使用该库。

The lambda layer structure folder is: lambda层结构文件夹为:

在此处输入图像描述

In the requirements.txt file I have:requirements.txt文件中我有:

pydantic==1.10.4

But it seems that is not installing the pydantic library in my lambda layer.但似乎没有在我的 lambda 层中安装 pydantic 库。 I have tried to install the library in the lambda layer folder using:我尝试使用以下方法将库安装在 lambda 图层文件夹中:

pip install -t . pydantic==1.10.4

But is not working neither.但是也不起作用。

How can I install a library in my lambda layer and use it in my lambda layer custom script?如何在我的 lambda 层中安装库并在我的 lambda 层自定义脚本中使用它?

If you want to install Python packages from the requirements.txt file, you can use aws_cdk.aws_lambda_python_alpha.PythonFunction construct.如果要从 requirements.txt 文件安装 Python 包,可以使用aws_cdk.aws_lambda_python_alpha.PythonFunction构造。 In this case you need to replace your LayerVersion construct with PythonLayerVersion construct.在这种情况下,您需要将 LayerVersion 构造替换为PythonLayerVersion构造。

If you want to use Function and LayerVersion constructs, you need to download libraries to your project.如果要使用 Function 和 LayerVersion 构造,则需要将库下载到项目中。 You can use this article as a reference.您可以将本文作为参考。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM