Search code examples
pythonamazon-web-servicesaws-lambda

How to create a layer in lambda function


Steps followed to create

  1. Create a virtual environment and activate it
  2. pip install elasticsearch
  3. Zip the folder inside site-packages.zip

4.Create Layer in AWS (say name is elastic)

  1. Add the code below in lambda_handler and add the Layer elastic

Below is code

import json
from elasticsearch import Elasticsearch, RequestsHttpConnection
def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Still I got "errorMessage": "Unable to import module 'lambda_function': No module named 'elasticsearch'",


Solution

  • If I may, I would like to recommend an alternative technique which has never failed me. The technique includes docker tool described in the recent AWS blog:

    Thus for this question, I verified it using elasticsearch as follows:

    1. Create empty folder, e.g. mylayer.

    2. Go to the folder and create requirements.txt file with the content of

    elasticsearch
    
    1. Run the following docker command (may adjust python version to your needs):
    docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
    
    1. Create layer as zip:
    zip -r elastic.zip python > /dev/null
    
    1. Create lambda layer based on elastic.zip in the AWS Console. Don't forget to specify Compatible runtimes to python3.8.

    2. Test the layer in lambda using the following lambda function:

    import json
    
    from elasticsearch import Elasticsearch, RequestsHttpConnection
    
    def lambda_handler(event, context):
        # TODO implement
        
        print(dir(Elasticsearch))
        
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from Lambda!')
        }
    

    The function executes correctly:

    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bulk', 'clear_scroll', 'close', 'count', 'create', 'delete', 'delete_by_query', 'delete_by_query_rethrottle', 'delete_script', 'exists', 'exists_source', 'explain', 'field_caps', 'get', 'get_script', 'get_script_context', 'get_script_languages', 'get_source', 'index', 'info', 'mget', 'msearch', 'msearch_template', 'mtermvectors', 'ping', 'put_script', 'rank_eval', 'reindex', 'reindex_rethrottle', 'render_search_template', 'scripts_painless_execute', 'scroll', 'search', 'search_shards', 'search_template', 'termvectors', 'update', 'update_by_query', 'update_by_query_rethrottle']