Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional alarm name to CloudWatch alarm #3

Merged
merged 4 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ custom:
sqs-alarms:
- queue: your-sqs-queue-name
topic: your-sns-topic-name
name: your-alarm-name # optional parameter
thresholds:
- 1
- 50
Expand Down
58 changes: 33 additions & 25 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Alarm {
this.topic = alarm.topic
this.region = region
this.thresholds = alarm.thresholds
this.name = alarm.name
}

formatAlarmName(value) {
Expand All @@ -19,33 +20,40 @@ class Alarm {

ressources () {
return this.thresholds.map(
value => ({
[this.formatAlarmName(value)]: {
Type: 'AWS::CloudWatch::Alarm',
Properties: {
AlarmDescription: util.format('Alarm if queue contains more than %s messages', value),
Namespace: 'AWS/SQS',
MetricName: 'ApproximateNumberOfMessagesVisible',
Dimensions: [
{
Name: 'QueueName',
Value: this.queue
}
],
Statistic: 'Sum',
Period: 60,
EvaluationPeriods: 1,
Threshold: value,
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
AlarmActions: [
{ 'Fn::Join': [ '', [ 'arn:aws:sns:' + this.region + ':', { 'Ref': 'AWS::AccountId' }, ':' + this.topic ] ] }
],
OKActions: [
{ 'Fn::Join': [ '', [ 'arn:aws:sns:' + this.region + ':', { 'Ref': 'AWS::AccountId' }, ':' + this.topic ] ] }
]
value => {
const config = {
[this.formatAlarmName(value)]: {
Type: 'AWS::CloudWatch::Alarm',
Properties: {
AlarmDescription: util.format('Alarm if queue contains more than %s messages', value),
Namespace: 'AWS/SQS',
MetricName: 'ApproximateNumberOfMessagesVisible',
Dimensions: [
{
Name: 'QueueName',
Value: this.queue
}
],
Statistic: 'Sum',
Period: 60,
EvaluationPeriods: 1,
Threshold: value,
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
AlarmActions: [
{ 'Fn::Join': [ '', [ 'arn:aws:sns:' + this.region + ':', { 'Ref': 'AWS::AccountId' }, ':' + this.topic ] ] }
],
OKActions: [
{ 'Fn::Join': [ '', [ 'arn:aws:sns:' + this.region + ':', { 'Ref': 'AWS::AccountId' }, ':' + this.topic ] ] }
]
}
}
}
})

if (this.name) {
config[this.formatAlarmName(value)].Properties.AlarmName = util.format('%s-%s-%d', this.name, this.queue, value)
}
return config
}
)
}
}
Expand Down
51 changes: 50 additions & 1 deletion test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,55 @@ it('creates CloudFormation configuration', () => {
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.Threshold', 3)
})

describe('alarm name', () => {
let config

beforeEach(() => {
config = {
service: {
custom: {
'sqs-alarms': [
{ queue: 'test-queue',
topic: 'test-topic',
thresholds: [1, 2, 3]
}
]
},
provider: {
region: 'test-region',
compiledCloudFormationTemplate: {
Resources: {}
}
}
}
}
});

describe('is given', () => {
it('adds alarm name to CloudFormation configuration', () => {
config.service.custom['sqs-alarms'][0].name = 'alarm'

const test = new Plugin(config);
test.beforeDeployResources()

const data = config.service.provider.compiledCloudFormationTemplate.Resources

expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.AlarmName', 'alarm-test-queue-3')
})
})

describe('is not given', () => {
it('adds no alarm name to CloudFormation configuration', () => {
const test = new Plugin(config);
test.beforeDeployResources()

const data = config.service.provider.compiledCloudFormationTemplate.Resources

expect(data).not.toHaveProperty('testqueueMessageAlarm3.Properties.AlarmName')
})
})
})

it('creates alarms for multiple queues', () => {
let config = {
service: {
Expand Down Expand Up @@ -89,4 +138,4 @@ it('does not fail without configuration', () => {
const data = config.service.provider.compiledCloudFormationTemplate.Resources

expect(data).not.toHaveProperty('testqueueMessageAlarm3')
})
})