首页 > 解决方案 > Does terraform support map of list?

问题描述

I want to create a variable that will resemble the following yaml format

Dev:
 - name: Michael
   type: Senior
 - name: Ron
   type: Junior
 - name: Lex
   type: Senior
 - name: Rocky
   type: Junior
Test:
 - name: Rony
   type: Junior
 - name: Ryan
   type: Junior
 - name: John
   type: Senior
 - name: Brad
   type: Junior

What is the best way in Terraform to represent the above data structure?.

标签: amazon-web-servicesterraformdevops

解决方案


如果您要求每个列表元素都具有这些键nametype,但顶级键是任意的,那么接受该数据结构的最直接类型约束将是:

  type = map(list(object({
    name = string
    type = string
  }))

我在这里假设列表项的顺序很重要。set(...)相反,如果这只是一个没有特定顺序的不同对象的集合(例如,如果您打算使用每个对象来声明一个完全独立的对象),那么使用类型种类而不是类型种类可能更有意义list(...),更清楚地表明排序并不重要,并且每个项目都必须是唯一的。


推荐阅读