forked from p-prakash/aws-lambda-functions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerateRandomString.js
71 lines (63 loc) · 2.09 KB
/
GenerateRandomString.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
/**
*
* Handler called by Lambda function.
* @param {object} event - event parameter gets the attributes from CFN trigger.
* @param {object} context - context parameter used to log details to CloudWatch log stream.
*
*/
exports.handler = function(event, context) {
if (event.RequestType === 'Delete') {
sendResponse(event, context, 'SUCCESS');
return;
}
var responseStatus = 'SUCCESS';
var responseData = {};
var crypto = require('crypto');
if (typeof event.ResourceProperties.length !== 'undefined') {
console.log(typeof event.ResourceProperties.length);
var sl = parseInt(event.ResourceProperties.length);
responseData.randomstr = crypto.randomBytes(sl).toString('hex').substr(0, sl);
}
else {
responseData.randomstr = crypto.randomBytes(10).toString('hex').substr(0, 6);
}
console.log('Randmon string is ', responseData.randomstr);
sendResponse(event, context, responseStatus, responseData);
};
// Sends a response to the pre-signed S3 URL
var sendResponse = function(event, context, responseStatus, responseData) {
var responseBody = JSON.stringify({
Status: responseStatus,
Reason: 'See the details in CloudWatch Log Stream: ' + context.logStreamName,
PhysicalResourceId: context.logStreamName,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
Data: responseData
});
console.log('RESPONSE BODY:\n', responseBody);
var https = require('https');
var url = require('url');
var parsedUrl = url.parse(event.ResponseURL);
var options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: 'PUT',
headers: {
'Content-Type': '',
'Content-Length': responseBody.length
}
};
var req = https.request(options, function(res) {
console.log('STATUS:', res.statusCode);
console.log('HEADERS:', JSON.stringify(res.headers));
context.succeed('Successfully sent stack response!');
});
req.on('error', function(err) {
console.log('sendResponse Error:\n', err);
context.fail(err);
});
req.write(responseBody);
req.end();
};