Created
July 21, 2015 11:23
-
-
Save maruks/e036168263cd412146e6 to your computer and use it in GitHub Desktop.
AWS gateway / lambda function invocation
This file contains hidden or 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
# AWS Version 4 signing example | |
# This version makes a POST request and passes request parameters | |
# in the body (payload) of the request. Auth information is passed in | |
# an Authorization header. | |
import sys, os, base64, datetime, hashlib, hmac | |
import requests # pip install requests | |
method = 'POST' | |
service = 'execute-api' | |
host = '3cwaju3kee.execute-api.eu-west-1.amazonaws.com' | |
region = 'eu-west-1' | |
endpoint = 'https://3cwaju3kee.execute-api.eu-west-1.amazonaws.com/test/clj-lambda' | |
content_type = 'application/json' | |
request_parameters = '<root "attr"="bar">' | |
request_parameters += '"foo"' | |
request_parameters += '</root>' | |
# Key derivation functions. See: | |
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python | |
def sign(key, msg): | |
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest() | |
def getSignatureKey(key, date_stamp, regionName, serviceName): | |
kDate = sign(('AWS4' + key).encode('utf-8'), date_stamp) | |
kRegion = sign(kDate, regionName) | |
kService = sign(kRegion, serviceName) | |
kSigning = sign(kService, 'aws4_request') | |
return kSigning | |
# AWS access key | |
access_key = 'your key' | |
secret_key = 'your secret key' | |
api_key = 'api key' | |
# Create a date for headers and the credential string | |
t = datetime.datetime.utcnow() | |
amz_date = t.strftime('%Y%m%dT%H%M%SZ') | |
date_stamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope | |
canonical_uri = '/test/clj-lambda' | |
## Step 3: Create the canonical query string. In this example, request | |
# parameters are passed in the body of the request and the query string | |
# is blank. | |
canonical_querystring = '' | |
# Step 4: Create the canonical headers. Header names and values | |
# must be trimmed and lowercase, and sorted in ASCII order. | |
# Note that there is a trailing \n. | |
canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n' + 'x-api-key:' + api_key + '\n' | |
signed_headers = 'content-type;host;x-amz-date;x-api-key' | |
payload_hash = hashlib.sha256(request_parameters).hexdigest() | |
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash | |
algorithm = 'AWS4-HMAC-SHA256' | |
credential_scope = date_stamp + '/' + region + '/' + service + '/' + 'aws4_request' | |
string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request).hexdigest() | |
# ************* TASK 3: CALCULATE THE SIGNATURE ************* | |
# Create the signing key using the function defined above. | |
signing_key = getSignatureKey(secret_key, date_stamp, region, service) | |
# Sign the string_to_sign using the signing_key | |
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest() | |
# ************* TASK 4: ADD SIGNING INFORMATION TO THE REQUEST ************* | |
# Put the signature information in a header named Authorization. | |
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature | |
# # Python note: The 'host' header is added automatically by the Python 'requests' library. | |
headers = {'Content-Type':content_type, | |
'X-Amz-Date':amz_date, | |
'Authorization':authorization_header, | |
'x-api-key':api_key} | |
# ************* SEND THE REQUEST ************* | |
print '\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++' | |
print 'Request URL = ' + endpoint | |
r = requests.post(endpoint, data=request_parameters, headers=headers) | |
print '\nRESPONSE++++++++++++++++++++++++++++++++++++' | |
print 'Response code: %d\n' % r.status_code | |
print r.text |
Author
maruks
commented
Jul 21, 2015
(deflambdafn example.lambda.XmlLambda
[in out ctx]
(let [event (slurp in)]
(with-open [writer (io/writer out)]
(.write writer event))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment