Created
September 2, 2021 00:26
-
-
Save ystoneman/60c6496b5fae1a33c14f34c71b2dfdfc to your computer and use it in GitHub Desktop.
Python sample API calls to AWS ElasticSearch using AWS4Auth to sign requests
This file contains 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
# Author: https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html#es-request-signing-python | |
from elasticsearch import Elasticsearch, RequestsHttpConnection | |
from requests_aws4auth import AWS4Auth | |
import boto3 | |
host = 'search-testing-mvevysr2i4aen6pqfq5ympghsy.us-east-1.es.amazonaws.com' # For example, my-test-domain.us-east-1.es.amazonaws.com | |
region = 'us-east-1' # e.g. us-west-1 | |
service = 'es' | |
credentials = boto3.Session().get_credentials() | |
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token) | |
es = Elasticsearch( | |
hosts = [{'host': host, 'port': 443}], | |
http_auth = awsauth, | |
use_ssl = True, | |
verify_certs = True, | |
connection_class = RequestsHttpConnection | |
) | |
document = { | |
"title": "Moneyball", | |
"director": "Bennett Miller", | |
"year": "2011" | |
} | |
es.index(index="movies", doc_type="_doc", id="5", body=document) | |
print(es.get(index="movies", doc_type="_doc", id="5")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment