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
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 a year ago
EXPERT
reviewed a year 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') })

  • I tried this, but still encountering the same error:

    const lambdaLayerOpensearchpy = new lambda.LayerVersion(this, 'layer_3', {
      code: lambda.Code.fromAsset('./python.zip')
    })
    
  • You don’t need to reference the location of the code. You just import the library

    Eg

    Import opensearchlibrary

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.