I have variable with below input
variable functions = ["one", "two"]
Im trying to achieve something like this in locals (what my attempt is, included below, or something I hope it would work)
locals{
alarms_with_math_expression = {
for function in var.functions:
"aws_lambda_fn_error-${function}" => {
alarm_description = "Error rate has exceeded 60%"
evaluation_periods = "2"
threshold = "60"
metric_query_id = "Lambda"
metric_to_compare_name = "Errors"
metric_to_compare_namespace = "AWS/Lambda"
},
"aws_lambda_log_process_error-${function}" => {
alarm_description = "Processing error rate has exceeded 40%"
evaluation_periods = "1"
threshold = "40"
metric_query_id = "LogProcessing"
metric_to_compare_name = "LogProcessErrorCount"
metric_to_compare_namespace = "TestLambda"
}
}
}
Output im trying to generate, a 4 member map, as below, so that I can use for_each with creating alarms in resource section
{
"aws_lambda_fn_error-one" = {
alarm_description = "Error rate has exceeded 60%"
evaluation_periods = "2"
threshold = "60"
metric_query_id = "Lambda"
metric_to_compare_name = "Errors"
metric_to_compare_namespace = "AWS/Lambda"
},
"aws_lambda_log_process_error-one" => {
alarm_description = "Processing error rate has exceeded 40%"
evaluation_periods = "1"
threshold = "40"
metric_query_id = "LogProcessing"
metric_to_compare_name = "LogProcessErrorCount"
metric_to_compare_namespace = "TestLambda"
},
"aws_lambda_fn_error-two" = {
alarm_description = "Error rate has exceeded 60%"
evaluation_periods = "2"
threshold = "60"
metric_query_id = "Lambda"
metric_to_compare_name = "Errors"
metric_to_compare_namespace = "AWS/Lambda"
},
"aws_lambda_log_process_error-two" => {
alarm_description = "Processing error rate has exceeded 40%"
evaluation_periods = "1"
threshold = "40"
metric_query_id = "LogProcessing"
metric_to_compare_name = "LogProcessErrorCount"
metric_to_compare_namespace = "TestLambda"
},
}
resource "aws_cloudwatch_metric_alarm" "generic_alarms" {
for_each = var.environment == "prod" ? local.alarms_with_math_expression : {}
alarm_name = each.key
alarm_description = each.value.alarm_description
.
.
}
Ideas?
CodePudding user response:
You can do this with flatten
and ...
(argument expansion):
locals{
alarms_with_math_expression = merge(flatten([
for function in var.functions:
[ {"aws_lambda_fn_error-${function}" = {
alarm_description = "Error rate has exceeded 60%"
evaluation_periods = "2"
threshold = "60"
metric_query_id = "Lambda"
metric_to_compare_name = "Errors"
metric_to_compare_namespace = "AWS/Lambda"
}},
{"aws_lambda_log_process_error-${function}" = {
alarm_description = "Processing error rate has exceeded 40%"
evaluation_periods = "1"
threshold = "40"
metric_query_id = "LogProcessing"
metric_to_compare_name = "LogProcessErrorCount"
metric_to_compare_namespace = "TestLambda"
}}]
])...)
}