Terraform で Codeシリーズの Notifications を作り Slack 通知させてみた

CodePipeline,CodeCommitのイベント通知をSlackで簡単に受け取れます
2021.01.26

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ちゃだいん(@chazuke4649)です。

先日、Terraform とマネジメントコンソールを使って、Codeシリーズの通知まわりを作る機会がありましたのでご紹介します。

前提

下図構成のCodeシリーズを使用したCI/CDパイプラインの、赤枠部分を作ります。

赤矢印が通知の流れです。すでにCodeCommit,CodePipeline,ECSなどは作成済みであり、それらの構成に追加します。

Terraform と Provider のバージョンは以下のように指定しています。

terraform {
  required_version = "= 0.13.5"

  required_providers {
    aws     = "~> 3.18"    
  }

1.SNSトピックを作成する

まずは、SNSトピックを作成します。 以下tfファイルを実行します。

# SNS topic for Slack: project-dev-pipeline-app

resource "aws_sns_topic" "codeseries_notif" {
  name = "codeseries-notifications-for-app"
}

data "aws_iam_policy_document" "notif_access" {
  statement {
    actions = ["sns:Publish"]

    principals {
      type        = "Service"
      identifiers = ["codestar-notifications.amazonaws.com"]
    }

    resources = [aws_sns_topic.codeseries_notif.arn]
  }
}

resource "aws_sns_topic_policy" "default" {
  arn    = aws_sns_topic.codeseries_notif.arn
  policy = data.aws_iam_policy_document.notif_access.json
}

トピックポリシーとして、codestar-notificationsサービスを許可しています。

次に作成する Notifications の実体は CodeStar サービスです。Notifications に関しての詳細は以下エントリがわかりやすいのでご一読ください。

2.Notificationsを作成する

次に、Notificationsを作成します。 以下tfファイルを実行します。

## Notification: CodeCommit

resource "aws_codestarnotifications_notification_rule" "commits" {
  detail_type    = "BASIC"
  event_type_ids = [
    "codecommit-repository-pull-request-created",
    "codecommit-repository-pull-request-source-updated",
    "codecommit-repository-pull-request-status-changed",
    "codecommit-repository-pull-request-merged",
    "codepipeline-pipeline-manual-approval-failed",
    "codepipeline-pipeline-manual-approval-needed",
    "codepipeline-pipeline-manual-approval-succeeded",
    ]

  name     = "codecommit-notify-for-project"
  resource = "arn:aws:codecommit:ap-northeast-1:123456789012:project"

  target {
    address = aws_sns_topic.codeseries_notif.arn
  }
}


## Notification: CodePipeline app

resource "aws_codestarnotifications_notification_rule" "pipeline" {
  detail_type    = "BASIC"
  event_type_ids = [
    "codepipeline-pipeline-stage-execution-started",
    "codepipeline-pipeline-stage-execution-succeeded",
    "codepipeline-pipeline-stage-execution-resumed",
    "codepipeline-pipeline-stage-execution-canceled",
    "codepipeline-pipeline-stage-execution-failed",
    "codepipeline-pipeline-pipeline-execution-failed",
    "codepipeline-pipeline-pipeline-execution-canceled",
    "codepipeline-pipeline-pipeline-execution-started",
    "codepipeline-pipeline-pipeline-execution-resumed",
    "codepipeline-pipeline-pipeline-execution-succeeded",
    "codepipeline-pipeline-pipeline-execution-superseded",
    "codepipeline-pipeline-manual-approval-failed",
    "codepipeline-pipeline-manual-approval-needed",
    "codepipeline-pipeline-manual-approval-succeeded",
    ]

  name     = "codepipeline-notify-for-dev-app"
  resource = aws_codepipeline.project-dev-pipeline-app.arn

  target {
    address = aws_sns_topic.codeseries_notif.arn
  }
}

補足としては、一つは CodeCommit にて何かしらのイベントが発生した時に通知ルール、もう一つは CodePipelienのイベントが発生した時の通知ルールで分けています。

どんなCodeシリーズのイベントタイプに対応しているかについては、以下ドキュメントをご参照ください。

Notification concepts - Developer Tools console

3.Chatbotを作成する

現時点では Terraform はまだ Chatbot をサポートしていないので、マネジメントコンソールから作成します。

Chatbotの作成方法は以下エントリがわかりやすいのでご参照ください。

以下エントリはタイトルの通り、Slackの「プライベート」チャンネルの場合のみ気を付ける点について説明されています。

では今回作成した画面は以下の通りです。

ポイントとしては、

  • 通知先のSlackチャンネルIDを Slack側から取得しておき、それを入力する
  • ChatbotのIAMサービスロールは事前に作成しておく

画面最下部に先ほど作成した SNSトピックを指定すれば完了です。

これを作成すると、もちろん SNSトピック側でも、Chatbotがサブスクライバーとして登録されていることを確認できます。

これで準備は完了です。

4.通知テストを行う

全てのリソースが作成・統合されたので、実際に指定したSlackチャンネルに通知が飛ぶかどうか検証します。

すでに作成済みの CodeCommitリポジトリに対して、ローカルリポジトリから、develop-appブランチに対して変更を push しました。

すると、Slackチャンネルには下図の通り一連の通知が届きました。

無事、通知は成功しました。

終わりに

今回は、CI/CDパイプラインのイベントを Slack通知させる方法を行いました。各AWSマネージドサービスと、デプロイに Terraform を使用すれば、とても簡単に実装することができます。ありがたや。

それではこの辺で。ちゃだいん(@chazuke4649)でした。