-
Notifications
You must be signed in to change notification settings - Fork 99
/
add_cname_as_description_to_cloud_front_alarms.js
90 lines (81 loc) · 3.19 KB
/
add_cname_as_description_to_cloud_front_alarms.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
78
79
80
81
82
83
84
85
86
87
88
89
// To run:
// AWS_PROFILE=<aws-profile> node add_cname_as_description_to_cloud_front_alarms.js
const awsConfigHelper = require('./util/awsConfigHelper');
const wait = require('./util/wait');
const AWS = require('aws-sdk');
const cli = require('cli');
let cloudwatch = null;
function isCloudFrontAlarm(alarm) {
return alarm.Dimensions && alarm.Dimensions.find((dim) => {
return dim.Name === "DistributionId";
});
}
function getDistributionIdFromAlarm(alarm) {
const distribution = alarm.Dimensions.find((dim) => {
return dim.Name === "DistributionId";
});
return distribution.Value;
}
async function getCloudFrontAlarms() {
await awsConfigHelper.updateConfig("us-east-1");
cloudwatch = new AWS.CloudWatch();
let alarms = [];
let nextToken = null;
do {
const alarmsResponse = await cloudwatch.describeAlarms(
{ NextToken: nextToken }).promise();
if (alarmsResponse && alarmsResponse.MetricAlarms) {
alarmsResponse.MetricAlarms.forEach((alarm) => {
if (isCloudFrontAlarm(alarm)) {
alarms.push(alarm);
}
});
nextToken = alarmsResponse.NextToken;
}
} while (nextToken);
return alarms;
}
async function getCloudFrontDistributions() {
await awsConfigHelper.updateConfig("us-east-1");
const cloudFront = new AWS.CloudFront();
let distributions = [];
let nextToken = null;
do {
nextToken = null;
const distributionsResponse = await cloudFront.listDistributions(
{ Marker: nextToken }).promise();
if (distributionsResponse &&
distributionsResponse.DistributionList &&
distributionsResponse.DistributionList.Items
) {
distributionsResponse.DistributionList.Items.forEach((distribution) => {
distributions[distribution.Id] = distribution;
});
nextToken = distributionsResponse.DistributionList.NextMarker;
}
} while (nextToken);
return distributions;
}
async function setDescriptionForCloudFrontAlarms() {
const cloudFrontAlarms = await getCloudFrontAlarms();
const cloudFrontDistributions = await getCloudFrontDistributions();
for (const cloudFrontAlarm of cloudFrontAlarms) {
const distributionId = getDistributionIdFromAlarm(cloudFrontAlarm);
cloudFrontAlarm.EvaluationPeriods = 2;
cloudFrontAlarm.DatapointsToAlarm = 2;
cloudFrontAlarm.Threshold = 5;
cloudFrontAlarm.AlarmDescription = cloudFrontDistributions[distributionId].Aliases ?
cloudFrontDistributions[distributionId].Aliases.Items.join(", ") : undefined;
delete cloudFrontAlarm.AlarmArn;
delete cloudFrontAlarm.AlarmConfigurationUpdatedTimestamp;
delete cloudFrontAlarm.StateValue;
delete cloudFrontAlarm.StateReason;
delete cloudFrontAlarm.StateReasonData;
delete cloudFrontAlarm.StateUpdatedTimestamp;
delete cloudFrontAlarm.Metrics;
console.log("setting for", cloudFrontAlarm.AlarmName);
await cloudwatch.putMetricAlarm(cloudFrontAlarm).promise();
await wait(1000);
}
}
setDescriptionForCloudFrontAlarms();