Skip to content

Commit 6d1c219

Browse files
authored
fix/SA-251 - conditional teams and slack resources (#40)
* fix: conditional creation of slack and team resources
1 parent 6e84469 commit 6d1c219

File tree

8 files changed

+45
-48
lines changed

8 files changed

+45
-48
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,7 @@ Frequently (quartley at least) check and upgrade:
140140

141141
| Name | Description |
142142
|------|-------------|
143+
| <a name="output_channels_config"></a> [channels\_config](#output\_channels\_config) | The configuration data for each distribution channel |
144+
| <a name="output_distributions"></a> [distributions](#output\_distributions) | The list of slack/teams distributions that are managed |
143145
| <a name="output_sns_topic_arn"></a> [sns\_topic\_arn](#output\_sns\_topic\_arn) | The ARN of the SNS topic |
144146
<!-- END_TF_DOCS -->

locals.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ locals {
2929
teams_webhook_url = local.enable_teams_secret ? try(jsondecode(data.aws_secretsmanager_secret_version.teams[0].secret_string)["webhook_url"], var.teams.webhook_url) : try(var.teams.webhook_url, null)
3030

3131
channels_config = {
32-
"slack" = {
32+
"slack" = var.slack != null ? {
3333
webhook_url = local.slack_webhook_url
3434
lambda_name = try(var.slack.lambda_name, "slack-notify")
3535
lambda_description = try(var.slack.lambda_description, "Sends posts to slack")
3636
filter_policy = try(var.slack.filter_policy, null)
3737
filter_policy_scope = try(var.slack.filter_policy_scope, null)
38-
},
39-
"teams" = {
38+
} : null,
39+
"teams" = var.teams != null ? {
4040
webhook_url = local.teams_webhook_url
4141
lambda_name = try(var.teams.lambda_name, "teams-notify")
4242
lambda_description = try(var.teams.lambda_description, "Sends posts to teams")
4343
filter_policy = try(var.teams.filter_policy, null)
4444
filter_policy_scope = try(var.teams.filter_policy_scope, null)
45-
}
45+
} : null,
4646
}
4747
}

main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ resource "aws_sns_topic_subscription" "subscribers" {
4242
# tfsec:ignore:aws-lambda-enable-tracing
4343
# tfsec:ignore:aws-lambda-restrict-source-arn
4444
module "notify" {
45-
count = var.enable_slack || var.enable_teams ? 1 : 0
4645
source = "./modules/notify"
4746

4847
cloudwatch_log_group_kms_key_id = var.cloudwatch_log_group_kms_key_id

modules/notify/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ Subsumed by appvia's GNU V3 license; [see license](../../LICENSE).
164164
| <a name="input_lambda_source_path"></a> [lambda\_source\_path](#input\_lambda\_source\_path) | The source path of the custom Lambda function | `string` | `null` | no |
165165
| <a name="input_post_icons_url"></a> [post\_icons\_url](#input\_post\_icons\_url) | URLs (not base64 encoded!) to publically available icons for highlighting posts of error and/or warning status. Ideally 50px square. | <pre>object({<br> error_url = string<br> warning_url = string<br> })</pre> | <pre>{<br> "error_url": "https://raw.githubusercontent.com/appvia/terraform-aws-notifications/main/resources/posts-attention-icon.png",<br> "warning_url": "https://raw.githubusercontent.com/appvia/terraform-aws-notifications/main/resources/posts-warning-icon.png"<br>}</pre> | no |
166166
| <a name="input_powertools_layer_arn_suffix"></a> [powertools\_layer\_arn\_suffix](#input\_powertools\_layer\_arn\_suffix) | The suffix of the ARN to use for AWS Powertools lambda layer (must match the architecture:https://docs.powertools.aws.dev/lambda/python/latest/. | `string` | `"AWSLambdaPowertoolsPythonV2-Arm64:79"` | no |
167-
| <a name="input_putin_khuylo"></a> [putin\_khuylo](#input\_putin\_khuylo) | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | `bool` | `true` | no |
168167
| <a name="input_python_runtime"></a> [python\_runtime](#input\_python\_runtime) | The lambda python runtime | `string` | `"python3.12"` | no |
169168
| <a name="input_recreate_missing_package"></a> [recreate\_missing\_package](#input\_recreate\_missing\_package) | Whether to recreate missing Lambda package if it is missing locally or not | `bool` | `true` | no |
170169
| <a name="input_reserved_concurrent_executions"></a> [reserved\_concurrent\_executions](#input\_reserved\_concurrent\_executions) | The amount of reserved concurrent executions for this lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations | `number` | `-1` | no |
@@ -186,6 +185,7 @@ Subsumed by appvia's GNU V3 license; [see license](../../LICENSE).
186185

187186
| Name | Description |
188187
|------|-------------|
188+
| <a name="output_distributions"></a> [distributions](#output\_distributions) | The list of slack/teams distributions that are managed |
189189
| <a name="output_notify_slack_lambda_function_arn"></a> [notify\_slack\_lambda\_function\_arn](#output\_notify\_slack\_lambda\_function\_arn) | The ARN of the Lambda function |
190190
| <a name="output_notify_slack_lambda_function_version"></a> [notify\_slack\_lambda\_function\_version](#output\_notify\_slack\_lambda\_function\_version) | Latest published version of your Lambda function |
191191
| <a name="output_notify_slack_slack_lambda_function_name"></a> [notify\_slack\_slack\_lambda\_function\_name](#output\_notify\_slack\_slack\_lambda\_function\_name) | The name of the Lambda function |

modules/notify/main.tf

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ data "aws_partition" "current" {}
33
data "aws_region" "current" {}
44

55
locals {
6-
create = var.create && var.putin_khuylo
6+
create = var.create
77

88
sns_topic_arn = try(
99
aws_sns_topic.this[0].arn,
@@ -76,12 +76,23 @@ locals {
7676
warning-icon-url = var.post_icons_url.warning_url
7777
}
7878
)
79+
80+
# the enable_[slack|teams] variable controls the subscription between SNS and lambda only; it is
81+
# feasible that we want to keep the infrastructure (lambda, lambda role, log group et al) while suspending
82+
# the posts.
83+
# but we only want to create the infrastructure if details of slack or team have been defined
84+
create_distribution = {
85+
"slack" = var.delivery_channels["slack"] != null ? true : false,
86+
"teams" = var.delivery_channels["teams"] != null ? true : false,
87+
}
88+
89+
distributions = toset([for x in ["slack", "teams"] : x if local.create_distribution[x] == true])
7990
}
8091

8192
#trivy:ignore:avd-aws-0059
8293
#trivy:ignore:avd-aws-0057
8394
data "aws_iam_policy_document" "lambda" {
84-
for_each = toset(["slack", "teams"])
95+
for_each = local.distributions
8596

8697
dynamic "statement" {
8798
for_each = concat([local.lambda_policy_document[each.value]], var.kms_key_arn != "" ? [local.lambda_policy_document_kms] : [])
@@ -95,7 +106,7 @@ data "aws_iam_policy_document" "lambda" {
95106
}
96107

97108
resource "aws_cloudwatch_log_group" "lambda" {
98-
for_each = toset(["slack", "teams"])
109+
for_each = local.distributions
99110

100111
name = "/aws/lambda/${var.delivery_channels[each.value].lambda_name}"
101112
retention_in_days = var.cloudwatch_log_group_retention_in_days
@@ -121,7 +132,7 @@ resource "aws_sns_topic" "this" {
121132

122133

123134
resource "aws_sns_topic_subscription" "sns_notify_slack" {
124-
count = var.create && var.enable_slack ? 1 : 0
135+
count = var.create && var.enable_slack && local.create_distribution["slack"] == true ? 1 : 0
125136

126137
topic_arn = local.sns_topic_arn
127138
protocol = "lambda"
@@ -131,7 +142,7 @@ resource "aws_sns_topic_subscription" "sns_notify_slack" {
131142
}
132143

133144
resource "aws_sns_topic_subscription" "sns_notify_teams" {
134-
count = var.create && var.enable_teams ? 1 : 0
145+
count = var.create && var.enable_teams && local.create_distribution["teams"] == true ? 1 : 0
135146

136147
topic_arn = local.sns_topic_arn
137148
protocol = "lambda"
@@ -187,7 +198,7 @@ resource "local_file" "notification_emblems_python" {
187198

188199
#trivy:ignore:avd-aws-0067
189200
module "lambda" {
190-
for_each = toset(["slack", "teams"])
201+
for_each = local.distributions
191202

192203
source = "terraform-aws-modules/lambda/aws"
193204
version = "3.2.0"

modules/notify/outputs.tf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ output "sns_topic_arn" {
33
value = local.sns_topic_arn
44
}
55

6+
output "distributions" {
7+
description = "The list of slack/teams distributions that are managed"
8+
value = local.distributions
9+
}
10+
611
output "notify_slack_lambda_function_arn" {
712
description = "The ARN of the Lambda function"
8-
value = module.lambda["slack"].lambda_function_arn
13+
value = try(module.lambda["slack"].lambda_function_arn, "")
914
}
1015
output "notify_teams_lambda_function_arn" {
1116
description = "The ARN of the Lambda function"
12-
value = module.lambda["teams"].lambda_function_arn
17+
value = try(module.lambda["teams"].lambda_function_arn, "")
1318
}
1419

1520
output "notify_slack_slack_lambda_function_name" {
1621
description = "The name of the Lambda function"
17-
value = module.lambda["slack"].lambda_function_name
22+
value = try(module.lambda["slack"].lambda_function_name, "")
1823
}
1924
output "notify_teams_slack_lambda_function_name" {
2025
description = "The name of the Lambda function"
21-
value = module.lambda["teams"].lambda_function_name
26+
value = try(module.lambda["teams"].lambda_function_name, "")
2227
}
2328

2429
output "notify_slack_lambda_function_version" {
2530
description = "Latest published version of your Lambda function"
26-
value = module.lambda["slack"].lambda_function_version
31+
value = try(module.lambda["slack"].lambda_function_version, "")
2732
}
2833
output "notify_teams_lambda_function_version" {
2934
description = "Latest published version of your Lambda function"
30-
value = module.lambda["teams"].lambda_function_version
35+
value = try(module.lambda["teams"].lambda_function_version, "")
3136
}
3237

3338
output "slack_lambda_cloudwatch_log_group_arn" {

modules/notify/variables.tf

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
variable "putin_khuylo" {
2-
description = "Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo!"
3-
type = bool
4-
default = true
5-
}
6-
71
variable "architecture" {
82
description = "Instruction set architecture for your Lambda function. Valid values are \"x86_64\" or \"arm64\"."
93
type = string
@@ -34,12 +28,6 @@ variable "create_sns_topic" {
3428
default = true
3529
}
3630

37-
variable "hash_extra" {
38-
description = "The string to add into hashing function. Useful when building same source path for different functions."
39-
type = string
40-
default = ""
41-
}
42-
4331
variable "lambda_role" {
4432
description = "IAM role attached to the Lambda Function. If this is set then a role will not be created for you."
4533
type = string
@@ -129,12 +117,6 @@ variable "sns_topic_lambda_feedback_sample_rate" {
129117
default = 100
130118
}
131119

132-
variable "slack_emoji" {
133-
description = "A custom emoji that will appear on Slack messages"
134-
type = string
135-
default = ":aws:"
136-
}
137-
138120
variable "kms_key_arn" {
139121
description = "ARN of the KMS key used for decrypting slack webhook url"
140122
type = string
@@ -286,17 +268,6 @@ variable "aws_powertools_service_name" {
286268
default = "appvia-notifications"
287269
}
288270

289-
variable "aws_powertools_log_level" {
290-
description = "The log level for aws powertools"
291-
type = string
292-
default = "DEBUG"
293-
294-
validation {
295-
condition = contains(["TRACE", "DEBUG", "INFO", "WARNING", "ERROR"], var.aws_powertools_log_level)
296-
error_message = "Valid values are TRACE, DEBUG, INFO, WARNING, ERROR"
297-
}
298-
}
299-
300271
variable "accounts_id_to_name" {
301272
description = "A mapping of account id and account name - used by notification lamdba to map an account ID to a human readable name"
302273
type = map(string)

outputs.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
21
output "sns_topic_arn" {
32
description = "The ARN of the SNS topic"
43
value = local.sns_topic_arn
54
}
5+
6+
output "distributions" {
7+
description = "The list of slack/teams distributions that are managed"
8+
value = try(module.notify.distributions, "")
9+
}
10+
11+
output "channels_config" {
12+
description = "The configuration data for each distribution channel"
13+
value = local.channels_config
14+
}

0 commit comments

Comments
 (0)