Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Terraform Interpolation vs. Directives
Artem
Artem

Posted on

     

Terraform Interpolation vs. Directives

Every Terraform user should be familiar with HashiCorp Language (HCL) aka Terraforminterpolation syntax.

Let say I have a variable defined asfilename, then interpolation allows me to do stuff like that:

resource"local_file""index"{filename="${var.filename}.txt"content="foo!"}
Enter fullscreen modeExit fullscreen mode

The Terraform official website says:

[Interpolation] evaluates the expression given between the markers, converts the result to a string if necessary, and then inserts it into the final string.

In other words, it can do some elementary operation and dump result as a string.

This is neat, and can be helpful if we want to add suffix or prefix to an EC2 instance, but what if we are tasked with generating a config file dynamically with unknown number of nested blocks?

That's wheredirectives come into play!

The Terraform docs have this short definition of the directive:

[It] allows for conditional results and iteration over collections.

It is kinda likefor_each, but inside of string rather than resource.

An example should make things clear!

variable"filename"{default="index.html"}variable"facts"{default=["fun","hard"]}variable"complex_facts"{default=[{element:"h2"content:"FUN!"},{element:"h1"content:"EASY!"}]}resource"local_file""index_html"{filename="${path.module}/${var.filename}"content=<<EOT    <html>    %{for fact in var.facts}      <p>Terraform is ${fact}</p>    %{endfor}    %{for fact in var.complex_facts}      <${fact.element}>Terraform is ${fact.content}</${fact.element}>    %{endfor}    </html>  EOT}
Enter fullscreen modeExit fullscreen mode

You can see how listfacts is used to generate HTML

elements:

%{forfactinvar.facts}<p>Terraformis${fact}</p>%{endfor}
Enter fullscreen modeExit fullscreen mode

The same syntax can be used on objects, like with incomplex_facts case:

%{forfactinvar.complex_facts}<${fact.element}>Terraformis${fact.content}</${fact.element}>%{endfor}
Enter fullscreen modeExit fullscreen mode

The resulting HTML file is pretty ugly:

<html><p>Terraform is fun</p><p>Terraform is hard</p><h2>Terraform is FUN!</h2><h1>Terraform is EASY!</h1></html>
Enter fullscreen modeExit fullscreen mode

This can be used to generate tiny and ugly HTMLs (although there are better tools for this purpose), but it can also be used for more complex configuration files!

It can also be used to replace ternary expression, which some people find hard to read.

This interpolation sequence:

resource"local_file""txt"{filename="${path.module}/file.txt"content=<<EOT    ${ var.content != "" ? var.content : "NO CONTENT PROVIDED"}  EOT}
Enter fullscreen modeExit fullscreen mode

can be replace by the following directives sequence:

resource"local_file""txt"{filename="${path.module}/file.txt"content=<<EOT    %{ if var.content != "" }${var.content}%{ else }NO CONTENT PROVIDED!%{ endif }  EOT}
Enter fullscreen modeExit fullscreen mode

That's all for today! Have fun hacking Terraform!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Santa Rosa, CA
  • Education
    San Francisco State University
  • Work
    Cloud Security Engineer at Virtru
  • Joined

More fromArtem

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp