Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
370 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict' | ||
|
||
/** | ||
* @param {object} eventsAlarmsConfig The fully resolved alarm configuration | ||
*/ | ||
module.exports = function eventsAlarms (eventsAlarmsConfig, context) { | ||
return { | ||
createEventsAlarms | ||
} | ||
|
||
/** | ||
* Add all required Events alarms to the provided CloudFormation template | ||
* based on the Events resources found within | ||
* | ||
* @param {CloudFormationTemplate} cfTemplate A CloudFormation template object | ||
*/ | ||
function createEventsAlarms (cfTemplate) { | ||
const ruleResources = cfTemplate.getResourcesByType( | ||
'AWS::Events::Rule' | ||
) | ||
|
||
for (const [ruleResourceName, ruleResource] of Object.entries(ruleResources)) { | ||
if (eventsAlarmsConfig.FailedInvocations.enabled) { | ||
const failedInvocations = createFailedInvocationsAlarm( | ||
ruleResourceName, | ||
ruleResource, | ||
eventsAlarmsConfig.FailedInvocations | ||
) | ||
cfTemplate.addResource(failedInvocations.resourceName, failedInvocations.resource) | ||
} | ||
|
||
if (eventsAlarmsConfig.ThrottledRules.enabled) { | ||
const throttledRules = createThrottledRulesAlarm( | ||
ruleResourceName, | ||
ruleResource, | ||
eventsAlarmsConfig.ThrottledRules | ||
) | ||
cfTemplate.addResource(throttledRules.resourceName, throttledRules.resource) | ||
} | ||
} | ||
} | ||
|
||
function createEventsAlarm ( | ||
alarmName, | ||
alarmDescription, | ||
ruleName, | ||
comparisonOperator, | ||
threshold, | ||
metricName, | ||
statistic, | ||
period, | ||
evaluationPeriods, | ||
treatMissingData | ||
) { | ||
const metricProperties = { | ||
Dimensions: [{ Name: 'RuleName', Value: ruleName }], | ||
MetricName: metricName, | ||
Namespace: 'AWS/Events', | ||
Period: period, | ||
Statistic: statistic | ||
} | ||
|
||
return { | ||
Type: 'AWS::CloudWatch::Alarm', | ||
Properties: { | ||
ActionsEnabled: true, | ||
AlarmActions: context.alarmActions, | ||
AlarmName: alarmName, | ||
AlarmDescription: alarmDescription, | ||
EvaluationPeriods: evaluationPeriods, | ||
ComparisonOperator: comparisonOperator, | ||
Threshold: threshold, | ||
TreatMissingData: treatMissingData, | ||
...metricProperties | ||
} | ||
} | ||
} | ||
|
||
function createFailedInvocationsAlarm (ruleResourceName, ruleResource, config) { | ||
const ruleName = ruleResource.Properties.Name | ||
const threshold = config.Threshold | ||
|
||
return { | ||
resourceName: `slicWatchEventsFailedInvocationsAlarm${ruleResourceName}`, | ||
resource: createEventsAlarm( | ||
`EventsFailedInvocationsAlarm_${ruleName}`, // alarmName | ||
`Failed Invocations for ${ruleName} breaches ${threshold}`, // alarmDescription | ||
ruleName, | ||
config.ComparisonOperator, | ||
threshold, | ||
'FailedInvocations', // metricName | ||
config.Statistic, | ||
config.Period, | ||
config.EvaluationPeriods, | ||
config.TreatMissingData | ||
) | ||
} | ||
} | ||
|
||
function createThrottledRulesAlarm (ruleResourceName, ruleResource, config) { | ||
const ruleName = ruleResource.Properties.Name | ||
const threshold = config.Threshold | ||
return { | ||
resourceName: `slicWatchEventsThrottledRulesAlarm${ruleResourceName}`, | ||
resource: createEventsAlarm( | ||
`EventsThrottledRulesAlarm_${ruleName}`, // alarmName | ||
`Throttled RulesAlarm for ${ruleName} breaches ${threshold}`, // alarmDescription | ||
ruleName, | ||
config.ComparisonOperator, | ||
threshold, | ||
'ThrottledRules', // metricName | ||
config.Statistic, | ||
config.Period, | ||
config.EvaluationPeriods, | ||
config.TreatMissingData | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict' | ||
|
||
const eventsAlarms = require('../alarms-eventbridge') | ||
const { test } = require('tap') | ||
const defaultConfig = require('../default-config') | ||
const { | ||
assertCommonAlarmProperties, | ||
alarmNameToType, | ||
createTestConfig, | ||
createTestCloudFormationTemplate, | ||
testContext | ||
} = require('./testing-utils') | ||
|
||
test('Events alarms are created', (t) => { | ||
const alarmConfig = createTestConfig( | ||
defaultConfig.alarms, | ||
{ | ||
Period: 120, | ||
EvaluationPeriods: 2, | ||
TreatMissingData: 'breaching', | ||
ComparisonOperator: 'GreaterThanOrEqualToThreshold', | ||
Events: { | ||
FailedInvocations: { | ||
Threshold: 50 | ||
}, | ||
ThrottledRules: { | ||
Threshold: 50 | ||
} | ||
} | ||
} | ||
) | ||
|
||
const eventsAlarmConfig = alarmConfig.Events | ||
|
||
const { createEventsAlarms } = eventsAlarms(eventsAlarmConfig, testContext) | ||
const cfTemplate = createTestCloudFormationTemplate() | ||
createEventsAlarms(cfTemplate) | ||
|
||
const alarmResources = cfTemplate.getResourcesByType('AWS::CloudWatch::Alarm') | ||
|
||
const expectedTypes = { | ||
EventsFailedInvocationsAlarm: 'FailedInvocations', | ||
EventsThrottledRulesAlarm: 'ThrottledRules' | ||
} | ||
|
||
t.equal(Object.keys(alarmResources).length, Object.keys(expectedTypes).length) | ||
for (const alarmResource of Object.values(alarmResources)) { | ||
const al = alarmResource.Properties | ||
assertCommonAlarmProperties(t, al) | ||
const alarmType = alarmNameToType(al.AlarmName) | ||
const expectedMetric = expectedTypes[alarmType] | ||
t.equal(al.MetricName, expectedMetric) | ||
t.ok(al.Statistic) | ||
t.equal(al.Threshold, eventsAlarmConfig[expectedMetric].Threshold) | ||
t.equal(al.EvaluationPeriods, 2) | ||
t.equal(al.TreatMissingData, 'breaching') | ||
t.equal(al.ComparisonOperator, 'GreaterThanOrEqualToThreshold') | ||
t.equal(al.Namespace, 'AWS/Events') | ||
t.equal(al.Period, 120) | ||
t.same(al.Dimensions, [ | ||
{ | ||
Name: 'RuleName', | ||
Value: 'serverless-test-project-dev-eventsRule-rule-1' | ||
} | ||
]) | ||
} | ||
|
||
t.end() | ||
}) | ||
|
||
test('Events alarms are not created when disabled globally', (t) => { | ||
const alarmConfig = createTestConfig( | ||
defaultConfig.alarms, | ||
{ | ||
Events: { | ||
enabled: false, // disabled globally | ||
Period: 60, | ||
FailedInvocations: { | ||
Threshold: 50 | ||
}, | ||
ThrottledRules: { | ||
Threshold: 50 | ||
} | ||
} | ||
} | ||
) | ||
|
||
const eventsAlarmConfig = alarmConfig.Events | ||
|
||
const { createEventsAlarms } = eventsAlarms(eventsAlarmConfig, testContext) | ||
|
||
const cfTemplate = createTestCloudFormationTemplate() | ||
createEventsAlarms(cfTemplate) | ||
|
||
const alarmResources = cfTemplate.getResourcesByType('AWS::CloudWatch::Alarm') | ||
|
||
t.same({}, alarmResources) | ||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.