使用环境变量作为本地块中常见标签的映射

iej*_*j94 5 tagging environment-variables terraform

我在尝试在 terraform 中自动化标记过程时遇到了一个特定问题。我设置了一个环境变量,它本质上是我们将用于应用程序中配置的所有资源的所有标签的列表。看起来像这样...

export TF_VAR_taglist='{JiraEpic = "ETOS-56","AssignedResearcherPri" = "Isaac",AssignedResearcherSec = "Matt"}'

设置环境变量后,我在变量.tf 文件中添加了一个名为“taglist”的变量,用于获取上述环境变量。看起来像这样...

variable "taglist"{}

最后,我有另一个 locals.tf 文件,我在其中设置了 common_tags 变量。就像这样...

locals { common_tags ="${var.taglist}" }

当我应用 terraform 时,构建在尝试正确映射标签时失败。这是我收到的错误...

Error: Incorrect attribute value type

  on kube_master_worker_nodes_ec2.tf line 9, in resource "aws_instance" "master":
   9:   tags = local.common_tags
    |----------------
    | local.common_tags is "{JiraEpic = \"ETOS-56\",AssignedResearcherPri = \"Isaac \",AssignedResearcherSec = \"Matt\"}"

Inappropriate value for attribute "tags": map of string required.

Run Code Online (Sandbox Code Playgroud)

map(string然后我决定像这样在 Variables.tf 文件中 定义变量的类型,variable "taglist"{ type = map(string) } 我希望这能让 terraform 将该变量识别为字符串映射,而不仅仅是字符串文字,但我错了,这些是应用该定义时出现的错误。

Error: Missing attribute separator

  on <value for var.taglist> line 1:
  (source code not available)

Expected a newline or comma to mark the beginning of the next attribute.


Error: No value for required variable

  on variables.tf line 11:
  11: variable "taglist"{

The root module input variable "taglist" is not set, and has no default value.
Use a -var or -var-file command line argument to provide a value for this
variable.
Run Code Online (Sandbox Code Playgroud)

我真的很困惑,我觉得我已经很接近了。谁能对此提供一些见解以及我应该如何解决它?

iej*_*j94 4

我首先要感谢 Martin Atkins 给了我在环境变量中使用冒号而不是等号的想法,因为这是唯一的问题。该变量未正确表示为 JSON 对象,因此 terraform 将其解释为字符串。

我变了

export TF_VAR_taglist='{JiraEpic = "ETOS-56","AssignedResearcherPri" = "Isaac",AssignedResearcherSec = "Matt"}'
Run Code Online (Sandbox Code Playgroud)

对此

export TF_VAR_taglist='{"JiraEpic":"ETOS-56","AssignedResearcherPri":"Isaac", "AssignedResearcherSec":"Matt"}'
Run Code Online (Sandbox Code Playgroud)

现在,它就像吸了猫薄荷的小猫一样发出呼噜声。