如何使用terraform将默认值设置为aws_iam_policy



我想动态设置aws策略。设置变量以调用aws_iam_policy资源。变量文件为

变量.tf

variable "name" {
default = ""
type = string
}
variable "policy" {
default = ""
type = string
}

在主.tf 中

resource "aws_iam_policy" "my_policy" {
name   = var.name
policy = var.policy
}

运行tflint

name var得到:

Error: name must be 1 characters or higher (aws_iam_policy_invalid_name)
Error: "" does not match valid pattern ^[w+=,.@-]+$ (aws_iam_policy_invalid_name)

策略变量得到:

Error: policy must be 1 characters or higher (aws_iam_policy_invalid_policy)
Error: "" does not match valid pattern ^[x{0009}x{000A}x{000D}x{0020}-x{00FF}]+$ (aws_iam_policy_invalid_policy)

难道不能设置为"字符串,如果默认情况下不设置策略?

托管策略名称不能为空字符串。[w+=,.@-]+模式之后至少需要1个字符。

但是要自动生成名称,如果您有空字符串,您可以尝试以下操作:

resource "aws_iam_policy" "my_policy" {
count  = var.policy != "" ? 1 : 0 
name   = var.name != "" ? var.name : null
policy = var.policy
}

相关内容

  • 没有找到相关文章

最新更新