From 796986275141d1662f9747e057d4e8791c8d87f4 Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Sun, 17 Dec 2023 16:58:36 +0100 Subject: [PATCH 1/5] Present the approval notifications in a slightly better way --- lambdas/notifier/notifier.py | 33 +++++++++++++++++++++++++-------- lambdas/notifier/tests.py | 2 +- main.tf | 33 +++++++++++++++++++++++++++++---- variables.tf | 5 ++++- 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/lambdas/notifier/notifier.py b/lambdas/notifier/notifier.py index 7813f85..5e57d61 100644 --- a/lambdas/notifier/notifier.py +++ b/lambdas/notifier/notifier.py @@ -11,12 +11,19 @@ logger = logging.getLogger(__name__) STATE_COLORS = { - "STARTED": "#1a9edb", - "SUCCEEDED": "#50ba32", - "RESUMED": "#1a9edb", - "FAILED": "#f02b1d", - "CANCELED": "#919191", - "SUPERSEDED": "#919191", + "Deployment": { + "STARTED": "#1a9edb", + "SUCCEEDED": "#50ba32", + "RESUMED": "#1a9edb", + "FAILED": "#f02b1d", + "CANCELED": "#919191", + "SUPERSEDED": "#919191", + }, + "Approval": { + "STARTED": "#f5d142", + "SUCCEEDED": "#50ba32", + "FAILED": "#f02b1d", + }, } @@ -31,6 +38,7 @@ def format_slack_attachment( execution_id: str, environment: str, region: str, + action: str, revision_summary: t.Optional[str], revision_url: t.Optional[str], ) -> dict: @@ -57,7 +65,7 @@ def format_slack_attachment( }, ] return { - "color": STATE_COLORS[pipeline_state], + "color": STATE_COLORS[action][pipeline_state], "fallback": (f"`{pipeline_name}` has `{pipeline_state}`"), "fields": [ {"title": "Pipeline", "value": pipeline_name}, @@ -70,6 +78,13 @@ def format_slack_attachment( def format_slack_text(*, pipeline_name: str, pipeline_state: str, action: str): + if action == "Approval": + if pipeline_state == "STARTED": + return f"*{pipeline_name}* is awaiting approval." + elif pipeline_state == "FAILED": + return f"*{pipeline_name}* has rejected." + elif pipeline_state == "SUCCEEDED": + return f"*{pipeline_name}* has been approved." return f"*{action}* of *{pipeline_name}* has {pipeline_state.lower()}." @@ -90,18 +105,20 @@ def build_slack_message_from_event(event): revision = pipeline_execution["artifactRevisions"][0] revision_url = revision.get("revisionUrl") revision_summary = revision.get("revisionSummary") + action = pipeline_action or "Deployment" # Build a message with an attachment with details text = format_slack_text( pipeline_name=pipeline_name, pipeline_state=pipeline_state, - action=pipeline_action or "Deployment", + action=action, ) attachment = format_slack_attachment( pipeline_name=pipeline_name, pipeline_state=pipeline_state, execution_id=execution_id, region=region, + action=action, revision_summary=revision_summary, revision_url=revision_url, environment=os.environ["ENVIRONMENT"], diff --git a/lambdas/notifier/tests.py b/lambdas/notifier/tests.py index c20a706..93353c2 100644 --- a/lambdas/notifier/tests.py +++ b/lambdas/notifier/tests.py @@ -67,7 +67,7 @@ PIPELINE_EXECUTION = { "service_response": { "pipelineExecution": { - "pipelineName": "gina-qa-storefront", + "pipelineName": "kjagiello-test-pipeline", "pipelineVersion": 20, "pipelineExecutionId": "7911f31b-9991-42c2-90db-a5c20ad62b82", "status": "Succeeded", diff --git a/main.tf b/main.tf index 0d93e40..89f23ed 100644 --- a/main.tf +++ b/main.tf @@ -20,12 +20,15 @@ resource "aws_sns_topic_subscription" "pipeline_updates" { } resource "aws_cloudwatch_event_rule" "pipeline_updates" { - name = module.subscription_label.id + name = "${module.subscription_label.id}-pipeline" tags = module.this.tags event_pattern = jsonencode({ - source = ["aws.codepipeline"] - detail-type = ["CodePipeline Pipeline Execution State Change"], + source = ["aws.codepipeline"] + detail-type = [ + "CodePipeline Pipeline Execution State Change", + ] detail = { + state = [for id in var.event_type_ids : upper(id)] pipeline = var.codepipelines.*.name } }) @@ -34,7 +37,29 @@ resource "aws_cloudwatch_event_rule" "pipeline_updates" { resource "aws_cloudwatch_event_target" "pipeline_updates" { rule = aws_cloudwatch_event_rule.pipeline_updates.name arn = aws_sns_topic.pipeline_updates.arn - target_id = module.subscription_label.id + target_id = "${module.subscription_label.id}-pipeline" +} + +resource "aws_cloudwatch_event_rule" "approval_updates" { + name = "${module.subscription_label.id}-approval" + tags = module.this.tags + event_pattern = jsonencode({ + source = ["aws.codepipeline"] + detail-type = [ + "CodePipeline Action Execution State Change", + ] + detail = { + pipeline = var.codepipelines.*.name + type = { + category = ["Approval"] + } + } + }) +} + +resource "aws_cloudwatch_event_target" "approval_updates" { + rule = aws_cloudwatch_event_rule.approval_updates.name + arn = aws_sns_topic.pipeline_updates.arn } resource "aws_sns_topic_policy" "pipeline_updates" { diff --git a/variables.tf b/variables.tf index 2122771..146b20f 100644 --- a/variables.tf +++ b/variables.tf @@ -45,7 +45,10 @@ variable "event_type_ids" { "started", "resumed", "succeeded", - "superseded" + "superseded", + "stopping", + "stopped", + "abandoned" ]) ) == 0 error_message = <<-EOF From a37bb036346f37b017ca2dea752b9a7c5461677f Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Sun, 17 Dec 2023 17:26:58 +0100 Subject: [PATCH 2/5] fixup! Present the approval notifications in a slightly better way --- lambdas/notifier/notifier.py | 12 +- lambdas/notifier/tests.py | 311 ++++++++++++++++++++++++++--------- 2 files changed, 239 insertions(+), 84 deletions(-) diff --git a/lambdas/notifier/notifier.py b/lambdas/notifier/notifier.py index 5e57d61..a7f6713 100644 --- a/lambdas/notifier/notifier.py +++ b/lambdas/notifier/notifier.py @@ -66,7 +66,11 @@ def format_slack_attachment( ] return { "color": STATE_COLORS[action][pipeline_state], - "fallback": (f"`{pipeline_name}` has `{pipeline_state}`"), + "fallback": format_slack_text( + pipeline_name=pipeline_name, + pipeline_state=pipeline_state, + action=action, + ).replace("*", ""), "fields": [ {"title": "Pipeline", "value": pipeline_name}, {"title": "Execution ID", "value": execution_link}, @@ -80,11 +84,11 @@ def format_slack_attachment( def format_slack_text(*, pipeline_name: str, pipeline_state: str, action: str): if action == "Approval": if pipeline_state == "STARTED": - return f"*{pipeline_name}* is awaiting approval." + return f"*Deployment* of *{pipeline_name}* is awaiting approval." elif pipeline_state == "FAILED": - return f"*{pipeline_name}* has rejected." + return f"*Deployment* of *{pipeline_name}* has been rejected." elif pipeline_state == "SUCCEEDED": - return f"*{pipeline_name}* has been approved." + return f"*Deployment* of *{pipeline_name}* has been approved." return f"*{action}* of *{pipeline_name}* has {pipeline_state.lower()}." diff --git a/lambdas/notifier/tests.py b/lambdas/notifier/tests.py index 93353c2..b9d225a 100644 --- a/lambdas/notifier/tests.py +++ b/lambdas/notifier/tests.py @@ -17,7 +17,7 @@ "ENVIRONMENT": "qa", } -TEST_MESSAGE = { +TEST_PIPELINE_UPDATE = { "version": "0", "id": "81ff0678-5b31-cfc8-0ac7-df8664cd764a", "detail-type": "CodePipeline Pipeline Execution State Change", @@ -34,35 +34,60 @@ }, } -TEST_EVENT = { - "Records": [ - { - "EventSource": "aws:sns", - "EventVersion": "1.0", - "EventSubscriptionArn": ( - "arn:aws:sns:eu-west-1:203144576027" - ":kjagiello-qa-codepipeline-notifications-test-pipeline-updates" - ":6f7fa819-b7ae-499c-ad67-9aa4fc7a732d" - ), - "Sns": { - "Type": "Notification", - "MessageId": "a32e2bde-ed90-5cbf-87ba-7d5e77e56761", - "TopicArn": ( + +def pipeline_approval(state): + return { + "version": "0", + "id": "81ff0678-5b31-cfc8-0ac7-df8664cd764a", + "detail-type": "CodePipeline Action Execution State Change", + "source": "aws.codepipeline", + "account": "203144576027", + "time": "2019-12-27T23:47:58Z", + "region": "eu-west-1", + "resources": [ + "arn:aws:codepipeline:eu-west-1:203144576027:kjagiello-qa-homepage" + ], + "detail": { + "pipeline": "kjagiello-qa-homepage", + "execution-id": "0aeb1e48-4de1-4d3c-8815-4a8a701b1fcc", + "state": state, + "version": 20.0, + "action": "Approval", + }, + } + + +def wrap_event(event): + return { + "Records": [ + { + "EventSource": "aws:sns", + "EventVersion": "1.0", + "EventSubscriptionArn": ( "arn:aws:sns:eu-west-1:203144576027" ":kjagiello-qa-codepipeline-notifications-test-pipeline-updates" + ":6f7fa819-b7ae-499c-ad67-9aa4fc7a732d" ), - "Subject": None, - "Message": json.dumps(TEST_MESSAGE), - "Timestamp": "2019-12-27T23:48:05.126Z", - "SignatureVersion": "1", - "Signature": "signature", - "SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/...", - "UnsubscribeUrl": "https://sns.eu-west-1.amazonaws.com/...", - "MessageAttributes": {}, - }, - } - ] -} + "Sns": { + "Type": "Notification", + "MessageId": "a32e2bde-ed90-5cbf-87ba-7d5e77e56761", + "TopicArn": ( + "arn:aws:sns:eu-west-1:203144576027" + ":kjagiello-qa-codepipeline-notifications-test-pipeline-updates" + ), + "Subject": None, + "Message": json.dumps(event), + "Timestamp": "2019-12-27T23:48:05.126Z", + "SignatureVersion": "1", + "Signature": "signature", + "SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/...", + "UnsubscribeUrl": "https://sns.eu-west-1.amazonaws.com/...", + "MessageAttributes": {}, + }, + } + ] + } + PIPELINE_EXECUTION = { "service_response": { @@ -87,63 +112,189 @@ }, } +TEST_MATRIX = [ + ( + TEST_PIPELINE_UPDATE, + { + "channel": "#notifications", + "username": "Mr. Robot", + "icon_emoji": ":rocket:", + "text": "*Deployment* of *kjagiello-qa-homepage* has started.", + "attachments": [ + { + "color": "#1a9edb", + "fallback": "Deployment of kjagiello-qa-homepage has started.", + "fields": [ + {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, + { + "title": "Execution ID", + "value": ( + "" + ), + }, + {"title": "Environment", "value": "QA", "short": True}, + {"title": "Region", "value": "eu-west-1", "short": True}, + { + "title": "Code revision", + "value": ( + "commit message\n\n" + "" + ), + }, + ], + } + ], + }, + ), + ( + pipeline_approval("STARTED"), + { + "channel": "#notifications", + "username": "Mr. Robot", + "icon_emoji": ":rocket:", + "text": "*Deployment* of *kjagiello-qa-homepage* is awaiting approval.", + "attachments": [ + { + "color": "#f5d142", + "fallback": "Deployment of kjagiello-qa-homepage is awaiting approval.", + "fields": [ + {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, + { + "title": "Execution ID", + "value": ( + "" + ), + }, + {"title": "Environment", "value": "QA", "short": True}, + {"title": "Region", "value": "eu-west-1", "short": True}, + { + "title": "Code revision", + "value": ( + "commit message\n\n" + "" + ), + }, + ], + } + ], + }, + ), + ( + pipeline_approval("SUCCEEDED"), + { + "channel": "#notifications", + "username": "Mr. Robot", + "icon_emoji": ":rocket:", + "text": "*Deployment* of *kjagiello-qa-homepage* has been approved.", + "attachments": [ + { + "color": "#50ba32", + "fallback": "Deployment of kjagiello-qa-homepage has been approved.", + "fields": [ + {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, + { + "title": "Execution ID", + "value": ( + "" + ), + }, + {"title": "Environment", "value": "QA", "short": True}, + {"title": "Region", "value": "eu-west-1", "short": True}, + { + "title": "Code revision", + "value": ( + "commit message\n\n" + "" + ), + }, + ], + } + ], + }, + ), + ( + pipeline_approval("FAILED"), + { + "channel": "#notifications", + "username": "Mr. Robot", + "icon_emoji": ":rocket:", + "text": "*Deployment* of *kjagiello-qa-homepage* has been rejected.", + "attachments": [ + { + "color": "#f02b1d", + "fallback": "Deployment of kjagiello-qa-homepage has been rejected.", + "fields": [ + {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, + { + "title": "Execution ID", + "value": ( + "" + ), + }, + {"title": "Environment", "value": "QA", "short": True}, + {"title": "Region", "value": "eu-west-1", "short": True}, + { + "title": "Code revision", + "value": ( + "commit message\n\n" + "" + ), + }, + ], + } + ], + }, + ), +] + @mock.patch.dict(os.environ, ENVIRONMENT) class TestNotifier(unittest.TestCase): - @mock.patch("urllib.request.urlopen") - @mock.patch("notifier.get_codepipeline_client") - def test_send_notification_for_event(self, codepipeline_mock, urlopen_mock): - cm = mock.MagicMock() - cm.getcode.return_value = 200 - cm.read.return_value = b"ok" - urlopen_mock.return_value = cm + def test_send_notification_for_event(self): + self.maxDiff = None + for event, slack_message in TEST_MATRIX: + with self.subTest(event): + with mock.patch("urllib.request.urlopen") as urlopen_mock, mock.patch( + "notifier.get_codepipeline_client" + ) as codepipeline_mock: + cm = mock.MagicMock() + cm.getcode.return_value = 200 + cm.read.return_value = b"ok" + urlopen_mock.return_value = cm - codepipeline_mock.return_value = boto3.client("codepipeline") - with Stubber(codepipeline_mock.return_value) as stubber: - stubber.add_response("get_pipeline_execution", **PIPELINE_EXECUTION) - notifier.handler(event=TEST_EVENT, context={}) + codepipeline_mock.return_value = boto3.client("codepipeline") + with Stubber(codepipeline_mock.return_value) as stubber: + stubber.add_response( + "get_pipeline_execution", **PIPELINE_EXECUTION + ) + notifier.handler(event=wrap_event(event), context={}) - urlopen_mock.assert_called_once() - request = urlopen_mock.call_args[0][0] - self.assertEqual(request.full_url, ENVIRONMENT["SLACK_WEBHOOK_URL"]) - self.assertEqual(request.get_header("Content-type"), "application/json") - self.assertEqual( - json.loads(request.data), - { - "channel": "#notifications", - "username": "Mr. Robot", - "icon_emoji": ":rocket:", - "text": "*Deployment* of *kjagiello-qa-homepage* has started.", - "attachments": [ - { - "color": "#1a9edb", - "fallback": "`kjagiello-qa-homepage` has `STARTED`", - "fields": [ - {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, - { - "title": "Execution ID", - "value": ( - "" - ), - }, - {"title": "Environment", "value": "QA", "short": True}, - {"title": "Region", "value": "eu-west-1", "short": True}, - { - "title": "Code revision", - "value": ( - "commit message\n\n" - "" - ), - }, - ], - } - ], - }, - ) + urlopen_mock.assert_called_once() + request = urlopen_mock.call_args[0][0] + self.assertEqual(request.full_url, ENVIRONMENT["SLACK_WEBHOOK_URL"]) + self.assertEqual( + request.get_header("Content-type"), "application/json" + ) + self.assertEqual( + json.loads(request.data), + slack_message, + ) @mock.patch("urllib.request.urlopen") @mock.patch("notifier.get_codepipeline_client") @@ -158,7 +309,7 @@ def test_handles_missing_revision_gracefully(self, codepipeline_mock, urlopen_mo codepipeline_mock.return_value = boto3.client("codepipeline") with Stubber(codepipeline_mock.return_value) as stubber: stubber.add_response("get_pipeline_execution", **pipeline_execution) - notifier.handler(event=TEST_EVENT, context={}) + notifier.handler(event=wrap_event(TEST_PIPELINE_UPDATE), context={}) urlopen_mock.assert_called_once() request = urlopen_mock.call_args[0][0] From f236aac660e6e2b2d46726a33d16ebb387fa7aa3 Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Sun, 17 Dec 2023 17:30:21 +0100 Subject: [PATCH 3/5] fixup! fixup! Present the approval notifications in a slightly better way --- lambdas/notifier/tests.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lambdas/notifier/tests.py b/lambdas/notifier/tests.py index b9d225a..a542d23 100644 --- a/lambdas/notifier/tests.py +++ b/lambdas/notifier/tests.py @@ -160,7 +160,9 @@ def wrap_event(event): "attachments": [ { "color": "#f5d142", - "fallback": "Deployment of kjagiello-qa-homepage is awaiting approval.", + "fallback": ( + "Deployment of kjagiello-qa-homepage is awaiting approval.", + ), "fields": [ {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, { @@ -197,7 +199,9 @@ def wrap_event(event): "attachments": [ { "color": "#50ba32", - "fallback": "Deployment of kjagiello-qa-homepage has been approved.", + "fallback": ( + "Deployment of kjagiello-qa-homepage has been approved." + ), "fields": [ {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, { @@ -234,7 +238,9 @@ def wrap_event(event): "attachments": [ { "color": "#f02b1d", - "fallback": "Deployment of kjagiello-qa-homepage has been rejected.", + "fallback": ( + "Deployment of kjagiello-qa-homepage has been rejected." + ), "fields": [ {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, { From b2c136e2ccd0afa633fada617c696ceb2281cf04 Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Sun, 17 Dec 2023 17:31:31 +0100 Subject: [PATCH 4/5] fixup! fixup! fixup! Present the approval notifications in a slightly better way --- lambdas/notifier/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambdas/notifier/tests.py b/lambdas/notifier/tests.py index a542d23..1efdcd2 100644 --- a/lambdas/notifier/tests.py +++ b/lambdas/notifier/tests.py @@ -161,7 +161,7 @@ def wrap_event(event): { "color": "#f5d142", "fallback": ( - "Deployment of kjagiello-qa-homepage is awaiting approval.", + "Deployment of kjagiello-qa-homepage is awaiting approval." ), "fields": [ {"title": "Pipeline", "value": "kjagiello-qa-homepage"}, From b1eef020b5780d3088a016a9e56df69bd283729d Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Sun, 17 Dec 2023 17:53:27 +0100 Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! Present the approval notifications in a slightly better way --- README.md | 3 ++- main.tf | 3 ++- variables.tf | 41 +++++++++++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 173a219..b6a69b4 100644 --- a/README.md +++ b/README.md @@ -44,19 +44,20 @@ module "codepipeline_notifications" { | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no | +| [approval\_event\_type\_ids](#input\_approval\_event\_type\_ids) | The list of pipeline events to trigger a notification on | `list(string)` |
[
"started",
"succeeded",
"failed"
]
| no | | [attributes](#input\_attributes) | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no | | [codepipelines](#input\_codepipelines) | CodePipeline resources that should trigger Slack notifications | `list(any)` | n/a | yes | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {}
}
| no | | [delimiter](#input\_delimiter) | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | -| [event\_type\_ids](#input\_event\_type\_ids) | The list of event type to trigger a notification on | `list(string)` |
[
"failed",
"canceled",
"started",
"resumed",
"succeeded",
"superseded"
]
| no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for default, which is `0`.
Does not affect `id_full`. | `number` | `null` | no | | [label\_key\_case](#input\_label\_key\_case) | The letter case of label keys (`tag` names) (i.e. `name`, `namespace`, `environment`, `stage`, `attributes`) to use in `tags`.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The naming order of the id output and Name tag.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | The letter case of output label values (also used in `tags` and `id`).
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Default value: `lower`. | `string` | `null` | no | | [name](#input\_name) | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no | | [namespace](#input\_namespace) | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no | +| [pipeline\_event\_type\_ids](#input\_pipeline\_event\_type\_ids) | The list of pipeline events to trigger a notification on | `list(string)` |
[
"started",
"failed",
"canceled",
"resumed",
"succeeded",
"superseded"
]
| no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [slack\_channel](#input\_slack\_channel) | A slack channel to send the deployment notifications to | `string` | n/a | yes | | [slack\_emoji](#input\_slack\_emoji) | The emoji avatar of the user that sends the notifications | `string` | `":rocket:"` | no | diff --git a/main.tf b/main.tf index 89f23ed..d990d18 100644 --- a/main.tf +++ b/main.tf @@ -28,8 +28,8 @@ resource "aws_cloudwatch_event_rule" "pipeline_updates" { "CodePipeline Pipeline Execution State Change", ] detail = { - state = [for id in var.event_type_ids : upper(id)] pipeline = var.codepipelines.*.name + state = [for id in var.pipeline_event_type_ids : upper(id)] } }) } @@ -50,6 +50,7 @@ resource "aws_cloudwatch_event_rule" "approval_updates" { ] detail = { pipeline = var.codepipelines.*.name + state = [for id in var.approval_event_type_ids : upper(id)] type = { category = ["Approval"] } diff --git a/variables.tf b/variables.tf index 146b20f..6afe39c 100644 --- a/variables.tf +++ b/variables.tf @@ -25,13 +25,13 @@ variable "slack_emoji" { default = ":rocket:" } -variable "event_type_ids" { +variable "pipeline_event_type_ids" { type = list(string) - description = "The list of event type to trigger a notification on" + description = "The list of pipeline events to trigger a notification on" default = [ + "started", "failed", "canceled", - "started", "resumed", "succeeded", "superseded" @@ -39,21 +39,42 @@ variable "event_type_ids" { validation { condition = length( - setsubtract(var.event_type_ids, [ + setsubtract(var.pipeline_event_type_ids, [ + "started", "failed", "canceled", - "started", "resumed", "succeeded", - "superseded", - "stopping", - "stopped", - "abandoned" + "superseded" + ]) + ) == 0 + error_message = <<-EOF + Invalid event type IDs found. + Allowed type IDs: started, failed, canceled, resumed, succeeded, superseded. + EOF + } +} + +variable "approval_event_type_ids" { + type = list(string) + description = "The list of pipeline events to trigger a notification on" + default = [ + "started", + "succeeded", + "failed", + ] + + validation { + condition = length( + setsubtract(var.approval_event_type_ids, [ + "started", + "succeeded", + "failed", ]) ) == 0 error_message = <<-EOF Invalid event type IDs found. - Allowed type IDs: failed, canceled, started, resumed, succeeded, superseded. + Allowed type IDs: started, succeeded, failed. EOF } }