{"id":12948,"date":"2020-04-21T13:58:36","date_gmt":"2020-04-21T13:58:36","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=12948"},"modified":"2021-11-12T05:38:38","modified_gmt":"2021-11-12T05:38:38","slug":"terraform-interpolation-explained-with-example-programs","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-interpolation-explained-with-example-programs\/","title":{"rendered":"Terraform interpolation explained with example programs"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>What is interpolation?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Interpolation is the estimation of a value or set of values based on their context. In other words, Interpolation is a useful mathematical and statistical tool used to estimate values between two points.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What is Terraform interpolation?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Terraform, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Embedded within strings in Terraform, whether you&#8217;re using the Terraform syntax or JSON syntax, you can interpolate other values. These interpolations are wrapped in ${}, such as ${var.foo}.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>You can perform simple math in interpolations, allowing you to write expressions such as ${count.index<\/li><li>1}. And you can also use conditionals to determine a value based on some logic.<\/li><li>You can escape interpolation with double dollar signs: $${foo} will be rendered as a literal ${foo}.<\/li><li>The string interpolation syntax ${ \u2026 } has been part of Terraform since its initial release in 2015.<\/li><li>In Terraform 0.12, is now extended to include support for conditionals and for expressions. These new features are most useful within multi-line strings. Terraform is extending interpolation syntax used for strings (${}) to loops and conditionals (which implies interpolation syntax is still valid for strings).<\/li><\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Configuration for Terraform 0.12<\/span>\n\nlocals {\n  lb_config = &lt;&lt;EOT\n%{ <span class=\"hljs-keyword\">for<\/span> instance in opc_compute_instance.example ~}\nserver ${instance.label} ${instance.ip_address}:<span class=\"hljs-number\">8080<\/span>\n%{ <span class=\"hljs-keyword\">endfor<\/span> }\nEOT\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"569\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/Terraform-interpolation.jpg\" alt=\"\" class=\"wp-image-12949\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/Terraform-interpolation.jpg 720w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/Terraform-interpolation-300x237.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>More Reference<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/www.terraform.io\/docs\/configuration-0-11\/interpolation.html<\/li><li>https:\/\/www.terraform.io\/docs\/configuration\/expressions.html<\/li><\/ul>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_99451\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_31961\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_99451\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is interpolation? Interpolation is the estimation of a value or set of values based on their context. In other words, Interpolation is a useful mathematical and&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5129],"tags":[],"class_list":["post-12948","post","type-post","status-publish","format-standard","hentry","category-terraform"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=12948"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12948\/revisions"}],"predecessor-version":[{"id":24864,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12948\/revisions\/24864"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=12948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=12948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=12948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}