我正在尝试将一个变量设置为一个字符串,稍后再将其与aws s3存储桶策略的另一个字符串连接。我正在尝试通过定义局部变量来做到这一点,但是我还需要指定一个要在其中使用的条件。我正在使用terraform 11。
例如:
示例,而不是有效代码:
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
}
答案 0 :(得分:1)
我认为这已经很接近了,我创建了一个小样本来展示如何使用条件句。有关更多详细信息,您可以查看Terraform的Conditional Expressions。
main.tf
variable "set_bucket_policy" {
type = bool
}
output "my_bucket_policy" {
value = var.set_bucket_policy == false ? "is set to false" : "is set to true"
}
示例输出
% terraform apply -var 'set_bucket_policy=false' -auto-approve
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
my_bucket_policy = is set to false
% terraform apply -var 'set_bucket_policy=true' -auto-approve
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
my_bucket_policy = is set to true