让我们尝试使用Terraform来管理Slack应用程序
大家有在使用Slack App吗?在Slack上能够完成各种业务非常方便。我也将其作为CI/CD和业务效率化工具的一部分使用。
虽然 Slack 应用非常方便,但由于我之前一直手动管理,所以在新建应用时遇到了很多困难。
- 
- 設定項目ぽちぽちするのが面倒
 
- Slack App 以外のリソース (AWS Lambdaなど) を別途用意する必要があり面倒
我在这次的项目中,为了解决这些问题,尝试使用 Terraform 来管理 Slack App,现在向大家介绍一下。
使用的是以下的机制。
- 
- Slack App Manifest
 
- Terraform null_resource
我们将依次解释并总结步骤。
Slack应用清单
Slack App 可以用 JSON 或 YAML 格式来定义。有以下两种使用方法。
- 
- WebのUIから利用する
 
API をcurlなどで直接叩いて利用する
由于我们希望通过 Terraform 进行使用,所以这次我们将使用 API。
可以在此页面上查看用于定义的清单模式。
使用Terraform的null_resource
这是一个无操作的Terraform资源。由于与其他Terraform资源具有相同的生命周期管理,在想要执行某些操作以创建任何资源时非常方便。
通过同时使用 null_resource 和 local-exec Provisioner,可以实现执行Shell脚本的功能。本次我们将利用这个机制来调用Manifest API,创建一个Slack App。
操作步骤
1. 获取访问令牌
首先,获取用于使用Manifest API的访问令牌。
您可以从此页面获取访问令牌。
(※由于访问令牌的有效期为12小时,请在相同页面上进行令牌的更新(刷新))
2. 制作Manifest
在应用程序的设置中,我们将创建一个名为 Manifest 的文件。
这次是创建一个带有斜杠命令功能的 Slack 应用程序的例子。
其他可定义的值请参考官方页面。
{
  "manifest": {
    "display_information": {
      "name": "${app_name}",
      "description": "${description}",
    },
    "features": {
      "bot_user": {
        "display_name": "${bot_name}"
      },
      "slash_commands": ${jsonencode(slash_commands)}
    },
    "oauth_config": {
      "scopes": {
        "bot": ${jsonencode(bot_scopes)}
      },
    }
  }
}
3. Terraform设置的一方。
我们将定义适用于Slack App的Terraform资源。
重要的部分是在null_resource中使用local-exec Provisioner来执行curl命令。
为了避免在步骤1中不小心提交访问令牌,建议通过环境变量(如TF_VAR_access_token)传递。
variable app_name {
  type = string
}
variable bot_name {
  type = string
}
variable description {
  type = string
}
variable bot_scopes {
  type = list(string)
}
variable slash_commands {
  type = list(map(string))
}
// access_token は TF_VAR_access_token の環境変数経由とかで渡してください
variable access_token {
  type      = string
  sensitive = true
}
locals {
  template = templatefile(
    "templates/app_manifest.json.tftpl",
    {
      app_name       = var.app_name
      bot_name       = var.bot_name
      description    = var.description
      slash_commands = var.slash_commands
      bot_scopes     = var.bot_scopes
    }
  )
}
## メインのリソース.curl を叩いててリクエストボディに app_manifest.json を渡している
resource "null_resource" "slack_app" {
  provisioner "local-exec" {
    command = "curl -XPOST -H \"Authorization: Bearer ${var.access_token}\" -H \"Content-type: application/json\" -d '${local.template}' https://slack.com/api/apps.manifest.create"
  }
}
- tfvarsをよしなに定義
app_name    = "test_app"
bot_name    = "test_app_user"
description = "test app"
bot_scopes  = [
  "channels:read",
  "chat:write",
  "chat:write.public",
  "commands"
]
slash_commands = [
  {
    "command" : "/hoge",
    "description" : "hoge",
    "usage_hint" : "/hoge echo hoge",
    "url" : "https://example.com/api/hoge"
  }
]
4. 执行地球形态化
用bash或其他方式执行”terraform apply -var-file main.tfvars”。
结束
我已经解释了如何使用Terraform来管理Slack应用程序。通过使用Terraform进行管理,能够一并创建周边资源,非常方便。这样一来,在创建类似配置的应用程序时,工作量将大大减少。
然而,由于访问令牌每12小时更换一次,与Github Actions等CICD工具的组合是困难的。
此外,这次我们介绍了从null_resource执行bash的简便方法,但无法进行更新或删除操作…
如果自己创建Slack App的provider,则可以实现这一点,但还需另外的机会。
祝你在Slack上享受美好的应用体验!
 
    