-
Notifications
You must be signed in to change notification settings - Fork 0
/
afterAllowTraffic.js
77 lines (68 loc) · 2.7 KB
/
afterAllowTraffic.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
72
73
74
75
76
77
"use strict";
const AWS = require("aws-sdk");
const codedeploy = new AWS.CodeDeploy({ apiVersion: "2014-10-06" });
var lambda = new AWS.Lambda();
exports.handler = (event, context, callback) => {
console.log("Entering PostTraffic Hook!");
// Read the DeploymentId and LifecycleEventHookExecutionId from the event payload
var deploymentId = event.DeploymentId;
var lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId;
var functionToTest = process.env.NewVersion;
console.log("AfterAllowTraffic hook tests started");
console.log("Testing new function version: " + functionToTest);
// Create parameters to pass to the updated Lambda function that
// include the original "date" parameter. If the function did not
// update as expected, then the "date" option might be invalid. If
// the parameter is invalid, the function returns
// a statusCode of 400 indicating it failed.
var lambdaParams = {
FunctionName: functionToTest,
Payload: '{"option": "date", "period": "today"}',
InvocationType: "RequestResponse",
};
var lambdaResult = "Failed";
// Invoke the updated Lambda function.
lambda.invoke(lambdaParams, function (err, data) {
if (err) {
// an error occurred
console.log(err, err.stack);
lambdaResult = "Failed";
} else {
// successful response
var result = JSON.parse(data.Payload);
console.log("Result: " + JSON.stringify(result));
console.log("statusCode: " + result.statusCode);
// Check if the status code returned by the updated
// function is 400. If it is, then it failed. If
// is not, then it succeeded.
if (result.statusCode != "400") {
console.log("Validation of time parameter succeeded");
lambdaResult = "Succeeded";
} else {
console.log("Validation failed");
}
// Complete the PostTraffic Hook by sending CodeDeploy the validation status
var params = {
deploymentId: deploymentId,
lifecycleEventHookExecutionId: lifecycleEventHookExecutionId,
status: lambdaResult, // status can be 'Succeeded' or 'Failed'
};
// Pass CodeDeploy the prepared validation test results.
codedeploy.putLifecycleEventHookExecutionStatus(
params,
function (err, data) {
if (err) {
// Validation failed.
console.log("CodeDeploy Status update failed");
console.log(err, err.stack);
callback("CodeDeploy Status update failed");
} else {
// Validation succeeded.
console.log("CodeDeploy status updated successfully");
callback(null, "CodeDeploy status updated successfully");
}
}
);
}
});
};