forked from amazon-archives/aws-lambda-zombie-workshop
-
Notifications
You must be signed in to change notification settings - Fork 21
/
exampleSNSFunction.js
71 lines (57 loc) · 2.16 KB
/
exampleSNSFunction.js
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
/* == Imports == */
var querystring = require('querystring');
var AWS = require('aws-sdk');
var path = require('path');
/* == Globals == */
var API = {
region: 'INSERT YOUR REGION CODE HERE', //i.e 'us-east-1'
endpoint: 'INSERT YOUR API GATEWAY URL HERE INCLUDING THE HTTPS://'
};
var endpoint = new AWS.Endpoint(API.endpoint);
var creds = new AWS.EnvironmentCredentials('AWS');
exports.handler = function(event, context) {
var params = querystring.parse(event.postBody);
var from = params.From;
var timestamp = "" + new Date().getTime();
var numMedia = params.NumMedia;
var message;
var mediaURL;
var phoneAuthorized = false; //will set to true when incoming phone is validated
//console.log('Received event:', JSON.stringify(event, null, 2));
var snsData = JSON.parse(event.Records[0].Sns.Message);
console.log('From SNS:', snsData);
var message = snsData.message;
var post_data = JSON.stringify({
"message": message,
"name": "SYSTEM ALERT",
"channel": "default"
});
postToChatService(post_data, context);
}
function postToChatService(post_data, context) {
var req = new AWS.HttpRequest(endpoint);
req.method = 'POST';
req.path = '/ZombieWorkshopStage/zombie/message';
req.port = '443';
req.region = API.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = post_data;
console.log('host is ' + endpoint.host);
var signer = new AWS.Signers.V4(req,'execute-api'); // es: service code
signer.addAuthorization(creds, new Date());
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
var respBody = '';
httpResp.on('data', function (chunk) {
respBody += chunk;
});
httpResp.on('end', function (chunk) {
console.log('Successfully processed HTTPS response');
context.succeed('Alert delivered: ' + JSON.parse(post_data).message);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
});
}