0

我现在面临的问题是这样的。我正在努力使我的政策更加灵活。所以我将它们转移到一个文件中,而不是使用 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"
      }
    }
  ]
}
4

1 回答 1

1

我会尝试以下两个步骤:

  1. 删除 "${max_tagged_images}" 周围的双引号

  2. 使用名为 tonumber 的 terraform 函数将其转换为数字:

    tonumber("1")

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

于 2020-11-09T13:39:10.950 回答