Terraform: блоки условных операторов в aws_iam_policy_document? - PullRequest
7 голосов
/ 26 мая 2020

Есть ли способ условно добавить statement блоков в aws_iam_policy_document? Я ищу что-то вроде:

data "aws_iam_policy_document" "policy" {
  statement {
    sid = "PolicyAlways"

    ...
  }

  if (var.enable_optional_policy) {
    statement {
      sid = "PolicySometimes"

      ...
    }
  }
}

1 Ответ

5 голосов
/ 27 мая 2020

Да. Вы можете (ab) использовать блок dynamic с логическим значением для необязательного включения блока.

data "aws_iam_policy_document" "policy" {
  statement {
    sid = "PolicyAlways"

    ...
  }

  dynamic "statement" {
    # The contents of the list below are arbitrary, but must be of length one. 
    # It is only used to determine whether or not to include this statement.
    for_each = var.enable_optional_policy ? [1] : []

    content {
      sid = "PolicySometimes"
      ...
    }
  }
}
...