DEV Community

Cover image for Terraform Module to Implement Slack Notification Channel Using AWS Chatbot & SNS
Md Mohaymenul Islam (Noyon)
Md Mohaymenul Islam (Noyon)

Posted on • Edited on

Terraform Module to Implement Slack Notification Channel Using AWS Chatbot & SNS

Have you ever wanted to get slack alerts from your AWS services
If yes then mate do I have a treat for you.
I will show you a Terraform Module which helps you to create a slack notification channel using AWS Chatbot and SNS topic. It will create a stack. By using this stack you can send anything to the Slack channel. It could be the Lamda Function, Step Function, CloudWatch Alarm, or any other notification.

Step-1: Configure AWS Chatbot

Sign in to the AWS Management Console and open the AWS Chatbot console

Image description

Select
Chat client: Slack
Then Click Configure client Button.

Image description

It will ask for your slack information. Just click Allow to allow the permission.

Image description

Note: If you don't any the power to Allow access then ask your Slack admin to Allow it for you or ask for administrator permission.

After Allowing the Slack information you will see the conformation window!

Image description

Note: Just note down the Slack workspace ID. Although, you will find this workspace Id from your Slack as well.

AWS Chatbot configuration is done!

Step-2: Configure Slack Channel

You can select the existing Slack chennel or Create a new one for this notification.
I am creating a new private channel for this configuration.

Image description

From the Integrations tab click Add an App button.

Image description

Then search AWS Chatbot App and Add in your channel.

Image description

Image description

Slack Chennel Configuration is Done!
Now note down the Slack Channel ID.

Image description

Step-3: Apply Terraform Module to Integrated This Slack Channel to AWS Chatbot

Now define your module like this:



provider "aws" {
  region     = "eu-west-1"
  access_key = "AWS-ACCESS-KEY"
  secret_key = "AWS-SECRET-ACCESS-KEY"
}


module "chatbot-slack-channel" {
  source             = "git@github.com:noyonict/aws-chatbot-slack-channel.git"
  configuration_name = "alerts_test_aws"
  slack_channel_id   = "A2C4E6G8I0K"
  slack_workspace_id = "Z1Y2X3W4V"
}


Enter fullscreen mode Exit fullscreen mode

configuration_name*: The name of the configuration/slack channel. Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*. For example: alerts_test_aws

slack_channel_id*: The ID of the Slack channel. To get the ID, open Slack, right click on the channel name in the left pane, then choose Copy Link. The channel ID is the 9-character string at the end of the URL. For example: A2C4E6G8I0K

slack_workspace_id*: The ID of the Slack workspace authorized with AWS Chatbot. To get the workspace ID, you must perform the initial authorization flow with Slack in the AWS Chatbot console. Then you can copy and paste the workspace ID from the console. For example: Z1Y2X3W4V

logging_level: Specifies the logging level for this configuration. This property affects the log entries pushed to Amazon CloudWatch Logs. Logging levels include ERROR, INFO, or NONE. Default: ERROR

tags: Additional tags (e.g. map('BusinessUnit','XYZ'). Default:

{
"Terraform" : true
}
`

Note: Required Terraform version >= 0.12 and also you can provide the AWS access details or it will using the default configuration.

Then open Terminal in the Module location:

terraform init
then

terraform plan
then

terraform apply --auto-approve

You will see this message:

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

It will create a stack of an SNS topic, an IAM role, Configured the slack channel to Chatbot, and also will create an SNS-topic Subscriptions for this Slack channel.

Image description

Image description

Image description

So you have successfully configured the Slack chennel to AWS Chatbot!

Step-4: Test Slack Chennel with test notification

Open the chennel configuration from AWS Chatbot and click the Send test message button.

Image description

It will despatched a test message for the SNS topic and SNS topic will send everything to his subscribers and AWS Chatbot will communicating to slack!

Image description

You will get the message in the slack channel immediately!

Image description

So thest has been successfully completed! Now using this SNS topic you can configure all of your CludWatch Alert. Now you just need to know how to create a CloudWatch Alert.

Note: You can configure as many channel as you want by using this Module. Just fllow the Step-3 for the other channel.

Summanry

AWS Chatbot uses Amazon Simple Notification Service (Amazon SNS) topics to send event and alarm notifications from AWS services to Slack. By configuring Slack Channel and SNS topic you can send of your every alert and notification to Slack. And you will be aware of everything for your AWS Infrastructure.

To learn more, read the AWS Chatbot documentation.

Thanks for reading! Happy Cloud Computing!

Connect with me: Linkedin

Top comments (0)