HashiCorp Terraform 0.12 新特性搶鮮看: 一流的表達(dá)方式

JUL 05 2018 MARTIN ATKINS

This is the second post of the series highlighting new features in Terraform 0.12.

As part of the lead up to the release of Terraform 0.12, we are publishing a series of feature preview blog posts. The post this week is on first-class expressions.

Terraform uses expressions for dynamic configuration and dependencies. First-class expressions enable variables and operations to be performed outside of strings such as var.foo. In Terraform 0.11 and earlier all expressions were required to be part of interpolations in existing strings, such as "${var.foo}". This simple change has far-ranging benefits, covered in the blog examples.

First-Class Expressions

The example below shows the Terraform 0.11 and earlier syntax:

# Configuration for Terraform 0.11 and earlier

variable "ami"           {}
variable "instance_type" {}
variable "vpc_security_group_ids" {
  type = "list"
}

resource "aws_instance" "example" {
  ami           = "${var.ami}"
  instance_type = "${var.instance_type}"

  vpc_security_group_ids = "${var.vpc_security_group_ids}"
}

The same example converted to Terraform 0.12:

# Configuration for Terraform 0.12

variable "ami"           {}
variable "instance_type" {}
variable "vpc_security_group_ids" {
  type = "list"
}

resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type

  vpc_security_group_ids = var.vpc_security_group_ids
}

Expressions with Lists and Maps

The improvements to expressions in Terraform 0.12 enable using lists and maps more directly with expressions. In versions of Terraform prior to 0.12, the HCL parser supports list and map syntax via [...] and {...} sequences, but it is not possible to use this syntax in conjunction with expressions. This is an artifact of the old HCL implementation having distinct phases for structure and interpolation and was a frequent source of confusion. To work around this limitation, Terraform provides the list and map functions for building lists inside interpolation expressions. The example below shows how this worked in Terraform 0.11 and earlier:

# Configuration for Terraform 0.11 and earlier

resource "aws_instance" "example" {
  # …

  # The following works because the list structure is static
  vpc_security_group_ids = ["${var.security_group_1}", "${var.security_group_2}"]

  # The following doesn't work, because the [...] syntax isn't known to the interpolation language
  vpc_security_group_ids = "${var.security_group_id != "" ? [var.security_group_id] : []}"

  # Instead, it's necessary to use the list() function
  vpc_security_group_ids = "${var.security_group_id != "" ? list(var.security_group_id) : list()}"
}

The same example below converted to Terraform 0.12:*

# Configuration for Terraform 0.12

resource "aws_instance" "example" {
  # …

  vpc_security_group_ids = var.security_group_id != "" ? [var.security_group_id] : []
}

This is also true of maps, allowing the {...} syntax to be used anywhere in an expression. This is generally expected behavior that unfortunately didn't work in previous Terraform releases, but now works as expected in Terraform 0.12.

Repeated Blocks

In 0.12, HCL also no longer accepts the following counter-intuitive configuration:

# Configuration for Terraform 0.11 and earlier

output "weird" {
  value = {
    foo = "foo"
  }
  value = {
    bar = "bar"
  }
}

Due to its attempts to flatten nested block structures down to JSON’s information model, HCL would previously interpret the above as value = [{foo = “foo”},{bar = “bar”}]. While this is a logical consequence of how repeated blocks of the same type behave, it has often caused confusion for users. There are many other tricky behaviors of this sort, which cause the resulting data structure to not conform to the shape given in configuration.

In 0.12, HCL resolves this by making an explicit distinction between attributes and blocks. Attributes are single-assigned values using the assignment operator =. Blocks are repeated maps of key/value configuration pairs.

The above “weird” configuration now produces an error in HCL, because the attribute value may be defined only once. The error message will direct the user to the exact file, line, and column that the error occurred. It is valid to have more than one instance of the same block type:

# Configuration for Terraform 0.12

