VOOZH about

URL: https://repost.aws/questions/QUPHB8rlEMSHaV0jixhlNwNw/lambda-layer-unable-to-import-module-no-module-named-opensearchpy

⇱ Lambda layer: "Unable to import module: No module named 'opensearchpy'" | AWS re:Post


Skip to content

Lambda layer: "Unable to import module: No module named 'opensearchpy'"

0

I am creating a python lambda with lambda layer for the opensearch module dependency.

Locally, I pip installed opensearch-py into lambdalayerstuff/opensearchpyFolder

Here's my CDK code:

const lambdaLayerOpensearchpy = new lambda.LayerVersion(this, 'layer_3', {
 code: lambda.Code.fromAsset('./lambdalayerstuff/opensearchpyFolder')
 })


// lambda
 const lambda1 = new lambda.Function(this, "lambda_1", {
 vpc: vpc,
 vpcSubnets: {
 subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS
 },
 memorySize: 512, // MB
 runtime: lambda.Runtime.PYTHON_3_8,
 code: lambda.Code.fromAsset(path.join(__dirname, '../src/lambda')),
 handler: 'lambdacode.handler',
 layers: [lambdaLayerOpensearchpy],
 timeout: cdk.Duration.seconds(120) 
 })

Please advise on what changes I need to allow my python lambda to use opensearchpy package. Thank you.

1 Answer
  • Newest
  • Most votes
  • Most comments
Are these answers helpful? Upvote the correct answer to help the community benefit from your knowledge.
0

opensearchpyFolder Needs to installed in a folder called python

So it will look like /python/opensearchpyFolder

Zip up the python folder recursive so it looks the same in the zip

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • richar
    2 years ago

    After creating the zip, how do I reference it in the code path for the CDK layer creation?

    const lambdaLayerOpensearchpy = new lambda.LayerVersion(this, 'layer_3', { code: lambda.Code.fromAsset('./lambdalayerstuff/opensearchpyFolder') })

  • richar
    2 years ago

    I tried this, but still encountering the same error:

    const lambdaLayerOpensearchpy = new lambda.LayerVersion(this, 'layer_3', {
     code: lambda.Code.fromAsset('./python.zip')
    })
    
  • Gary Mclean EXPERT
    2 years ago

    You don’t need to reference the location of the code. You just import the library

    Eg

    Import opensearchlibrary