1 回答
TA贡献2036条经验 获得超8个赞
import boto3
import json
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_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
}
添加回答
举报