Terraform Module to Implement Slack Notification Channel Using AWS Chatbot & SNS
Tags: terraform, chatbot, slack, aws
Published: 2022-04-13 19:58:39

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](https://github.com/noyonict/aws-chatbot-slack-channel)** 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7iugfvpwnsiu8ist212l.png)

Select
**Chat client**: Slack
Then Click `Configure client` Button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y1inwjnt1b736185gpc.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jk4w1gykyststa3upqze.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r72m9yvdr5ep867en3ux.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5qpj7ly43rlnxrqnyo1.png)


From the `Integrations` tab click `Add an App` button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/900flkcvkg13duila2q0.png)

Then search `AWS Chatbot` App and Add in your channel.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cafx0uvtw441cl5ew1u3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgq4l389c1kdw1w6mcdx.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40atzge0blvmc3a2kj9m.png)


## **_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"
}
```
**_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qibla02xh7ma1o5ahb9u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdzkxc05xk95771caev0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbn8jfdzehr707dv7pr3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nuetevsie733ttunu7j.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t74vi7lyajg5a8sa4wxu.png)

You will get the message in the slack channel immediately!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qm3mrxi8xmxiqn9guna.png)

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](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ConsoleAlarms.html). 

> 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](https://docs.aws.amazon.com/chatbot/latest/adminguide/what-is.html).

Thanks for reading! Happy Cloud Computing!

Connect with me: [Linkedin](https://www.linkedin.com/in/noyonict/)