This template sets up a notification rule for AWS CodeCommit repository events, publishing notifications to an SNS topic.

Terraform Template

data "aws_iam_policy_document" "notif_access" {

  statement {
    actions = ["sns:Publish"]

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

resource "aws_codecommit_repository" "code" {
  repository_name = "example-code-repo"
}

resource "aws_codestarnotifications_notification_rule" "commits" {
  detail_type = "BASIC"
  event_type_ids = ["codecommit-repository-comments-on-commits"]
  name = "example-code-repo-commits"
  resource = aws_codecommit_repository.code.arn

  target {
    address = aws_sns_topic.notif.arn
  }
}

resource "aws_sns_topic" "notif" {
  name = "notification"
}

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