The aws_iam_policy_document
supports multiple condition
directives.
The following Terraform configuration should help:
data "aws_iam_policy_document" "test" {
statement {
effect = "Deny"
actions = ["backup:*"]
resources = ["*"]
condition {
test = "StringEquals"
values = ["bucket-owner-full-control"]
variable = "s3:x-amz-acl"
}
condition {
test = "StringEquals"
values = ["xxxxxxxxxxxx"]
variable = "aws:SourceAccount"
}
condition {
test = "ArnLike"
values = ["arn:aws:s3:::my-tf-test-bucket"]
variable = "aws:SourceArn"
}
}
}
output "policy" {
value = data.aws_iam_policy_document.test.json
}
If we do a terraform plan on that we will get:
terraform plan
data.aws_iam_policy_document.test: Reading...
data.aws_iam_policy_document.test: Read complete after 0s [id=3933526891]
Changes to Outputs:
+ policy = jsonencode(
{
+ Statement = [
+ {
+ Action = "backup:*"
+ Condition = {
+ ArnLike = {
+ "aws:SourceArn" = "arn:aws:s3:::my-tf-test-bucket"
}
+ StringEquals = {
+ "aws:SourceAccount" = "xxxxxxxxxxxx"
+ "s3:x-amz-acl" = "bucket-owner-full-control"
}
}
+ Effect = "Deny"
+ Resource = "*"
},
]
+ Version = "2012-10-17"
}
)