Integration between AWS and Slack becomes very productive for the team developing, managing, or monitoring some infrastructure. It enhances the capability of fast decision-making and taking action.
There are a couple of ways to configure the integration of AWS and Slack, but in this walk-through, we will integrate the SNS (Simple Notification Service) topic to send notifications directly to Slack.
Below is the architecture we will implement in this Answer. For example, our infrastructure may differ, but the integration process will be the same.
In the example below, when the file is uploaded in the S3 bucket, it will trigger the Lambda function to send a message to SNS. SNS publishes this message to the subscriber (Slack in this case).
To integrate AWS and Slack, follow the steps below:
Configure the Slack setting
Configure the infrastructure
Confirm the subscription
Update the Slack setting
We need to find the Slack webhook URL used as an AWS endpoint. To find the webhook URL, we need to open the Slack channel where we want to receive messages and perform the steps given in the slide below:
Now we have configured the Slack setting, let’s create an infrastructure.
We need to create the following services on AWS:
Create an S3 bucket with a notification to trigger the Lambda function.
Create a Lambda function with the IAM role and the policy permission of S3 and SNS publish access.
Create an SNS topic.
Create a subscriber and set its “protocol” to “https” and “endpoint” to the Slack URL that we have copied above.
Note: We have created the Terraform project in the below playground. We can run it and skip the above step manually.
After completing all the above processes, we will receive a subscription URL in our Slack channel, as shown below:
We need to open the link in the browser to confirm the subscription.
Let’s go back to our Slack channel and change the “SubscribeURL” to “Message”:
In the below playground, we created the Terraform project, which performs the above steps to create AWS services. Click the “Run” button below:
Note:
To run the project, you must execute the
terraform init
command and thenterraform apply
.You need to provide your AWS credential and Slack channel URL, which we got in step 1, at runtime.
resource "aws_iam_role" "demo_slack_sns_lambda_role" { name = "demo-slack-sns-lambda-execution-role" assume_role_policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } EOF } resource "aws_iam_policy" "demo_slack_sns_lambda_policy" { name = "demo-slack-sns-lambda-policy" description = "Policy for demo lambda function" policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "sns:Publish" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject" ], "Resource": "arn:aws:s3:::*" } ] } EOF } resource "aws_iam_role_policy_attachment" "sns_policy_attachment" { role = aws_iam_role.demo_slack_sns_lambda_role.name policy_arn = aws_iam_policy.demo_slack_sns_lambda_policy.arn } resource "aws_lambda_function" "demo_slack_sns_lambda_function" { function_name = "demo-slack-sn-lambda-function" runtime = "python3.10" handler = "lambda_handler.lambda_handler" filename = "lambda_function.zip" source_code_hash = filebase64sha256("lambda_function.zip") role = aws_iam_role.demo_slack_sns_lambda_role.arn environment { variables = { SNS_TOPIC_ARN = aws_sns_topic.demo_slack_topic.arn } } } resource "aws_lambda_permission" "example_lambda_permission" { statement_id = "AllowS3Invocation" action = "lambda:InvokeFunction" function_name = aws_lambda_function.demo_slack_sns_lambda_function.function_name principal = "s3.amazonaws.com" source_arn = aws_s3_bucket.demo_slack_sns_bucket.arn } resource "aws_s3_bucket_notification" "bucket_notification" { bucket = aws_s3_bucket.demo_slack_sns_bucket.id lambda_function { lambda_function_arn = aws_lambda_function.demo_slack_sns_lambda_function.arn events = ["s3:ObjectCreated:*"] } }
After creating services, we need to confirm the subscription and update the Slack settings. Now, we are able to receive notifications in our Slack channel whenever we upload files on our S3 bucket.
Note: Here the name of bucket, Lambda function, and SNS topic are prefixes with
demo_slack_sns
, you can change accordingly.
The step-by-step guide outlines the necessary configurations, from Slack settings to creating AWS services and confirming subscriptions. Configuring AWS to send notifications directly to Slack through SNS topics provides a highly efficient and streamlined workflow for teams involved in developing, managing, or monitoring infrastructure.
Free Resources