Quickly Set up Slack Bot for GCP Error Reporting Integration
## Overview
In today’s fast-paced tech environment, effective communication is key to quickly addressing issues. Google Cloud Platform (GCP) Error Reporting is a powerful tool that helps developers monitor and manage errors in their applications. However, the default Slack integration often results in notifications that are not very readable, making it difficult for engineers to quickly identify and respond to issues.
In this post, we will explore how to create a Slack bot that integrates with GCP Error Reporting to enhance the readability of error notifications sent to Slack. We will walk through the deployment of a custom Slack app, gcp-error-reporting-slack-bot, which allows you to format and customize notifications for better clarity.
In addition to the built-in Slack integration, Error Reporting has Webhook integration, so I decided to develop a simple Slack app that receives Error Reporting events via Webhook.
For simplicity this time, we are using Basic Auth for authentication and running the Slack app on Cloud Run.
## Deployment
### 1. Deploying the gcp-error-reporting-slack-bot
The first step is to deploy the Slack bot on Google Cloud Run. Below is a Terraform configuration that sets up the necessary resources:
locals {
gcp_error_reporting_slack_bot_env_vars = {
PROJECT_CHANNEL_MAP = "project_01:CXXXXXXXX,project_02:CZZZZZZZZZ"
DEFAULT_CHANNEL_ID = "CYYYYYYYYY"
BASIC_AUTH_USERNAME = "test-user"
}
gcp_error_reporting_slack_bot_env_secrets = {
SLACK_BOT_TOKEN = google_secret_manager_secret.slack_bot_token_gcp.secret_id
BASIC_AUTH_PASSWORD = google_secret_manager_secret.gcp_error_reporing_basic_auth_password.secret_id
}
}
resource "google_service_account" "gcp_error_reporting_slack_bot" {
account_id = "gcp-error-reporting-slack-bot"
}
resource "google_secret_manager_secret_iam_member" "gcp_error_reporting_slack_bot_is_secret_accessor" {
for_each = {
for secret in [
google_secret_manager_secret.slack_bot_token_gcp,
google_secret_manager_secret.gcp_error_reporing_basic_auth_password,
] : secret.secret_id => secret.project
}
project = each.value
secret_id = each.key
role = "roles/secretmanager.secretAccessor"
member = google_service_account.gcp_error_reporting_slack_bot.member
}
resource "google_cloud_run_v2_service" "gcp_error_reporting_slack_bot" {
name = "gcp-error-reporting-slack-bot"
location = var.region # asia-northeast1
template {
containers {
image = "nakamasato/gcp-error-reporting-slack-bot-dev:1.0.0"
dynamic "env" {
for_each = local.gcp_error_reporting_slack_bot_env_vars
content {
name = env.key
value = env.value
}
}
dynamic "env" {
for_each = local.gcp_error_reporting_slack_bot_env_secrets
content {
name = env.key
value_source {
secret_key_ref {
secret = env.value
version = "latest"
}
}
}
}
}
service_account = google_service_account.gcp_error_reporting_slack_bot.email
}
}
resource "google_cloud_run_service_iam_binding" "gcp_error_reporting_slack_bot_is_public" {
location = google_cloud_run_v2_service.gcp_error_reporting_slack_bot.location
service = google_cloud_run_v2_service.gcp_error_reporting_slack_bot.name
role = "roles/run.invoker"
members = [
"allUsers"
]
}
### 2. Creating the Notification Channel (Webhook)
Next, set up a notification channel with the `webhook_basicauth` type. This will allow GCP to send error reports to your Slack bot. Here’s how to configure it:
data "google_secret_manager_secret_version" "gcp_error_reporing_basic_auth_password" {
secret = "gcp-error-reporing-basic-auth-password"
}
resource "google_monitoring_notification_channel" "gcp_error_reporting_slack_bot" {
display_name = "gcp-error-reporting-slack-bot"
type = "webhook_basicauth"
labels = {
username = "gcp-error-reporting"
url = "${google_cloud_run_v2_service.gcp_error_reporting_slack_bot.uri}/webhook"
}
sensitive_labels {
password = google_secret_manager_secret_version.gcp_error_reporing_basic_auth_password.secret_data
}
}
### 3. Setting Up the Error Reporting Notification Channel
Unfortunately, Terraform does not currently support setting up the Error Reporting notification channel directly (Add support for Error Reporting notification channels). You will need to do this manually through the GCP console.
### 4. Testing the Notification Channel
After setting everything up, test the connection to ensure it works correctly. You can send a test connection from the webhook settings and check Cloud Logging for verification.
You should also see a log entry confirming receipt of the message in Slack, which might initially appear empty until an actual error occurs.
### 5. Viewing Actual Slack Notifications
Once everything is set up and working, you’ll start seeing notifications in your Slack channel that are formatted for better clarity. Here’s an example of what those notifications will look like:
## Summary
By following the steps in this guide, you’ve successfully created a Slack bot that integrates with GCP Error Reporting. This solution not only improves the readability of notifications but also allows for quick identification of issues, facilitating faster resolution. For further customization and enhancements, refer to the resources linked below.
With this setup, you can now efficiently manage error notifications and keep your team informed in real-time!