терраформируйте карту, используя цикл for с двумя различными ресурсами внутри

#amazon-web-services #terraform #terraform-provider-aws

Вопрос:

У меня есть переменная с нижеприведенным входом

 variable functions = ["one", "two"]
 

Я пытаюсь добиться чего-то подобного в местных жителях (какова моя попытка, приведенная ниже, или что-то, что, я надеюсь, сработает)

 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"
      }
  }
}
 

Вывод, который я пытаюсь создать, карта из 4 элементов, как показано ниже, чтобы я мог использовать for_each для создания сигналов тревоги в разделе ресурсов

 {
"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
  .
  .
}
 

Идеи?

Ответ №1:

Вы можете сделать это с flatten помощью и ... (расширение аргумента):

 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"
      }}]
  ])...)
}
 

Комментарии:

1. @Дэнни, без проблем. Дай мне знать, как все пройдет.

2. Ты король выражений!!. Я могу генерировать выходные данные плана без ошибок. Это работает 🙂