Created
August 31, 2017 23:06
-
-
Save LegoStormtroopr/4a9a2110eb03c661894529b508d83845 to your computer and use it in GitHub Desktop.
Connecting Django/Haystack to AWS ElasticSearch using IAM rotating credentials
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 | |
import os | |
import requests | |
from botocore.auth import SigV4Auth | |
from requests_aws4auth import AWS4Auth | |
from elasticsearch import RequestsHttpConnection | |
class AWSRequestsHttpConnection(RequestsHttpConnection): | |
def perform_request(self, *args, **kwargs): | |
credentials = boto3.session.Session().get_credentials() | |
signed_creds = SigV4Auth(credentials, 'es', os.environ['AWS__REGION']) | |
secure_auth = AWS4Auth( | |
credentials.access_key, credentials.secret_key, | |
os.environ['AWS__REGION'], 'es', | |
session_token = signed_creds.credentials.token | |
) | |
self.session.auth = secure_auth | |
return super(AWSRequestsHttpConnection, self).perform_request(*args, **kwargs) |
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
requests_aws4auth | |
boto3 |
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
from boto_auth_requests import AWSRequestsHttpConnection | |
from urllib.parse import urlparse | |
es = urlparse(os.environ.get('AWS_ELASTICSEARCH_URL') or 'http://127.0.0.1:9200/') | |
es_index_name = os.environ.get('AWS_ELASTICSEARCH_INDEX_NAME', ("I_FORGOT_TO_SET_AN_INDEX__%s" % random_string(15))) | |
port = es.port or 80 | |
HAYSTACK_CONNECTIONS = { | |
'default': { | |
'ENGINE': 'haystack_elasticsearch.elasticsearch5.Elasticsearch5SearchEngine', | |
'URL': es.scheme + '://' + es.hostname + ':' + str(port), | |
'INCLUDE_SPELLING': True, | |
'INDEX_NAME': 'haystack', | |
'TIMEOUT' : 60 , | |
'KWARGS': { | |
'port': port, | |
'use_ssl': True, | |
'verify_certs': True, | |
'connection_class': AWSRequestsHttpConnection, | |
} | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I_FORGOT_TO_SET_AN_INDEX__ lol
Thanks for this!