Lambda layer: "Unable to import module: No module named 'opensearchpy'"
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.
- Language
- English
asked 2 years ago593 views
- Newest
- Most votes
- Most comments
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
- richar2 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') })
- richar2 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 EXPERT2 years ago
You donβt need to reference the location of the code. You just import the library
Eg
Import opensearchlibrary
Relevant content
- Accepted Answer
asked a year ago
