Skip to content

Instantly share code, notes, and snippets.

@b-meson
Created July 13, 2017 14:42
Show Gist options
  • Save b-meson/d9c2118240eb03839c4a0fd99ad73bd8 to your computer and use it in GitHub Desktop.
Save b-meson/d9c2118240eb03839c4a0fd99ad73bd8 to your computer and use it in GitHub Desktop.
push ES Config's JSON to ES using requests
# Access Keys from ~/.aws/credentials
aws_access_key_id = 'SOMEKEY'
aws_secret_access_key = 'SECRETKEYVALUE'
aws_session_token = 'ANOTHER KEY FROM STS'
import requests
import requests_aws4auth
from requests_aws4auth import AWS4Auth
auth = AWS4Auth(aws_access_key_id,aws_secret_access_key,'region', 'es', session_token=aws_session_token)
endpoint = "https://clouddomain.region.es.amazonaws.com/_template/template_1"
payload = open('template.json','rb').read()
response = requests.post(endpoint, auth=auth, data=payload )
print(response.text)
@b-meson
Copy link
Author

b-meson commented Jul 14, 2017

the below code is less fragile and fixes the endpoint issue

import boto3
import requests
import requests_aws4auth
from requests_aws4auth import AWS4Auth

session=boto3.session.Session()                                                                                         
credentials = session.get_credentials().get_frozen_credentials()                                                        
             
# grab credentials from boto session                                                                                                           
access_key = credentials.access_key                                                                                     
secret_key = credentials.secret_key                                                                                     
token = credentials.token                                                                                                                                                                                                     
region = session.region_name    

# session_token is an optional parameter if using STS or federated access 
headers = { 'Content-Type': 'application/json'}
auth = AWS4Auth(access_key,secret_key,region, 'es', session_token=token)
endpoint = "https://es-cloudomain.us-west-2.es.amazonaws.com/_template/"

payload = open('elastic_search_cloudtrail_template.json','r').read()
#response = requests.put(endpoint+'template_1', auth=auth, data=payload, headers=headers)
#response = requests.delete(endpoint+'template_1', auth=auth)
response = requests.get(endpoint, auth=auth)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment