Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS gateway / lambda function invocation
# 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
@maruks

This comment has been minimized.

Copy link
Owner Author

maruks commented Jul 21, 2015

{
 "content-type" : "$input.params('Content-Type')",
 "date" : "$input.params('X-Amz-Date')",
 "api-key" : "$input.params('x-api-key')",
 "auth" : "$input.params('Authorization')",
 "body" : $input.json('$')
 }
@maruks

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.