Skip to content

Commit 4f6a27d

Browse files
authored
Merge pull request #227 from horike37/feature/alarm_name_override
feat: support override for alarm logical id
2 parents 2d27595 + c74f089 commit 4f6a27d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/deploy/stepFunctions/compileAlarms.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function getCloudWatchAlarms(
4747
const cloudWatchMetricName = cloudWatchMetricNames[metricName];
4848
const AlarmDescription =
4949
`${stateMachineName}[${stage}][${region}]: ${alarmDescriptions[metricName]}`;
50-
const logicalId = `${stateMachineLogicalId}${cloudWatchMetricName}Alarm`;
50+
const defaultLogicalId = `${stateMachineLogicalId}${cloudWatchMetricName}Alarm`;
51+
const logicalId = _.get(metric, 'logicalId', defaultLogicalId);
5152
const treatMissingData = _.get(metric, 'treatMissingData', defaultTreatMissingData);
5253

5354
return {

lib/deploy/stepFunctions/compileAlarms.schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const simpleMetric = Joi.string()
2525

2626
const complexMetric = Joi.object().keys({
2727
metric: simpleMetric.required(),
28+
logicalId: Joi.string(),
2829
treatMissingData,
2930
});
3031

lib/deploy/stepFunctions/compileAlarms.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,43 @@ describe('#compileAlarms', () => {
380380

381381
expect(consoleLogSpy.callCount).equal(0);
382382
});
383+
384+
it('should allow alarms to override default logical ID', () => {
385+
const genStateMachine = (name) => ({
386+
name,
387+
definition: {
388+
StartAt: 'A',
389+
States: {
390+
A: {
391+
Type: 'Pass',
392+
End: true,
393+
},
394+
},
395+
},
396+
alarms: {
397+
topics: {
398+
ok: '${self:service}-${opt:stage}-alerts-ok',
399+
alarm: '${self:service}-${opt:stage}-alerts-alarm',
400+
insufficientData: '${self:service}-${opt:stage}-alerts-missing',
401+
},
402+
metrics: [
403+
{ metric: 'executionsFailed', logicalId: 'MyAlarm', treatMissingData: 'breaching' },
404+
],
405+
treatMissingData: 'ignore',
406+
},
407+
});
408+
409+
serverless.service.stepFunctions = {
410+
stateMachines: {
411+
myStateMachine: genStateMachine('stateMachineBeta'),
412+
},
413+
};
414+
415+
serverlessStepFunctions.compileAlarms();
416+
const resources = serverlessStepFunctions.serverless.service
417+
.provider.compiledCloudFormationTemplate.Resources;
418+
419+
expect(resources).to.have.property('MyAlarm');
420+
expect(consoleLogSpy.callCount).equal(0);
421+
});
383422
});

0 commit comments

Comments
 (0)