Last active
February 5, 2020 18:56
-
-
Save asthinasthi/9ceb20159edc70df0997ca2a79361548 to your computer and use it in GitHub Desktop.
Python Elasticsearch DSL Init AWS ES Connection
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
import boto3, os | |
from requests_aws4auth import AWS4Auth | |
from elasticsearch import RequestsHttpConnection | |
from elasticsearch_dsl import connections | |
def init_connection(): | |
service = "es" | |
region = "us-west-2" | |
credentials = boto3.Session().get_credentials() | |
awsauth = AWS4Auth( | |
credentials.access_key, | |
credentials.secret_key, | |
region, | |
service, | |
session_token=credentials.token, | |
) | |
connections.configure( | |
default={ | |
"hosts": [{"host": "my-non-vpc-elasticsearch.us-west-2.es.amazonaws.com", "port": 443,}], | |
"http_auth": awsauth, | |
"use_ssl": True, | |
"verify_certs": True, | |
"connection_class": RequestsHttpConnection, | |
} | |
) | |
connections.get_connection() # Checks if conn already exists if not creates a new one | |
def init_local_es_connection(): | |
es = Elasticsearch("localhost:9200") | |
connections.add_connection(alias="default", conn=es) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment