Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Created June 21, 2019 07:17
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 ruanbekker/b61b2b696ec94eb50f5b91ba0efa85d2 to your computer and use it in GitHub Desktop.
Save ruanbekker/b61b2b696ec94eb50f5b91ba0efa85d2 to your computer and use it in GitHub Desktop.
Authenticate AWS Elasticsearch Requests with Requests and RequestsAws4Auth

Auth Requests with IAM using Requests and Aws4Auth

Elasticsearch Service allows IAM User which is configured in the aws credentials provider under the profile name: myprofile.

We use the requests_aws4auth to validate the request and pass the auth to requests:

$ python python_requests_elasticsearch_iam.py
{
  "name": "xx",
  "cluster_name": "xx:domain-name",
  "cluster_uuid": "xx",
  "version": {
    "number": "6.7.0",
    "build_flavor": "oss",
    "build_type": "zip",
    "build_hash": "xx",
    "build_date": "2019-04-17T05:34:35.022392Z",
    "build_snapshot": false,
    "lucene_version": "7.7.0",
    "minimum_wire_compatibility_version": "5.6.0",
    "minimum_index_compatibility_version": "5.0.0"
  },
  "tagline": "You Know, for Search"
}
import boto3
import json
import requests
from requests_aws4auth import AWS4Auth
host = 'https://search-domain-name-xx.eu-west-1.es.amazonaws.com'
region = 'eu-west-1'
service = 'es'
headers = {'Content-Type': 'application/json'}
def prettify(payload):
print(json.dumps(payload, indent=2, default=str))
credentials = boto3.Session(
region_name=region,
profile_name='myprofile'
).get_credentials()
awsauth = AWS4Auth(
credentials.access_key,
credentials.secret_key,
region, service,
session_token=credentials.token
)
response = requests.get(host, auth=awsauth, headers=headers)
prettify(response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment