简体   繁体   中英

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

I am deploying a lambda function using CDK python.

This is my stack for the lambdas:

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.

I can import this lambda layer without problems using this import:

from response_schema import Response

This is my layer python code:

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.

The lambda layer structure folder is:

在此处输入图像描述

In the requirements.txt file I have:

pydantic==1.10.4

But it seems that is not installing the pydantic library in my lambda layer. I have tried to install the library in the lambda layer folder using:

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?

If you want to install Python packages from the requirements.txt file, you can use aws_cdk.aws_lambda_python_alpha.PythonFunction construct. In this case you need to replace your LayerVersion construct with PythonLayerVersion construct.

If you want to use Function and LayerVersion constructs, you need to download libraries to your project. You can use this article as a reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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