I am new to AWS and Elasticsearch/Opensearch, and I am having some issues.
I have a Lambda based on python (v3.12) that previously worked with Elasticsearch. My client wants to use Opensearch instead, so i downloaded the library "opensearch-py 2.6.0" and replaced my former library folder 'elasticsearch' with the folder 'opensearchpy'.
In my code, I replaced the 2 references to ELasticsearch :
from opensearchpy import OpenSearch
from dicoDefault import dico as dicoDefault
from dicoDefault import defaultRegex
from dicoDefault import getRegex
from dicoDefault import indices
from dicoDefault import types
from ngram import NGram
from opensearchpy import OpenSearch
from ssl import create_default_context
import certifi
import json
import re
import os
import urllib.parse
import datetime
es = OpenSearch([os.environ['ESURL']])
def lambda_handler(event, context):
inputs = event['queryStringParameters']
In the Tester panel on AWS, I run the following query :
{
"httpMethod": "GET",
"queryStringParameters": {
"q": "Marseille"
}
}
However, when I run tests, I have the following error message :
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'events'",
"errorType": "Runtime.ImportModuleError",
"requestId": "e03d61d5-fa1f-424f-a917-1c60e562b3a4",
"stackTrace": []
}
I tried to make some prints, but it seems the function 'lambda_handler' is not even run. I did not have this problem with Elasticsearch. So I do not think it has to do with a missing file.
From what I've seen, it seems to be a problem related to file versions. I thought maybe there is some file that is not up to date somewhere in the opensearchpy library ?
Does anyone have some insight into this ?
Problem solved.
In an empty folder, I created a file "requirements.txt" containing the following text :
opensearch-py==2.6.0
Then, in a command prompt, I run the following command line :
pip install -r requirements.txt -t python/
That command created a new folder "python" and created all the necessary libraries for Opensearchpy to work in that folder.
I added my AWS python files in that folder, compressed the files in a ZIP and replaced the former Lambda with it.
And magic, it works !
Meaning that when migrating from the python elasticsearch 6.3.1 library to the python opensearch 2.6.0 library, you have to :