DEV Community

Cover image for How to send AWS CloudWatch Alarms to Slack

How to send AWS CloudWatch Alarms to Slack

Alex Barashkov on October 18, 2018

Alarm and monitoring systems are a key part of mature products and applications. If you worry about your customers, it’s better to be notified when...
Collapse
 
andreaswittig profile image
Andreas Wittig

Hi Alex, thanks for sharing. We took the same approach to a hackathon a few years ago and even built a product out of it: marbot receives CloudWatch alarms and all other kinds of notifications from your AWS infrastructure and escalates them among your team.

Collapse
 
alex_barashkov profile image
Alex Barashkov

I'm not surprised :) with whole complexity and irrationality of AWS there are plenty option to build own startups on top of it

Collapse
 
dharshann profile image
Dharshan Bharathuru

@alex Thanks for the writing. I'm facing issue while deploying.

'User: arn:aws:iam::273191640429:user/cloudwatch-to-slack-user is not authorized to perform: iam:PassRole on resource: arn:aws:iam::273191640429:role/cloudwatch-to-slack-role'

I know its a permission issue. But due to limited knowledge on AWS couldn't able to solve it by meself.

And let know which are all other permissions needed.

Collapse
 
dharshann profile image
Dharshan Bharathuru

I have created an IAM user with administrative permissions. And works good now.

Collapse
 
marcotech profile image
Marty Comella

Took me a while to get the permissions set on the IAM user. Now I am stuck. I have verified that I have npm v12.16.1.

I get this error after the message "Uploading zip file to AWS Lambda us-east-1 with parameters:"

The runtime parameter of nodejs8.10 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs12.x) while creating or updating functions.
=> Retrying
InvalidParameterValueException: The runtime parameter of nodejs8.10 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs12.x) while creating or updating functions.
at Object.extractError (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/protocol/json.js:48:27)
at Request.extractError (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8)
at Request.callListeners (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/sequential_executor.js:109:20)
at Request.emit (/VSCode/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/sequential_executor.js:81:10)
at Request.emit (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request. (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/request.js:38:9)
at Request. (/AWSCloudWatchToSlackLamba/lambda-cloudwatch-slack/node_modules/aws-sdk/lib/request.js:685:12) {
message: 'The runtime parameter of nodejs8.10 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs12.x) while creating or updating functions.',
code: 'InvalidParameterValueException',
time: 2020-03-22T16:21:06.899Z,
requestId: 'bc115ac4-f63b-4d39-840f-87222e751928',
statusCode: 400,
retryable: false,
retryDelay: 22.01625183426701
}

Collapse
 
j00mz profile image
Joe Tavin

This happened to me too.
To fix it upgrade the dependencies in the package.json file

I upgraded to these versions and it worked.

"dependencies": {
"aws-sdk": "^2.673.0",
"https": "^1.0.0",
"lodash": "^4.17.15",
"url": "^0.11.0"
},
"devDependencies": {
"node-lambda": "0.16.0"
}

Collapse
 
andres1905 profile image
andres1905

Hello, I have a question, I have seen the code, in this case, in the Cloudwatch function, I would like to pull the values of Dimensions seen in Cloudwatch, how would that form be? Because I want to specify the Ec2 server ID

Collapse
 
david_j_eddy profile image
David J Eddy

Thank you for the post. I was wondering if there was a way to do this with nicely formatted messages in Slack. #DevChatOps

Collapse
 
alex_barashkov profile image
Alex Barashkov

I'm just wondering that maybe one day AWS will decide to make it more "user friendly", because now it's really a lot of actions required to do a one simple thing.

Collapse
 
gkpajeet profile image
Ajeet Srivastava

Hi Alex

Can we use this code to create an alarm when CPU utilization is greater than a certain value and when it happen, then send the slack notification