Created
August 22, 2018 19:47
-
-
Save shentonfreude/a53c06ba82a1d653aad754b6b2970854 to your computer and use it in GitHub Desktop.
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
# Use the Elasticsearch python bindings to connect to Elasticsearch or AWS | |
# ElasticsearchService (ES). For plain Elasticsearch we only need host and | |
# port. For ES, we need to create an AWS RequestAuth and pass those to the | |
# instantiation. This gets the launched EC2/Lambda instances' ID, KEY -- and, | |
# for Lambda, a TOKEN. See: https://github.com/davidmuller/aws-requests-auth | |
# Another approach is shown here: | |
# https://elasticsearch-py.readthedocs.io/en/master/#running-on-aws-with-iam | |
# using requests_aws4auth: https://pypi.org/project/requests-aws4auth/ | |
import os | |
from aws_requests_auth.aws_auth import AWSRequestsAuth | |
from elasticsearch import Elasticsearch, RequestsHttpConnection | |
es_host = os.environ['ELASTICSEARCH_HOST'] | |
es_port = int(os.environ['ELASTICSEARCH_PORT']) | |
auth = AWSRequestsAuth( | |
aws_access_key=os.environ['AWS_ACCESS_KEY_ID'], | |
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'], | |
aws_token=os.environ['AWS_SESSION_TOKEN'], | |
aws_host=es_host, | |
aws_region='us-east-1', | |
aws_service='es', | |
) | |
es = Elasticsearch( | |
host=es_host, | |
port=es_port, | |
# For AWS Elasticsearch Service: | |
connection_class=RequestsHttpConnection, | |
http_auth=auth, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment