Skip to content

pappacena/terraform-aws-codepipeline-slack-notifications

 
 

Repository files navigation

terraform-aws-codepipeline-slack-notifications

Github Actions

A terraform module to set up Slack notifications for your AWS CodePipelines. Available through the Terraform registry.

image

Usage

resource "aws_codepipeline" "example" {
  // ...
}

module "codepipeline_notifications" {
  source  = "pappacena/codepipeline-slack-notifications/aws"
  version = "0.0.2"
  notification_rule_name = "notification-rule"

  name          = "codepipeline-notifications"
  namespace     = "pappacena"
  stage         = "sandbox"
  slack_url     = "https://hooks.slack.com/services/(...)"
  slack_channel = "#notifications"
  codepipelines = [
    aws_codepipeline.example,
  ]
}

Beware that during the initial apply, it might fail with following error:

Error: error creating codestar notification rule: ConfigurationException: AWS CodeStar Notifications could not create the AWS CloudWatch Events managed rule in your AWS account. If this is your first time creating a notification rule, the service-linked role for AWS CodeStar Notifications might not yet exist. Creation of this role might take up to 15 minutes. Until it exists, notification rule creation will fail. Wait 15 minutes, and then try again. If this is is not the first time you are creating a notification rule, there might be a problem with a network connection, or one or more AWS services might be experiencing issues. Verify your network connection and check to see if there are any issues with AWS services in your AWS Region before trying again.

This is due to this module using AWS CodeStar for subscribing to the CodePipeline state changes. The first use of a CodeStar resource automatically creates the required service-linked role, which typically is nearly instantaneous. Just reapply your Terraform plan and you should be good to go.

Multiple notifications

It is possible to notify different channels for different actions. For example, to notify failures to #pipeline-errors and everything else to #pipeline-info:

resource "aws_codepipeline" "example" {
  // ...
}

module "codepipeline_notifications" {
  source  = "pappacena/codepipeline-slack-notifications/aws"
  version = "0.0.2"
  notification_rule_name = "notification-rule-info"

  name          = "codepipeline-notifications"
  namespace     = "pappacena"
  stage         = "sandbox"
  slack_url     = "https://hooks.slack.com/services/(...)"
  slack_channel = "#notifications"
  codepipelines = [
    aws_codepipeline.example,
  ]
  event_type_ids = [
    "codepipeline-pipeline-pipeline-execution-started",
    "codepipeline-pipeline-pipeline-execution-resumed",
    "codepipeline-pipeline-pipeline-execution-succeeded",
    "codepipeline-pipeline-pipeline-execution-superseded"
  ]
}

module "codepipeline_notifications" {
  source  = "pappacena/codepipeline-slack-notifications/aws"
  version = "0.0.2"
  notification_rule_name = "notification-rule-error"

  name          = "codepipeline-notifications"
  namespace     = "pappacena"
  stage         = "sandbox"
  slack_url     = "https://hooks.slack.com/services/(...)"
  slack_channel = "#notifications"
  codepipelines = [
    aws_codepipeline.example,
  ]
  event_type_ids = [
    "codepipeline-pipeline-pipeline-execution-canceled",
    "codepipeline-pipeline-pipeline-execution-failed"
  ]
}

Note the different event_type_ids configs, and notification_rule_name, which should be unique.

Module documentation

Requirements

Name Version
terraform >= 0.12
archive >= 1.3
aws >= 2.70, < 5.0

Providers

Name Version
archive >= 1.3
aws >= 2.70, < 5.0

Inputs

Name Description Type Default Required
attributes List of attributes to add to label list(any) [] no
codepipelines CodePipeline resources that should trigger Slack notifications list(any) n/a yes
event_type_ids The list of event type to trigger a notification on list(any)
[
"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"
]
no
name Name (unique identifier for app or service) string n/a yes
notification_rule_name Unique identifier for the notification rule.
Needed in case you have more than 1 notification for the same pipeline string n/a yes
namespace Namespace (e.g. skynet) string n/a yes
slack_channel A slack channel to send the deployment notifications to string n/a yes
slack_emoji The emoji avatar of the user that sends the notifications string ":rocket:" no
slack_url Slack webhook URL for deploy notifications string n/a yes
slack_username The name of the user that sends the notifications string "Deploy Bot" no
stage Stage (e.g. prod, dev, staging) string n/a yes
tags Additional tags (e.g. map('BusinessUnit','XYZ')) map(string) {} no

Outputs

No outputs.

About

Slack notifications for your AWS CodePipelines.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 62.6%
  • HCL 37.4%