Terraform:aws_iam_policy_document 中的条件语句块?

jbr*_*eed 9 amazon-iam terraform

有没有办法有条件地添加statementaws_iam_policy_document?我正在寻找类似的东西:

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

    ...
  }

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

      ...
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ley 11

是的。可以(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"
      ...
    }
  }
}
Run Code Online (Sandbox Code Playgroud)