1
votes

I have a simple Python Code that uses Elasticsearch module "curator" to make snapshots. I've tested my code locally and it works.

Now I want to run it in an AWS Lambda but I have this error :

Unable to import module 'lambda_function': No module named 'error'

Here is how I proceeded :

I created manually a Lambda and gave it a "AISA-BasicLambdaExecutionRole" role. Then I created my package with my function and the dependencies that I installed with the command :

pip install elasticsearch-curator -t /<path>/myRepository 

I zipped the content (not the folder) and uploaded it in my Lambda. I changed the Handler name to "lambda_function.lambda_handler" (my function's name is "lambda_function.py").

Did I miss something ? This is my first time working with Lambda and Python

I've seen the other questions about this error :

"errorMessage": "Unable to import module 'lambda_function'"

But nothing works for me.

EDIT :

Here is my lambda_function :

from __future__ import print_function
import curator
import time
from curator.exceptions import NoIndices
from elasticsearch import Elasticsearch

def lambda_handler(event, context):



  es = Elasticsearch()

  index_list = curator.IndexList(es)

  index_list.filter_by_regex(kind='prefix', value="logstash-")

  Number = 1

  try:
    while Number <= 3:

        Name="snapshotLmbd_n_"+ str(Number) +""

        curator.Snapshot(index_list, repository="s3-backup", name= Name , wait_for_completion=True).do_action()     
        Number += 1

        print('Just taking a nap ! will be back soon')
        time.sleep(30)

  except KeyboardInterrupt:
        print('My bad ! I interrupted this')
        return

Thank you for your time.

1
Do you have import error of from error import ... in your lambda_function.py by any chance?ElmoVanKielmo
@ElmoVanKielmo Thank you for your fast response. No I do not import error. I edited my post to add the function.AsmaaM
Check if your python script has executable permissions. chomd 755omuthu
@omuthu This was actually the problem ! Even though I set permissions to 755, after the upload to aws, apparently the permissions are lost. What I did actually is use serverless instead to avoid completely this problem. Can you write the answer so I can accept it ? Thank you again for the insightAsmaaM

1 Answers

1
votes

Ok, since you have everything else correct, check for the permissions of the python script.

It must have executable permissions (755)