慕桂英3389331
import boto3import jsonfrom requests_aws4auth import AWS4Authfrom elasticsearch import Elasticsearch, RequestsHttpConnectionsession = boto3.session.Session()credentials = session.get_credentials()awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, session.region_id, 'es', session_token=credentials.token)es = Elasticsearch( ['https://xx.amazonaws.com'], http_auth=awsauth, use_ssl=True, verify_certs=True, connection_class=RequestsHttpConnection)def lambda_handler(event, context): es.cluster.health() es.indices.delete(index='data', ignore=[400, 404]) es.indices.create(index='data', ignore=400) r = [{'id': '1', 'data': 'Health'}, {'id': '2', 'data': 'countries'}, {'id': '3', 'data': 'currency'}, {'id': '4', 'data': 'language'}] for e in enumerate(r): es.index(index="data", body=e[1]) result = es.search(index="data", body={"query": {"match_all": {}}}) return{ 'statusCode': 200, #'body': json.dumps('API INVOKES!') 'body':result }