terraform - 如何在不破坏现有客户端的情况下扩展 terraform 模块输入变量模式?

标签 terraform

我有一个带有以下输入变量的模块:

variable "apsvc_map" {
  description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
  type        = map(object({ 
    identity_ids = list(string),
    disabled = bool
  }))
}

现在我想在模式中添加一个新属性 - no_custom_hostname_binding .新版本将是:
variable "apsvc_map" {
  description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
  type        = map(object({ 
    identity_ids = list(string),
    disabled = bool
    no_custom_hostname_binding = bool
  }))
}

try 的帮助下,此更改可以在模块代码中向后兼容。函数,因为省略新属性等同于为其提供 false值(value)。

但是,terraform 会严格处理此模式,并且不允许在没有新字段的情况下传递输入:
2020-05-30T15:34:20.8061749Z Error: Invalid value for module argument
2020-05-30T15:34:20.8062005Z 
2020-05-30T15:34:20.8062205Z   on ..\..\modules\web\main.tf line 47, in module "web":
2020-05-30T15:34:20.8062336Z   47:   apsvc_map = {
2020-05-30T15:34:20.8062484Z   48:     dfhub = {
2020-05-30T15:34:20.8062727Z   49:       disabled     = false
2020-05-30T15:34:20.8065156Z   50:       identity_ids = [local.identity_id]
2020-05-30T15:34:20.8065370Z   51:     }
2020-05-30T15:34:20.8065459Z   52:   }
2020-05-30T15:34:20.8065538Z 

我从 terraform 提示的错误中了解到,因为我没有在输入中指定新属性的值。

所以,有三种解决方案:
  • 更新所有现有代码以添加新属性 - 不可能。
  • 给新版本的模块打上不同的标签,让新代码引用新标签,而旧代码继续引用旧标签——从长远来看会导致标签泛滥,在标签名称。最终 - 不可能。
  • 通过注释掉可选属性来放宽输入变量模式并使用 try在代码中。

  • 最后一个选项并不理想,因为模块的文档不会列出可选属性。但从代码管理的角度来看——它是最好的。

    所以问题是 - 可以将输入对象属性定义为可选的吗?理想情况下,它应该包含默认值,但我可以使用 try现在的方法。

    编辑 1

    我实际上认为我可以在对象中传递未知属性,但没有。一旦给出了模式,它就没什么了。因此,唯一向后兼容的解决方案是使用 map(any)就我而言。

    最佳答案

    已为 Terraform 建议了对象变量中的可选参数:
    https://github.com/hashicorp/terraform/issues/19898

    不幸的是,截至 2020 年 5 月 30 日,这方面没有任何进展。
    这是他们 repo 中投票最多的问题,我们所能做的就是继续投票,希望很快就会实现。

    你是对的,替代方案是不可能的,或者是简单的

    关于terraform - 如何在不破坏现有客户端的情况下扩展 terraform 模块输入变量模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105031/

    相关文章:

    Azure Terraform Virtual Vetwork 对等互连错误(对两个现有 vnet 进行对等互连)

    azure - 在哪里可以找到 Terraform 环境变量的 Azure 服务主体凭据?

    amazon-web-services - 如何在Terraform中输出每个AZ的节点IP

    asp.net-core - Nginx Ingress之后的IdentityServer4,已与Terraform和Helm一起部署:并非所有用户都可以登录

    terraform - 检查 "s3"后端中的状态时出错 : NoSuchBucket: The specified bucket does not exist

    amazon-ec2 - 我可以在 terraform 创建的 ec2 实例中执行 ssh 吗?

    azure - azure terraform 中的 site_config 配置

    azure - Terraform Azure_Linux_VM 使 SSH key 不起作用

    azure - 如何使用多个keyvault并使用循环?地形

    aws-api-gateway - 如何通过 Terraform 设置 aws cloudwatch 保留