Technical questionAccepted answer
Terraform: Conditional statement blocks in aws_iam_policy_document?
jbreed
May 26
0 views1
Is there a way to conditionally add statement blocks in aws_iam_policy_document? I'm looking for something like:
data "aws_iam_policy_document" "policy" {
statement {
sid = "PolicyAlways"
...
}
if (var.enable_optional_policy) {
statement {
sid = "PolicySometimes"
...
}
}
}
1 answer
Accepted answer · original discussion
Ben Whaley
PermalinkMay 26
Yes. You can use a dynamic block with a boolean to optionally include the block.
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"
...
}
}
}
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.