resource "aws_security_group" "example" {
  # …

  # "ingress" is parsed as a block because there is no equals sign
  ingress {
    # ..
  }

  ingress {
    # ..
  }
}

Upgrade Guide

Terraform 0.12 will continue to support the string interpolation syntax from prior versions. As noted in the changes above, the use of the assignment operator = with repeated blocks will now produce an error and the = must be omitted. This is a breaking change but will have an intuitive error message pointing directly to the point of the error.

Next

This was part 2 of the blog post series previewing Terraform 0.12.

First-class expressions will be released in Terraform 0.12, coming later this summer. To learn more about how to upgrade to Terraform 0.12, read the upgrade instructions which will be continuously updated as we get closer to releasing Terraform 0.12. If you have any feedback or concerns about these changes, please communicate with the Terraform team via the public mailing list.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市侥袜,隨后出現(xiàn)的幾起案子抚吠,更是在濱河造成了極大的恐慌出爹,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件残炮,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)踩窖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)晨横,“玉大人洋腮,你說(shuō)我怎么就攤上這事箫柳。” “怎么了啥供?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵悯恍,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我滤灯,道長(zhǎng)坪稽,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任鳞骤,我火速辦了婚禮窒百,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘豫尽。我一直安慰自己篙梢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布美旧。 她就那樣靜靜地躺著渤滞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪榴嗅。 梳的紋絲不亂的頭發(fā)上妄呕,一...
    開(kāi)封第一講書(shū)人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音嗽测,去河邊找鬼绪励。 笑死,一個(gè)胖子當(dāng)著我的面吹牛唠粥,可吹牛的內(nèi)容都是我干的疏魏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼晤愧,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼大莫!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起官份,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤只厘,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后舅巷,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體懈凹,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年悄谐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了介评。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖们陆,靈堂內(nèi)的尸體忽然破棺而出寒瓦,到底是詐尸還是另有隱情,我是刑警寧澤坪仇,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布杂腰,位于F島的核電站,受9級(jí)特大地震影響椅文,放射性物質(zhì)發(fā)生泄漏喂很。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一皆刺、第九天 我趴在偏房一處隱蔽的房頂上張望少辣。 院中可真熱鬧,春花似錦羡蛾、人聲如沸漓帅。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)忙干。三九已至,卻和暖如春浪藻,著一層夾襖步出監(jiān)牢的瞬間捐迫,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工爱葵, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留施戴,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓钧惧,卻偏偏與公主長(zhǎng)得像暇韧,于是被迫代替她去往敵國(guó)和親勾习。 傳聞我的和親對(duì)象是個(gè)殘疾皇子浓瞪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,336評(píng)論 0 10
  • 這本是亦舒寫(xiě)的,不是溥儀的巧婶。講了一個(gè)三十多歲的女人乾颁,當(dāng)了十多年全職太太,職場(chǎng)技能基本退化的差不多的時(shí)候被丈夫離婚的...
    土土和悟空閱讀 451評(píng)論 4 1
  • 我和L先生之間發(fā)生了許許多多的事艺栈,盡管大家看來(lái)平淡無(wú)常英岭,可對(duì)我而言卻是彌足珍貴的。第一次辦公交卡湿右,第一次吃自助诅妹,第...
    最愛(ài)的就是龍殿啊閱讀 366評(píng)論 0 0
  • “22:00點(diǎn)了……寶貝你還沒(méi)睡著嗎?” 西西小朋友有個(gè)特點(diǎn)是入睡非常難,這種狀況經(jīng)常整得媽媽抓狂發(fā)瘋吭狡。媽媽從8:...
    小米粥里的一條蟲(chóng)閱讀 155評(píng)論 0 0
  • 有一段時(shí)間尖殃,因?yàn)楣ぷ鞯年P(guān)系,跟PM打交道特別多, PM是project Manager的縮寫(xiě)划煮,即項(xiàng)目經(jīng)理送丰。項(xiàng)目管理...
    b83c46eba77f閱讀 394評(píng)論 0 0