如何设置计算的局部变量

How do I set a computed local variable

我有一个 tf.json 文件,它声明了一堆局部变量。其中一个变量是一组复杂对象,如下所示:

{
  "locals": [
    {
      "ordered_cache_behaviors": [
        {
          "path_pattern": "/poc-app-angular*",
          "s3_target": "dev-ui-static",
          "ingress": "external"
        }
      ]
    }
  ]
}

这就是我想要做的...而不是在我的文件中静态声明变量 ordered_cache_behaviors 我希望它是一个计算值。我将从 S3 存储桶中获取此配置并在此处设置值。因此静态值将只是一个空数组 [],我将在从 S3.

获取数据后附加一个脚本

此逻辑需要在每次 terraform planterraform apply 之前执行。做这个的最好方式是什么?我假设我需要使用 Provisioner 来触发脚本?如果是这样,我该如何在此处设置局部变量?

如果缓存配置数据可以 JSON 格式化,您可以使用 s3_bucket_object datasource plus the jsondecode 函数作为替代方法:

将您的缓存配置数据作为 cache-config.json 上传到 poc-app-cache-config 存储桶,然后使用以下命令让 Terraform 从 S3 下载该文件并将其解析到您的本地 ordered_cache_behaviors 变量中:

data "aws_s3_bucket_object" "cache_configuration" {
  bucket = "poc-app-cache-config"
  key    = "cache-config.json" # JSON-formatted cache configuration map
}

...

locals {
  ordered_cache_behaviors = jsondecode(aws_s3_bucket_object.cache_configuration.body)
}