From d1366ae106834f0a56cf9f1a1ebe3809d4326b15 Mon Sep 17 00:00:00 2001 From: Alex Burgel Date: Wed, 27 Nov 2024 16:02:53 -0500 Subject: [PATCH 1/2] fix: Use for_each to make log group subs order independent --- modules/cloudwatch-logs/main.tf | 7 ++++--- modules/cloudwatch-logs/outputs.tf | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/cloudwatch-logs/main.tf b/modules/cloudwatch-logs/main.tf index f7af6f9..ddabc54 100644 --- a/modules/cloudwatch-logs/main.tf +++ b/modules/cloudwatch-logs/main.tf @@ -23,10 +23,11 @@ module "kfh" { } resource "aws_cloudwatch_log_subscription_filter" "this" { - count = length(var.cloudwatch_log_groups) - name = "${var.cloudwatch_log_groups[count.index]}-logs_subscription_filter" + for_each = toset(var.cloudwatch_log_groups) + + name = "${each.key}-logs_subscription_filter" role_arn = aws_iam_role.this.arn - log_group_name = var.cloudwatch_log_groups[count.index] + log_group_name = each.key filter_pattern = var.log_subscription_filter_pattern destination_arn = module.kfh.kinesis_firehose_delivery_stream_arn } diff --git a/modules/cloudwatch-logs/outputs.tf b/modules/cloudwatch-logs/outputs.tf index 032fa82..40d7cbd 100644 --- a/modules/cloudwatch-logs/outputs.tf +++ b/modules/cloudwatch-logs/outputs.tf @@ -1,3 +1,3 @@ output "cloudwatch_log_subscription_filters" { - value = aws_cloudwatch_log_subscription_filter.this[*].name + value = [for filter in aws_cloudwatch_log_subscription_filter.this : filter.name] } From c389e33ce890e55e7e00d21c4467d0c3b44c1a25 Mon Sep 17 00:00:00 2001 From: Alex Burgel Date: Sun, 15 Dec 2024 17:12:42 -0500 Subject: [PATCH 2/2] Add flag to enable new behavior --- modules/cloudwatch-logs/main.tf | 12 +++++++++++- modules/cloudwatch-logs/outputs.tf | 2 +- modules/cloudwatch-logs/variables.tf | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/cloudwatch-logs/main.tf b/modules/cloudwatch-logs/main.tf index ddabc54..c203c59 100644 --- a/modules/cloudwatch-logs/main.tf +++ b/modules/cloudwatch-logs/main.tf @@ -23,7 +23,17 @@ module "kfh" { } resource "aws_cloudwatch_log_subscription_filter" "this" { - for_each = toset(var.cloudwatch_log_groups) + count = !var.use_order_independent_filter_resource_naming ? length(var.cloudwatch_log_groups) : 0 + + name = "${var.cloudwatch_log_groups[count.index]}-logs_subscription_filter" + role_arn = aws_iam_role.this.arn + log_group_name = var.cloudwatch_log_groups[count.index] + filter_pattern = var.log_subscription_filter_pattern + destination_arn = module.kfh.kinesis_firehose_delivery_stream_arn +} + +resource "aws_cloudwatch_log_subscription_filter" "filters" { + for_each = var.use_order_independent_filter_resource_naming ? toset(var.cloudwatch_log_groups) : [] name = "${each.key}-logs_subscription_filter" role_arn = aws_iam_role.this.arn diff --git a/modules/cloudwatch-logs/outputs.tf b/modules/cloudwatch-logs/outputs.tf index 40d7cbd..3e437d3 100644 --- a/modules/cloudwatch-logs/outputs.tf +++ b/modules/cloudwatch-logs/outputs.tf @@ -1,3 +1,3 @@ output "cloudwatch_log_subscription_filters" { - value = [for filter in aws_cloudwatch_log_subscription_filter.this : filter.name] + value = var.use_order_independent_filter_resource_naming ? [for filter in aws_cloudwatch_log_subscription_filter.filters : filter.name] : aws_cloudwatch_log_subscription_filter.this[*].name } diff --git a/modules/cloudwatch-logs/variables.tf b/modules/cloudwatch-logs/variables.tf index 97be0bf..0249cce 100644 --- a/modules/cloudwatch-logs/variables.tf +++ b/modules/cloudwatch-logs/variables.tf @@ -136,3 +136,9 @@ variable "tags" { default = {} description = "A map of tags to apply to resources created by this module." } + +variable "use_order_independent_filter_resource_naming" { + type = bool + description = "Use order-independent naming for log group subscription filter resources." + default = false +}