Skip to content

Commit 94f9c46

Browse files
authored
CCM-12417: PR Env destroy failure event rule (#763)
1 parent e1ced65 commit 94f9c46

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

infrastructure/terraform/components/acct/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| <a name="input_cost_alarm_recipients"></a> [cost\_alarm\_recipients](#input\_cost\_alarm\_recipients) | A list of email addresses to receive alarm notifications | `list(string)` | `[]` | no |
1919
| <a name="input_cost_anomaly_threshold"></a> [cost\_anomaly\_threshold](#input\_cost\_anomaly\_threshold) | The threshold percentage for cost anomaly detection | `number` | `10` | no |
2020
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
21+
| <a name="input_enable_env_destroy_event_rule"></a> [enable\_env\_destroy\_event\_rule](#input\_enable\_env\_destroy\_event\_rule) | Toggles the creation of the CloudWatch Event Rule for environment destruction failures | `bool` | `false` | no |
2122
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
2223
| <a name="input_group"></a> [group](#input\_group) | The group variables are being inherited from (often synonmous with account short-name) | `string` | n/a | yes |
2324
| <a name="input_initial_cli_secrets_provision_override"></a> [initial\_cli\_secrets\_provision\_override](#input\_initial\_cli\_secrets\_provision\_override) | A map of default value to intialise SSM secret values with. Only useful for initial setup of the account due to lifecycle rules. | `map(string)` | `{}` | no |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
resource "aws_cloudwatch_event_rule" "env_destroy" {
2+
count = var.enable_env_destroy_event_rule ? 1 : 0
3+
name = "${local.csi}-env-destroy"
4+
description = "Forwards Environment Destroy Failed events to Custom Event Bus in Observability Account"
5+
6+
event_pattern = jsonencode({
7+
"source" = ["notify.envDestroyFailed"],
8+
})
9+
}
10+
11+
resource "aws_cloudwatch_event_target" "env_destroy" {
12+
count = var.enable_env_destroy_event_rule ? 1 : 0
13+
rule = aws_cloudwatch_event_rule.env_destroy[0].name
14+
arn = local.event_bus_arn
15+
role_arn = aws_iam_role.env_destroy[0].arn
16+
}
17+
18+
resource "aws_iam_role" "env_destroy" {
19+
count = var.enable_env_destroy_event_rule ? 1 : 0
20+
name = "${local.csi}-env-destroy"
21+
22+
assume_role_policy = jsonencode({
23+
Version = "2012-10-17",
24+
Statement = [{
25+
Effect = "Allow",
26+
Principal = {
27+
Service = "events.amazonaws.com"
28+
},
29+
Action = "sts:AssumeRole"
30+
}]
31+
})
32+
}
33+
34+
resource "aws_iam_policy" "env_destroy" {
35+
count = var.enable_env_destroy_event_rule ? 1 : 0
36+
name = "${local.csi}-env-destroy"
37+
38+
policy = jsonencode({
39+
Version = "2012-10-17",
40+
Statement = [{
41+
Effect = "Allow",
42+
Action = "events:PutEvents",
43+
Resource = local.event_bus_arn
44+
}]
45+
})
46+
}
47+
48+
resource "aws_iam_role_policy_attachment" "env_destroy" {
49+
count = var.enable_env_destroy_event_rule ? 1 : 0
50+
role = aws_iam_role.env_destroy[0].name
51+
policy_arn = aws_iam_policy.env_destroy[0].arn
52+
}

infrastructure/terraform/components/acct/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ variable "cost_anomaly_threshold" {
138138
description = "The threshold percentage for cost anomaly detection"
139139
default = 10
140140
}
141+
142+
variable "enable_env_destroy_event_rule" {
143+
type = bool
144+
description = "Toggles the creation of the CloudWatch Event Rule for environment destruction failures"
145+
default = false
146+
}

0 commit comments

Comments
 (0)