--- title: How to send AWS CloudWatch Alarms to Slack using lambda function. tags: AWS, EC2, cloudwatch, alarm, slack. description: --- # How to send AWS CloudWatch Alarms to Slack using lambda function ## What we need? - Login credential of your AWS account :laughing:. - Slack Admin access. ## Steps need to perform on AWS console. ### Create Role - Go on Roles from navigation pane. - Click on create role. - Chosse a service will use the role click on **Lambda**. - Click next. - Attach a policy name **AWSLambdaBasicExecutionRole** (you can search using policy name). - Click next next till it's not ask for role name. - Give role name and description. - click on create role. - :tada: It's Done. ## Configure slack app Add Incoming webhook That variable you should fill with the Slack Incoming Webhook, create a new one for CloudWatch on that page for your organization. Get A web hook URL from APP. ## Deploy Lambda function To deploy the AWS Lambda function, you need to clone the repository and have Node.js installed on your local machine. `git clone https://github.com/assertible/lambda-cloudwatch-slack.git` `cd lambda-cloudwatch-slack` `cp .env.example .env` Open your .env file and fill in the environment variables. ## Configure `.env` file AWS_FUNCTION_NAME The name of your function which will be declared in your AWS Lambda function page. Let’s call it “cloudwatch-to-slack.” AWS_ROLE=arn:aws:iam::123456789123:role/lambda_exec_role Copy the arn id of role you created eariler. AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY - Go on AWS console select IAM page. - Click on users from navigation pane. - Click Add user. - Attach administrative access to user `AdministratorAccess`. - And click on next. - Copy AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY. - Go on your local computer. - use command `vim ~/.aws/credentials` - Add your key here like this. ``` [claudia] aws_access_key_id = xxxxxxxxx aws_secret_access_key = xxxxxxxxxxxxxxxxx ``` - And same add this to .env file. - Run following commands. ``` npm install npm run deploy ``` - Check your lambda function page you will able to see one new lambda function created. - :tada: It's done. ###### If you get any problem for trobleshooting please user link in reference ## Create an SNS topic and subscription ### Create a topic - Go on [SNS](https://console.aws.amazon.com/sns/v2/home?region=us-east-1) page. - Click on Create topic. - Give a name and configure your topic. - Click on create topic. ### Create a subscription - Click on create a subscription. - Select a topic. - Select **lambda** in protocol. - And select lambda function created by your deployed code. - And click on create a subscription. #### Attach topic with your Alarm and it's all set. ## Thanks a lot --- ##### Referance link [How to send AWS CloudWatch Alarms to Slack](https://medium.com/pixelpoint/how-to-send-aws-cloudwatch-alarms-to-slack-502bcf106047)