I'm trying to set a variable to a string that will later be joined with another string for an aws s3 bucket policy. I'm trying to do this by defining a local variable, but I also need to specify a condition in which I would want to use this. I am using terraform 11.
for instance:
- if set_bucket_policy is false then make the variable an empty string ""
- otherwise use a heredoc to set the string value of the variable
example, not working code:
locals {
my_bucket_policy = var.set_bucket_policy == "false" ? "" : <<EOF
{
"Action": "s3:Get*",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/myrole"
},
"Resource": [
"arn:aws:s3:::mybucket",
"arn:aws:s3:::mybucket/*"
],
"Sid": ""
}
EOF
}
I think this is pretty close, I created a small sample showing how to use conditionals. For more details, you can check out Terraform's Conditional Expressions.
main.tf
Sample Output