def handler()

in graviton2/elasticsearch_graviton/lambdas/insert_into_index.py [0:0]


def handler(event, context):
    service = 'es'
    credentials = boto3.Session().get_credentials()
    awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
    
    fake = Faker()
    
    es = Elasticsearch(
        hosts = [{'host': endpoint, 'port': 443}],
        http_auth = awsauth,
        use_ssl = True,
        verify_certs = True,
        connection_class = RequestsHttpConnection
    )
    
    attempts = 20
    print("Will insert %s items into the index" % attempts)
    for i in range(attempts):
        document = fake.profile()
        
        try:
            result = es.index(index="people", doc_type="_doc", body=document)
            print("Indexed with ID '%s'" % result['_id'])
    
        except ConnectionTimeout as e:
            print("Connection to the ES cluster timed out: %s" % str(e))
        
        except Exception as e:
            print("Unexpected exception caught: %s" % str(e))
        
        time.sleep(2)