Skip to content

Instantly share code, notes, and snippets.

@asthinasthi
Last active February 5, 2020 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asthinasthi/9ceb20159edc70df0997ca2a79361548 to your computer and use it in GitHub Desktop.
Save asthinasthi/9ceb20159edc70df0997ca2a79361548 to your computer and use it in GitHub Desktop.
Python Elasticsearch DSL Init AWS ES Connection
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