<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Terraform on shocksolution.com</title><link>https://shocksolution.com/tags/terraform/</link><description>Recent content in Terraform on shocksolution.com</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Fri, 29 Apr 2022 18:57:01 +0000</lastBuildDate><atom:link href="https://shocksolution.com/tags/terraform/index.xml" rel="self" type="application/rss+xml"/><item><title>Google Cloud Build for Cloud Functions</title><link>https://shocksolution.com/posts/google-cloud-build-for-cloud-functions/</link><pubDate>Fri, 29 Apr 2022 18:57:01 +0000</pubDate><guid>https://shocksolution.com/posts/google-cloud-build-for-cloud-functions/</guid><description>&lt;p&gt;This post aims to add some missing information to Google&amp;rsquo;s docs about setting up CI/CD for Cloud Functions with Cloud Build.&lt;/p&gt;&#10;&lt;h2 id="global-vs-regional"&gt;Global vs Regional&lt;/h2&gt;&#10;&lt;p&gt;Cloud Build triggers are &lt;strong&gt;global&lt;/strong&gt;by default. You can create a regional trigger in the Cloud Console by changing the location when editing or creating a trigger.&lt;strong&gt;NOTE:&lt;/strong&gt; I can&amp;rsquo;t find any way at all to create a regional trigger with Terraform.&lt;/p&gt;&#10;&lt;p&gt;However, functions must be &lt;strong&gt;regional&lt;/strong&gt;. The &lt;a href="https://cloud.google.com/build/docs/deploying-builds/deploy-functions"&gt;Google docs&lt;/a&gt; indicate that you must specify a region in your cloud build config file. If you&amp;rsquo;re using a global trigger, the &lt;code&gt;`$LOCATION`&lt;/code&gt; &lt;a href="https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values"&gt;substitution variable&lt;/a&gt; is set to &lt;code&gt;global&lt;/code&gt;, and you&amp;rsquo;ll get a permission error when trying to deploy to a regional function. There are two options that I know of, and neither is great:&lt;/p&gt;</description></item><item><title>Terraform: for_each on a list of resources</title><link>https://shocksolution.com/posts/terraform-for_each-on-a-list-of-resources/</link><pubDate>Thu, 10 Mar 2022 18:25:02 +0000</pubDate><guid>https://shocksolution.com/posts/terraform-for_each-on-a-list-of-resources/</guid><description>&lt;p&gt;Terraform provides a very simply way to use for_each to iterate over a list of resources. If you have a list of strings, use the toset() function to convert the list to a set of strings.&lt;/p&gt;&#10;&lt;h2 id="example-assign-a-unique-role-on-each-resource"&gt;Example: assign a unique role on each resource&lt;/h2&gt;&#10;&lt;p&gt;My use case is setting up a number of dev environments in Google Cloud Platform. The number may change as the size of the dev team increases, so I don&amp;rsquo;t want to hard-code the number of resources anywhere in my Terraform code. The number of environments is stored in the &lt;code&gt;num_envs&lt;/code&gt; variable. For this example, I want to create a group of resources (Google Cloud Storage buckets). Each environment has its own service account, and I want each environment&amp;rsquo;s service account to have a specific role on that environment&amp;rsquo;s bucket. I also want to leverage official Google-supported Terraform modules whenever possible.&lt;/p&gt;</description></item><item><title>Auto-Create Multiple Blocks in a Terraform Resource</title><link>https://shocksolution.com/posts/auto-create-multiple-blocks-in-a-terraform-resource/</link><pubDate>Fri, 12 Nov 2021 16:35:50 +0000</pubDate><guid>https://shocksolution.com/posts/auto-create-multiple-blocks-in-a-terraform-resource/</guid><description>&lt;p&gt;Learn how to automatically create multiple Terraform resources, and multiple blocks within one Terraform resource, using the for_each meta-argument and dynamic blocks.&lt;/p&gt;&#10;&lt;h2 id="creating-multiple-resources"&gt;Creating Multiple Resources&lt;/h2&gt;&#10;&lt;p&gt;When using Terraform in a realistic environment (e.g. not a lab or tutorial), you often need to create automatically create multiple resources based on a list or map of data. This article reviews the options for creating resources based on data.There are two general methods to choose from when creating multiple resources:&lt;/p&gt;</description></item><item><title>The Best Mental Model for Writing Terraform Code</title><link>https://shocksolution.com/posts/the-best-mental-model-for-writing-terraform-code/</link><pubDate>Tue, 19 Oct 2021 03:48:29 +0000</pubDate><guid>https://shocksolution.com/posts/the-best-mental-model-for-writing-terraform-code/</guid><description>&lt;p&gt;This article explains my current mental model of Terraform, in the hope that it will save you some time in your learning process. The foundation of using any programming language or software tool correctly is to develop a valid mental model for it, and refine your model as you learn more. There isn&amp;rsquo;t one correct mental model, and your mental model must evolve as your understanding grows.&lt;/p&gt;&#10;&lt;p&gt;Your introduction to writing Terraform code is usually through a very simple example. Unfortunately, simple examples can obscure some of the fundamental concepts of the language. For example, when I first started with Terraform, I did not understand the difference between a variable and a local. Variables need to be declared in a .tf file and defined (assigned a value) in a .tfstate file (or through other means). Variables seem complicated compared to locals. You can just assign a variable to a local and start using it, like a variable in Python. Why use variables when locals seem so much easier?&lt;/p&gt;</description></item><item><title>Define a Google Load Balancer and Cloud Storage bucket with Terraform</title><link>https://shocksolution.com/posts/define-a-google-load-balancer-and-cloud-storage-bucket-with-terraform/</link><pubDate>Thu, 30 Sep 2021 18:11:54 +0000</pubDate><guid>https://shocksolution.com/posts/define-a-google-load-balancer-and-cloud-storage-bucket-with-terraform/</guid><description>&lt;p&gt;Here&amp;rsquo;s an example of using Terraform to define resources to host static content in a Google Cloud Storage bucket, fronted by a Cloud Load Balancer with a custom URL and SSL certificate. This example uses some other advanced features, such as Google Secrets and a map variable to define the SSL certificates. It&amp;rsquo;s pulled from a larger project, so this block of code isn&amp;rsquo;t guaranteed to run as-is. At a minimum, you&amp;rsquo;ll need to define the variables and set values.&lt;/p&gt;</description></item></channel></rss>