forked from natonic/Developer-Tools-Deep-Dive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment-automation-lambda-function-code_1467308443.txt
75 lines (67 loc) · 2.39 KB
/
deployment-automation-lambda-function-code_1467308443.txt
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
var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var codedeploy = new aws.CodeDeploy();
exports.handler = function(event, context) {
var artifact_type;
var bucket;
var key;
/* runtime functions */
function getS3ObjectAndCreateDeployment() {
// Get the s3 object to fetch application-name and deploymentgroup-name metadata.
s3.headObject({
Bucket: bucket,
Key: key
}, function(err, data) {
if (err) {
context.done('Error', 'Error getting s3 object: ' + err);
} else {
console.log('Creating deployment');
createDeployment(data);
}
});
}
function createDeployment(data) {
if (!data.Metadata['application-name'] || !data.Metadata['deploymentgroup-name']) {
console.error('application-name and deploymentgroup-name object metadata must be set.');
context.done();
}
var params = {
applicationName: data.Metadata['application-name'],
deploymentGroupName: data.Metadata['deploymentgroup-name'],
description: 'Lambda invoked codedeploy deployment',
ignoreApplicationStopFailures: false,
revision: {
revisionType: 'S3',
s3Location: {
bucket: bucket,
bundleType: artifact_type,
key: key
}
}
};
codedeploy.createDeployment(params,
function (err, data) {
if (err) {
context.done('Error','Error creating deployment: ' + err);
}
else {
console.log(data); // successful response
console.log('Finished executing lambda function');
context.done();
}
});
}
console.log('Received event:');
console.log(JSON.stringify(event, null, ' '));
// Get the object from the event
bucket = event.Records[0].s3.bucket.name;
key = event.Records[0].s3.object.key;
tokens = key.split('.');
artifact_type = tokens[tokens.length - 1];
if (artifact_type == 'gz') {
artifact_type = 'tgz';
} else if (['zip', 'tar', 'tgz'].indexOf(artifact_type) < 0) {
artifact_type = 'tar';
}
getS3ObjectAndCreateDeployment();
};