首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使数据模板文件识别terraform中的数字

如何使数据模板文件识别terraform中的数字
EN

Stack Overflow用户
提问于 2020-11-09 10:00:31
回答 1查看 684关注 0票数 0

THe我现在面临的问题就是这个。我正在设法使我的政策更加灵活。所以我把它们转换成一个文件而不是用EOF。

如何使模板文件识别数字值?

"${max_untagged_images}""${max_tagged_images}"应该是数字。

Aws生命周期策略:

代码语言:javascript
复制
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

}

数据模板:

代码语言:javascript
复制
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
  }
}

政策:

代码语言:javascript
复制
{
  "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"
      }
    }
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 13:39:10

我将尝试以下两个步骤:

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

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

(“1”)

(请参阅正式文档:https://www.terraform.io/docs/configuration/functions/tonumber.html)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64749437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档