0

我正在尝试将一个变量设置为一个字符串,该字符串稍后将与另一个字符串连接以用于 aws s3 存储桶策略。我试图通过定义一个局部变量来做到这一点,但我还需要指定一个我想要使用它的条件。我正在使用 terraform 11。

例如:

  • 如果 set_bucket_policy 为 false 则将变量设为空字符串 ""
  • 否则使用heredoc设置变量的字符串值

例如,不工作的代码:

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
}

4

1 回答 1

1

我认为这非常接近,我创建了一个小样本来展示如何使用条件。有关更多详细信息,您可以查看 Terraform 的条件表达式

主文件

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
于 2020-08-25T20:17:30.070 回答