-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.js
More file actions
65 lines (56 loc) · 1.99 KB
/
lambda.js
File metadata and controls
65 lines (56 loc) · 1.99 KB
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
console.log('Loading event');
var aws = require('aws-sdk');
function createCloudFormationStack(key, url, onCompletion, context) {
var stackName = key.match(/\/(.*).json$/)[1];
console.log('Creating CloudFormation client')
var cloudformation = new aws.CloudFormation({
apiVersion: '2010-05-15',
region: 'us-east-1'
});
var params = {
StackName: stackName,
TemplateURL: url,
Tags: [ {
"Key" : "Name",
"Value" : stackName
}, {
"Key" : "Template",
"Value" : key
}, {
"Key" : "CreatedBy",
"Value" : "LambdaCloudformationRunner"
}]
}
console.log('CreateStack with Params')
cloudformation.createStack(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
console.log('CreateStack succeed - now watch progress in CloudFormation console')
console.log(data); // successful response
}
onCompletion(context)
});
}
function cleanup(context) {
console.log('Cleaning up context')
context.done(null,'');
}
exports.handler = function(event, context) {
var s3 = new aws.S3({apiVersion: '2006-03-01'});
console.log('Received event:');
console.log(JSON.stringify(event, null, ' '));
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
//only pickup files when key starts with ‘cloudformation' and ends with ‘.json'
if ((/^cloudformation\//).test(key) && (/.json$/).test(key)) {
createCloudFormationStack(key,
'http://s3.amazonaws.com/' + bucket + '/' + key,
cleanup,
context);
} else {
console.log("File is not a cloudformation template (expecting key like cloudformation/*.json)");
cleanup(context)
}
};