-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda-test.html
117 lines (96 loc) · 5 KB
/
lambda-test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* reset */
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{border:0;font-size:100%;font:inherit;vertical-align:baseline;margin:0;padding:0}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:none}table{border-collapse:collapse;border-spacing:0}
</style>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
<script>
$.support.cors = true;
var AWS = {};
AWS.config = {
region: 'us-east-1',
accessKeyId: 'AKIAJTCGSXISBAK427RQ',
secretAccessKey: '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'
};
function getSignatureKey(key, dateStamp, regionName, serviceName) {
var kDate = CryptoJS.HmacSHA256(dateStamp, "AWS4" + key, { asBytes: true });
var kRegion = CryptoJS.HmacSHA256(regionName, kDate, { asBytes: true });
var kService = CryptoJS.HmacSHA256(serviceName, kRegion, { asBytes: true });
return CryptoJS.HmacSHA256("aws4_request", kService, { asBytes: true });
}
function sign(service, method, host, payload, amzdate, datestamp) {
// Step 1 - Create canonical request.
var uri = '/' + AWS.Lambda.apiVersion + '/functions/' + 'HelloWorld' + '/invocations';
var queryString = '';
var canonicalHeaders = 'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n' + 'x-amz-invocation-type:RequestResponse' + '\n' + 'x-amz-log-type:None' + '\n';
var signedHeaders = 'host;x-amz-date;x-amz-invocation-type;x-amz-log-type';
var payloadHash = CryptoJS.SHA256(payload);
var canonicalRequest = method + '\n' + uri + '\n' + queryString + '\n' + canonicalHeaders + '\n' + signedHeaders + '\n' + payloadHash;
// Step 2 - Create the string to sign.
var algorithm = 'AWS4-HMAC-SHA256';
var credentialScope = datestamp + '/' + AWS.config.region + '/' + service + '/' + 'aws4_request';
var stringToSign = algorithm + '\n' + amzdate + '\n' + credentialScope + '\n' + CryptoJS.SHA256(canonicalRequest);
// Step 3 - Calculate the signature.
var signingKey = getSignatureKey(AWS.config.secretAccessKey, datestamp, AWS.config.region, service);
var signature = CryptoJS.HmacSHA256(stringToSign, signingKey, { asBytes: true });
// Step 4 - Add signing information to the request.
return algorithm + ' Credential=' + AWS.config.accessKeyId + '/' + credentialScope + ', SignedHeaders=' + signedHeaders + ', Signature=' + signature;
}
AWS.Lambda = {
apiVersion: '2015-03-31',
invoke: function(params, callback) {
var method = 'POST';
var host = 'lambda.' + AWS.config.region + '.amazonaws.com';
var service = 'lambda';
var amzdate = moment().utc().format('YYYYMMDD[T]HHmmss[Z]');
var datestamp = moment().utc().format('YYYYMMDD');
var authHeader = sign(service, method, host, params.Payload, amzdate, datestamp);
var headers = {
'X-Amz-Date': amzdate,
'Authorization': authHeader,
'X-Amz-Invocation-Type': params.InvocationType,
'X-Amz-Log-Type': params.LogType,
'Content-Type': 'text/plain;charset=UTF-8'
};
$.ajax({
url: 'https://lambda.' + AWS.config.region + '.amazonaws.com/' + AWS.Lambda.apiVersion + '/functions/' + params.FunctionName + '/invocations',
method: method,
headers: headers,
data: params.Payload,
dataType: 'text'
}).done(function(data) {
callback(null, {Payload: data});
}).fail(function(xhr, status, ex) {
alert(ex);
});
}
};
var payload = {
'firstName': 'Dave',
'lastName': 'Maple'
};
var params = {
FunctionName: 'HelloWorld',
InvocationType: 'RequestResponse',
LogType: 'None',
Payload: JSON.stringify(payload)
};
$(document).ready(function() {
AWS.Lambda.invoke(params, function(ex, data) {
var response = JSON.parse(data.Payload);
$('#result').text(response.message);
});
});
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>