Last active
October 20, 2020 08:26
-
-
Save jcbf/5dd822969d56d7ace485fa169904e786 to your computer and use it in GitHub Desktop.
Make queries to Elasticsearch from a lambda in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run get info from Elasticsearch from AWS Lambda. | |
from __future__ import print_function | |
import boto3 | |
import certifi | |
import yaml | |
from aws_requests_auth.aws_auth import AWSRequestsAuth | |
from elasticsearch import Elasticsearch, RequestsHttpConnection | |
def handler(event, context): | |
# For this function, we don't care about 'event' and 'context', | |
# but they need to be in the function signature anyway. | |
esinfo = {} | |
esendpoint = 'search-yyyyyyy-xxxxxx.rrrrrrr.es.amazonaws.com' | |
session = boto3.session.Session() | |
credentials = session.get_credentials().get_frozen_credentials() | |
awsauth = AWSRequestsAuth( | |
aws_access_key=credentials.access_key, | |
aws_secret_access_key=credentials.secret_key, | |
aws_token=credentials.token, | |
aws_host=esendpoint, | |
aws_region=session.region_name, | |
aws_service='es' | |
) | |
es = Elasticsearch( | |
hosts=[{'host': esendpoint, 'port': 443}], | |
http_auth=awsauth, | |
use_ssl=True, | |
verify_certs=True, | |
ca_certs=certifi.where(), | |
connection_class=RequestsHttpConnection | |
) | |
esinfo = es.info(); | |
print(esinfo) | |
return esinfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."
}
Below is the code,I am running into the above issue. Please let me know if you find any issue with the code.
import boto3
import certifi
from elasticsearch import Elasticsearch, RequestsHttpConnection
from aws_requests_auth.aws_auth import AWSRequestsAuth
import json
session = boto3.session.Session()
print(session)
credentials = session.get_credentials().get_frozen_credentials()
print(credentials)
es_host = 'https://search-myelasticsearch-url:443'
awsauth = AWSRequestsAuth(
aws_access_key=credentials.access_key,
aws_secret_access_key=credentials.secret_key,
aws_host=es_host,
aws_region='us-east-1',
aws_service='es')
def getdata(event, context):
es_client = Elasticsearch('https://search-dcoupoasdfasdfasdfasfasfasdf-easasdfasfdsfsaws.com:443',
connection_class=RequestsHttpConnection,
use_ssl = True,
verify_certs=True,
ca_certs=certifi.where(),
http_auth=awsauth)
es_info = es_client.info()
return es_info