【问题标题】:terraform provisioner variable type listofmaps doesn't work for tagsterraform provisioner 变量类型 listofmaps 不适用于标签
【发布时间】:2021-01-31 03:07:25
【问题描述】:

在 Terraform 中,我使用 null_resource 提供程序来创建 aws eventbridge(自定义事件总线)。由于 terraform 不提供内置资源​​类型来创建自定义事件总线。

# cat main.tf 
resource "null_resource" "event_bus" {
    triggers = {
          event_bus_name = var.event_bus_name
    }
    provisioner "local-exec" {
          command = "aws events create-event-bus --name ${var.event_bus_name} --tags ${var.event_bus_tags}"
   
    }
    provisioner "local-exec" {
          when = destroy
 
          command = "aws events delete-event-bus --name ${self.triggers.event_bus_name}"
    }
}

我正在为标签定义变量,如下所示

# cat variable.tf 
variable "event_bus_tags" {
   type = list(map(any))
}
variable "event_bus_name" {
   type = string
}

我在 auto.tfvars 中调用变量如下

# cat var.auto.tfvars 
event_bus_tags = [
  {
    "Key": "environment", "Value": "dev"
  },
  {
    "Key": "type", "Value": "custom"
  }
]

event_bus_name = "my-event-bus"

但我收到以下错误。

# terraform apply --auto-approve
null_resource.event_bus: Creating...
Error: Invalid template interpolation value: Cannot include the given value in a string template: string required.

命令行运行良好。

aws events create-event-bus --name test --tags '[ { "Key": "env", "Value": "dev" } ]'

不确定标签的合适变量类型是什么,如果“键”和“值”是字符串类型。

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    您需要将 Terraform 对象转换为 JSON 字符串。 Terraform 为此提供了jsonencode 函数。

    command = "aws events create-event-bus --name ${var.event_bus_name} --tags ${jsonencode(var.event_bus_tags)}"
    

    【讨论】:

    • awesome 非常感谢@Mark,它按预期工作是 main.tf 中的更新命令command = "aws events create-event-bus --name ${var.event_bus_name} --tags '${jsonencode(var.event_bus_tags)}'"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多