如何让我的数据模板文件识别 terraform 中的数字

How do I make my data template file recognise a number in terraform

我现在面临的问题是这样的。我正在努力使我的政策更加灵活。所以我将它们转移到一个文件中而不是使用 EOF。

如何让模板文件识别数值?

"${max_untagged_images}""${max_tagged_images}" 假设是数字。

AWS 生命周期政策:

resource "aws_ecr_lifecycle_policy" "lifecycle" {
  count      = length(aws_ecr_repository.repo)
  repository = aws_ecr_repository.repo[count.index].name

  depends_on = [aws_ecr_repository.repo]

  policy = var.policy_type == "app" ? data.template_file.lifecycle_policy_app.rendered : data.template_file.lifecycle_policy_infra.rendered

}

数据模板:

data "template_file" "lifecycle_policy_app" {

  template = file("lifecyclePolicyApp.json")

  vars = {
    max_untagged_images = var.max_untagged_images
    max_tagged_images = var.max_tagged_images
    env = var.env
  }
}

政策:

{
  "rules": [
    {
      "rulePriority": 1,
      "description": "Expire untagged images older than ${max_untagged_images} days",
      "selection": {
        "tagStatus": "untagged",
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": "${max_untagged_images}"
      },
      "action": {
        "type": "expire"
      }
    },
    {
      "rulePriority": 2,
      "description": "Expire tagged images of ${env}, older than ${max_tagged_images} days",
      "selection": {
        "tagStatus": "tagged",
        "countType": "imageCountMoreThan",
        "countNumber": "${max_tagged_images}",
        "tagPrefixList": [
          "${env}"
        ]
      },
      "action": {
        "type": "expire"
      }
    }
  ]
}

我会尝试以下两个步骤:

  1. 去掉"${max_tagged_images}"

    两边的双引号
  2. 使用名为 tonumber 的 terraform 函数将其转换为数字:

    tonumber("1")

(按照官方文档:https://www.terraform.io/docs/configuration/functions/tonumber.html