Skip to content

Commit

Permalink
Switch to for_each for log forwarding resources
Browse files Browse the repository at this point in the history
In case `var.cloudwatch_log_groups` changes somewhere in the middle of the list, all resources indexed since that position must be recreated. This can be remedied by switching to `for_each` instead of `count`
  • Loading branch information
zbstof authored Feb 14, 2024
1 parent 3ca76a2 commit 8f95d4b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions logs_monitoring_cloudwatch_log.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
resource "aws_cloudwatch_log_subscription_filter" "test_lambdafunction_logfilter" {
count = length(var.cloudwatch_log_groups)
name = "${var.cloudwatch_log_groups[count.index]}-filter"
log_group_name = var.cloudwatch_log_groups[count.index]
for_each = { for lg in var.cloudwatch_log_groups : lg => lg }
name = "${each.value}-filter"
log_group_name = each.value
filter_pattern = ""
destination_arn = aws_cloudformation_stack.datadog-forwarder.outputs.DatadogForwarderArn
distribution = "Random"
}

resource "aws_lambda_permission" "allow_cloudwatch_logs_to_call_dd_lambda_handler" {
count = length(var.cloudwatch_log_groups)
statement_id = "${substr(replace(var.cloudwatch_log_groups[count.index], "/", "_"), 0, 67)}-AllowExecutionFromCloudWatchLogs"
for_each = { for lg in var.cloudwatch_log_groups : lg => lg }
statement_id = "${substr(replace(each.value, "/", "_"), 0, 67)}-AllowExecutionFromCloudWatchLogs"
action = "lambda:InvokeFunction"
function_name = aws_cloudformation_stack.datadog-forwarder.outputs.DatadogForwarderArn
principal = "logs.${var.aws_region}.amazonaws.com"
source_arn = "arn:aws:logs:${var.aws_region}:${var.aws_account_id}:log-group:${var.cloudwatch_log_groups[count.index]}:*"
source_arn = "arn:aws:logs:${var.aws_region}:${var.aws_account_id}:log-group:${each.value}:*"
}

0 comments on commit 8f95d4b

Please sign in to comment.