How to write a lambda handler to send the data to Elasticsearch

前端 未结 2 406
無奈伤痛
無奈伤痛 2021-01-21 19:18

Below is the code to send the data to Elasticsearch in local

r = [{\'Name\': \'Dr. Christopher DeSimone\', \'Specialised and Location\': \'Health\'},
 {\'Name\':          


        
相关标签:
2条回答
  • 2021-01-21 19:29
    from requests_aws4auth import AWS4Auth
    from elasticsearch import Elasticsearch, RequestsHttpConnection
    session = boto3.session.Session()
    credentials = session.get_credentials()
    
    awsauth = AWS4Auth(credentials.access_key,
                       credentials.secret_key,
                       session.region_name, 'es',
                       session_token=credentials.token)
    es = Elasticsearch(
        ['https://search-xxx.us-east-1.es.amazonaws.com'],
        http_auth=awsauth,
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection
    )
    
    
    def lambda_handler(event, context):
        es.cluster.health()
        es.indices.create(index='my-index', ignore=400)
        r = [{'Name': 'Dr. Christopher DeSimone', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Tajwar Aamir (Aamir)', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Bernard M. Aaron', 'Specialised and Location': 'Health'},
     {'Name': 'Eliana M. Aaron', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Joseph J. Aaron', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Michael R. Aaron', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Darryl H. Aarons', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. William B. Aarons', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Sirike T. Aasmaa', 'Specialised and Location': 'Health'},
     {'Name': 'Dr. Jacobo A. Abadi', 'Specialised and Location': 'Health'}]
        for e in enumerate(r):
             es.index(index="my-index", body=e[1])
    
    0 讨论(0)
  • 2021-01-21 19:46

    To complement @jellycsc, to run your the code on lambda you will need to bundle required packages with your lambda code.

    This means that you should prepare you lambda deployment package with the following python libraries:

    • requests-aws4auth
    • elasticsearch

    AWS docs show how to do this:

    • Updating a function with additional dependencies
    0 讨论(0)
提交回复
热议问题