<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by francesco antonio migliorin on Medium]]></title>
        <description><![CDATA[Stories by francesco antonio migliorin on Medium]]></description>
        <link>https://medium.com/@Giglium?source=rss-18affb055a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wt2EbdTObC7rAYhvwxmIqg.jpeg</url>
            <title>Stories by francesco antonio migliorin on Medium</title>
            <link>https://medium.com/@Giglium?source=rss-18affb055a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 19:28:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Giglium/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Installing the HashiCorp Vault CSI on Openshift 4]]></title>
            <link>https://medium.com/kiratech/installing-the-hashicorp-vault-csi-on-openshift-4-6bc0b2c0b841?source=rss-18affb055a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bc0b2c0b841</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[openshift]]></category>
            <category><![CDATA[openshift-4]]></category>
            <category><![CDATA[hashicorp-vault]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[francesco antonio migliorin]]></dc:creator>
            <pubDate>Wed, 29 Sep 2021 12:59:16 GMT</pubDate>
            <atom:updated>2021-09-29T12:59:16.693Z</atom:updated>
            <content:encoded><![CDATA[<p>The HashiCorp Vault Container Storage Interface (CSI) allows you to mount Vault Secrets stored in HashiCorp Vault as ephemeral Kubernetes Volumes.</p><p>However, if you tried to follow the <a href="https://learn.hashicorp.com/tutorials/vault/kubernetes-secret-store-driver?in=vault/kubernetes">official tutorial</a> on an Openshift 4.x cluster, you have most likely had some troubles, mostly due to OpenShift’s strict permission policies.</p><p>In this article, we will learn how to install the HashiCorp Vault CSI on Openshift 4.x.</p><h3>Prerequisites</h3><ul><li><a href="https://helm.sh/">Helm CLI</a>;</li><li><a href="https://docs.openshift.com/container-platform/4.8/cli_reference/openshift_cli/getting-started-cli.html">OpenShift CLI</a>;</li><li>An OpenShift 4.x cluster.</li></ul><h3>Install Vault CSI</h3><ol><li>Let’s start creating a new project for Vault and Kubernetes CSI:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c3b45fb56b7ef778a1a9c31f3a348f2/href">https://medium.com/media/6c3b45fb56b7ef778a1a9c31f3a348f2/href</a></iframe><p>2. Vault CSI and Kubernetes CSI need to have a <em>privileged</em> <a href="https://docs.openshift.com/container-platform/4.8/authentication/managing-security-context-constraints.html">SecurityContextConstraint (SCC)</a>. For this reason we are going to give it to their default service account:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28f27456fc0075e5aa95ea2b2f7c0266/href">https://medium.com/media/28f27456fc0075e5aa95ea2b2f7c0266/href</a></iframe><p>3. Add the Secrets Store CSI driver and the HashiCorp Helm repositories:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/891267d4ef2a0e9d25faf8aa8e72edb5/href">https://medium.com/media/891267d4ef2a0e9d25faf8aa8e72edb5/href</a></iframe><p>4. Install the Kubernetes-Secrets-Store-CSI-Driver:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ce79cfd9b48a06d2fa3fe5e26cc64c1/href">https://medium.com/media/3ce79cfd9b48a06d2fa3fe5e26cc64c1/href</a></iframe><blockquote><em>This command will take a while because we are also waiting until all the pods are up&amp;running.</em></blockquote><p>5. Install the Vault Helm chart. In this article, we will run it in development mode, with the injector service disabled and the CSI enabled:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45ff8dbcb0c58d038a88b4c505baa86c/href">https://medium.com/media/45ff8dbcb0c58d038a88b4c505baa86c/href</a></iframe><p>6. If we check the pods now, we will see all the Vault CSI pods are in a <em>CrashLoopBackOff </em>state:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0b6373996bc6f1981b73f7bc869b982/href">https://medium.com/media/b0b6373996bc6f1981b73f7bc869b982/href</a></iframe><p>This is because the CSI DaemonSet containers need to be run as privileged too. The only way to solve this issue is to patch it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3fa824906de69bd5439a2dda51e11d6/href">https://medium.com/media/a3fa824906de69bd5439a2dda51e11d6/href</a></iframe><p>After some seconds all the pods will be up&amp;running.</p><h3>Configure Vault</h3><ol><li>Start an interactive shell session on the <em>vault-0</em> pod:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28c6802e943e8bcb0fc4ac71414af080/href">https://medium.com/media/28c6802e943e8bcb0fc4ac71414af080/href</a></iframe><p>2. Create a test secret and its policy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94243dd126d961ec5db712af3a3fe84c/href">https://medium.com/media/94243dd126d961ec5db712af3a3fe84c/href</a></iframe><p>3. Configure the Kubernetes authentication method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/424ae5a005ad4b133976e875f075630c/href">https://medium.com/media/424ae5a005ad4b133976e875f075630c/href</a></iframe><p>4. Create a role for the test application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65a763915822d5ca8cd848b02e5e9298/href">https://medium.com/media/65a763915822d5ca8cd848b02e5e9298/href</a></iframe><p>5. Exit the <em>vault-0</em> pod with the <em>exit </em>command.</p><h3>Run a demo application</h3><ol><li>1. Let’s create a new demo project:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9d282157755f413333b1a4a91da74c1/href">https://medium.com/media/b9d282157755f413333b1a4a91da74c1/href</a></iframe><p>2. The Kubernetes-CSI-Driver provides a CRD named <em>SecretProviderClass</em>. This resource describes the parameters that are given to the Vault CSI provider. So let’s create it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ee16f42b350fe97f287a0965df21775/href">https://medium.com/media/2ee16f42b350fe97f287a0965df21775/href</a></iframe><p>3. Before launching our demo pod, we need to overcome one last obstacle. The pod needs to have the ability to grab CSI volumes. To do so, we need to create a new SCC or patch an existing one and add it to our pod’s service account. In this article, I will patch the <em>restricted</em> SCC and, will add it to the <em>default</em> service account in the <em>demo</em> namespace:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc3cc47842938a11bea77a9c2695a56c/href">https://medium.com/media/cc3cc47842938a11bea77a9c2695a56c/href</a></iframe><p>4. Now we can deploy the demo pod:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bdd3eead15debaa4fb9e3426b40d09f/href">https://medium.com/media/4bdd3eead15debaa4fb9e3426b40d09f/href</a></iframe><p>5. We can finally display the secret password written to the file system:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8b26b689df3b60685b095779d71194/href">https://medium.com/media/3a8b26b689df3b60685b095779d71194/href</a></iframe><h3>Clean up</h3><p>To remove all the things created in this article, we need to run the following commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54a101bd0a82488451d58c7832dac6c6/href">https://medium.com/media/54a101bd0a82488451d58c7832dac6c6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bc0b2c0b841" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kiratech/installing-the-hashicorp-vault-csi-on-openshift-4-6bc0b2c0b841">Installing the HashiCorp Vault CSI on Openshift 4</a> was originally published in <a href="https://medium.com/kiratech">kiratech.io</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a local three-broker Apache Kafka cluster]]></title>
            <link>https://medium.com/kiratech/setting-up-a-local-three-broker-apache-kafka-cluster-b459b28a6a4f?source=rss-18affb055a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b459b28a6a4f</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[zookeeper]]></category>
            <category><![CDATA[cluster]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[francesco antonio migliorin]]></dc:creator>
            <pubDate>Tue, 20 Jul 2021 13:02:43 GMT</pubDate>
            <atom:updated>2021-07-20T13:02:43.555Z</atom:updated>
            <content:encoded><![CDATA[<h3>Setting up a three-broker Apache Kafka cluster on a single node</h3><p>In this post we will learn how to to set up an <a href="https://kafka.apache.org/">Apache Kafka</a> cluster with 3 brokers on a single machine for testing purposes. The procedure is customizable and can be used to run as many brokers as needed on a single node, which can be useful while designing software and infrastructure with fault-tolerance in mind on development environments with limited capacity.</p><p>Apache Kafka is an open-source distributed event streaming platform in real-time. Kafka stores key-value messages that come from sources called <em>producers</em>. Messages are indexed and stored with a timestamp and can be consumed by processes called <em>consumers</em>. Kafka stores messages into different <em>partitions</em>, each belonging to a specific <em>topic</em>. Partitions can be replicated for fault tolerance. Specifically, consumers read from specific partitions within a topic.</p><h3>Prerequisites</h3><ul><li>JDK 8 and above</li></ul><h3>Setup</h3><h4>Step 1: Download Kafka</h4><p>You can download a stable Kafka release from <a href="https://downloads.apache.org/kafka/">the official website</a>. In this tutorial we are going to use kafka_2.12-2.6.2.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c38d4894c74e543168747c2f23c8be68/href">https://medium.com/media/c38d4894c74e543168747c2f23c8be68/href</a></iframe><h4>Step 2: Creating three identical folders</h4><p>Let’s rename the extracted folder and copy it twice, in order to get three identical Kafka folders, one per broker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8eba6ad1ca4d062d700ad88ddf015db/href">https://medium.com/media/a8eba6ad1ca4d062d700ad88ddf015db/href</a></iframe><h4>Step 3: Starting a Zookeeper server</h4><p>Start a Zookeeper server. Only one Zookeeper is needed, since it can coordinate many Kafka brokers and consumers. In this example, we will run the one located in the kafka_1 folder by means of kafka_1/bin/zookeeper-server-start.sh kafka_1/config/zookeeper.properties</p><p>If zookeeper is running correctly you will see an output similar to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce233f389b6ae51f85c0fc6127463e43/href">https://medium.com/media/ce233f389b6ae51f85c0fc6127463e43/href</a></iframe><p>This output shows that Zookeeper is up and running, listening on port 2181. Beware: <strong>do not kill this process!</strong></p><h4>Step 4: configuring the first Kafka broker</h4><p>Leave your Zookeeper terminal untouched and open a new terminal window in the same Virtual Machine. We will now conifgure the first Kafka broker by editing the kafka_1/server.properties file with your text editor of choice. In our case, using vi: vi kafka_1/server.properties:</p><ul><li>Set broker.id to 1. This attribute is the broker&#39;s unique identifier. It can be any number, but it must be unique in your cluster.</li><li>Uncomment the listeners=PLAINTEXT://:9092 row and change the port to 9091. This means the broker will be listening on port 9091 for connection requests.</li><li>Edit the log.dirs configuration. As a matter of fact, the log path can&#39;t be the same among different brokers. We suggest using the following configuration: log.dirs=/tmp/kafka_1-logs.</li><li>Save and exit</li></ul><h4>Step 5: running the first Kafka broker</h4><p>We can now start the kafka_1 broker with kafka_1/bin/kafka-server-start.sh kafka_1/config/server.properties.</p><p>If it is running correctly, you will see an output like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/070912819a05c60129e167babbd6a403/href">https://medium.com/media/070912819a05c60129e167babbd6a403/href</a></iframe><p>Leave this shell open too, and open two other terminal windows, one for kafka_2 and one for kafka_3</p><h4>Step 6: configuring the other two brokers</h4><p>It is now sufficient to repeat points 4 and 5 with kafka_2/server.properties and kafka_3/server.properties in the two terminals you just opened, applying the following changes:</p><ul><li>change broker.id to 2 and 3, respectively;</li><li>use listeners=PLAINTEXT://:9092 in kafka_2 and listeners=PLAINTEXT://:9093 in kafka_3;</li><li>change the log.dirs to /tmp/kafka_2-logs and /tmp/kafka_3-logs, respectively.</li></ul><p>Finally, run kafka_2/bin/kafka-server-start.sh kafka_2/config/server.properties in one terminal and kafka_3/bin/kafka-server-start.sh kafka_3/config/server.properties in the other.</p><h4>Step 7: enjoy!</h4><p>You now have a fully configured Kafka cluster with three brokers running on a single VM, which is wonderful for development purposes!</p><p>Hopefully, this tutorial has saved you quite some time. If you have any idea or question, feel free to leave a comment below — we will be delighted to try and help.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b459b28a6a4f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kiratech/setting-up-a-local-three-broker-apache-kafka-cluster-b459b28a6a4f">Setting up a local three-broker Apache Kafka cluster</a> was originally published in <a href="https://medium.com/kiratech">kiratech.io</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Five Terraform tips for common problems]]></title>
            <link>https://medium.com/kiratech/five-terraform-tips-for-common-problems-7c888d09922f?source=rss-18affb055a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c888d09922f</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[hashicorp]]></category>
            <category><![CDATA[cloud-native]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[francesco antonio migliorin]]></dc:creator>
            <pubDate>Wed, 19 May 2021 15:31:00 GMT</pubDate>
            <atom:updated>2021-05-19T15:31:00.536Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Terraform </strong>is a <strong>IaC tool </strong>which provides a common syntax to interact with multiple <strong>cloud providers</strong>. Most of the time, though, people face presumably <strong>easy problems </strong>that Terraform transforms into <strong>real nightmares</strong>. In this post, we will present <strong>workarounds </strong>or <strong>solutions </strong>to <strong>five common problems </strong>where novices usually ask for help.</p><blockquote><em>Note that Terraform’s current version is </em><em>0.15 -- hopefully, with future major releases, useful functionalities to face these problems will be introduced.</em></blockquote><h3>Prerequisites</h3><p>Before reading this article you need a basic understanding of Terraform and its syntax. If you never heard of Terraform before we suggest that you read: <a href="https://www.terraform.io/intro/index.html">Introduction — Terraform by HashiCorp</a></p><h3>Don’t abuse count!</h3><p>The instructions count and for_each are very similar but for_each has some advantages because it allows for accessing data structures by key rather than index. Maybe programmers like count because it is easier to write -- we are still trying to figure this out 😄</p><p>But why is count such a bad guy? Let&#39;s take a look at the following example.</p><p>Let’s create a very simple code that prints the favorite operating system of a person:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e706dca2f3d715df5bb70a142c595b7/href">https://medium.com/media/7e706dca2f3d715df5bb70a142c595b7/href</a></iframe><p>This results in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bc9e7f41bceb1dd38a484eb19eb0d7c/href">https://medium.com/media/4bc9e7f41bceb1dd38a484eb19eb0d7c/href</a></iframe><p>But what happens if we remove Bob? Now, the local.people variable looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b9673695b41488e7ed5767fd1469b37/href">https://medium.com/media/8b9673695b41488e7ed5767fd1469b37/href</a></iframe><p>If we run terraform apply again, the result may surprise the oblivious Terraform user:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbfea3794b8ee7b9781fe72f0cf13923/href">https://medium.com/media/fbfea3794b8ee7b9781fe72f0cf13923/href</a></iframe><p>Terraform destroyed null_resource.hello[2]... which was Carlo! What about Bob? Why was Carlo recreated?</p><p>Is this a Terraform bug or is it our mistake? What happened? Let’s take a look at the terraform plan:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34394cd6f37eac4b7041c92fa4dea21b/href">https://medium.com/media/34394cd6f37eac4b7041c92fa4dea21b/href</a></iframe><p>Instead of just removing Bob and leaving the current resources untouched, Terraform destroyed Carlo and replaced Bob with Carlo. This is because the unique identifier of Bob and Carlo was their index in the local.people list rather than the name.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62c76350810b2220e1c88c98d2f92862/href">https://medium.com/media/62c76350810b2220e1c88c98d2f92862/href</a></iframe><p>The consequences of this behavior could have been dreadful if we had been working with VMs! Now, let us show you why you should have used for_each instead:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97d693d640132c9ea5bd88dd96564051/href">https://medium.com/media/97d693d640132c9ea5bd88dd96564051/href</a></iframe><blockquote><em>Each element in the iteration needs to have a unique key. This is the design principle behind the </em><em>for_each loop, hence we need to transform our list to a map. For doing this we use </em><em>{ for p in local.people : p.name =&gt; p }. Thanks to this syntax, we create a map where the keys are the names and the values are the objects of the list (i.e. name and favorite OS). Note that we could also map names to favorite OS&#39;s directly</em></blockquote><p>This results in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca4682c474f3423cade3519ecbd6cf8e/href">https://medium.com/media/ca4682c474f3423cade3519ecbd6cf8e/href</a></iframe><p>Here you can see the first difference: the name, which is the unique key in your for_each loop, is printed in the log. For instance, null_resource.hello[2] -which was Carlo- is now printed as null_resource.hello[&quot;Carlo&quot;]. We can now remove Bob and see what happens. Now, local.people look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5809e3d5cbc24fe0fa79b958c05e42e8/href">https://medium.com/media/5809e3d5cbc24fe0fa79b958c05e42e8/href</a></iframe><p>This results in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55e207951424f69f960dfaa07f1ff9f8/href">https://medium.com/media/55e207951424f69f960dfaa07f1ff9f8/href</a></iframe><p>This is great! Only Bob was deleted and Carlo was left untouched.</p><p>Let’s recap what we learned here. While looping, Terraform uses an unique identifier for each iteration. For count, the identifier is a number; for_each, instead, uses the key of the map.</p><h3>Recreate a resource each time an apply is run</h3><p>Sometimes we want to define a resource which runs every time we execute an apply. Since Terraform saves the result of every execution in its state, we need to bind this &quot;ephemeral&quot; resource to something that always changes. The simplest way is to use a trigger with the timestamp function, as you can see in the following code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c94b1f672d8fdec6a73a1b287bc7dcf/href">https://medium.com/media/1c94b1f672d8fdec6a73a1b287bc7dcf/href</a></iframe><p>Every time we run a terraform apply, the timestamp function will return a different value. Thanks to this behavior, Terraform will be forced to destroy the previous resource and it will recreate a new one.</p><h3>Save variables to be executed during a destroy</h3><p>Provisioners like local-exec and remote-exec give developers the possibility to perform some actions when destroying resources. Usually, these actions are meant for clean-up but developers often encounter an unsuspected error. Take a look at this code and try to find the error:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/533f61b846c7e63042753328b0ecaf8d/href">https://medium.com/media/533f61b846c7e63042753328b0ecaf8d/href</a></iframe><p>Let’s try to perform a terraform apply to figure the error out:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9c64a3adbeffb557f3e17f5bd10ad93/href">https://medium.com/media/e9c64a3adbeffb557f3e17f5bd10ad93/href</a></iframe><p>This error may scare you but, luckily, the solution is very simple. Keep in mind this golden rule: <em>“Every time you need a variable at destroy-time, you need to create a trigger!”</em>. Very simple, right? Let’s see how to fix the previous code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb978f9d6212c5b5da4cfab74de6080e/href">https://medium.com/media/cb978f9d6212c5b5da4cfab74de6080e/href</a></iframe><p>This results in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f91a6e8aa74ac963725a20e1ff7c59c8/href">https://medium.com/media/f91a6e8aa74ac963725a20e1ff7c59c8/href</a></iframe><h3>Nested Loops</h3><p>If you are lucky while using nested loops, the first one is on a resource, and the second one is on a block, which means a dynamic block will do the trick. Unfortunately, sometimes nested loops over resources, modules or variables are needed. Let&#39;s imagine this situation: given a list of environments, a resource must be called in order to install, for each environment, the appropriate software.</p><p>A very simplified variable to simulate this situation may look as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/610177d20917bddb65ec0adbd144c358/href">https://medium.com/media/610177d20917bddb65ec0adbd144c358/href</a></iframe><blockquote><em>In this example, we have only two keys for each object, but try to imagine having many different variables related to many different tasks…</em></blockquote><p>With only one for_each the maximum result that we can obtain is something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3f4c5290a4efd290e18799ad9e9eaa3/href">https://medium.com/media/a3f4c5290a4efd290e18799ad9e9eaa3/href</a></iframe><p>In this example, we are not able to loop over the programs variable and, therefore the best we can do is getting one element from (or maybe joining) the list.</p><p>To achieve our goal, we need to loop over a supporting module. Let&#39;s create the folder ./modules/setup; your .tf files inside it will look like this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b24c0e5de1005917365f09ab163b1164/href">https://medium.com/media/b24c0e5de1005917365f09ab163b1164/href</a></iframe><blockquote><em>For the article, we wanted to use as less code as possible for brevity; in your works, follow the official guidelines: </em><a href="https://www.terraform.io/docs/language/modules/develop/index.html"><em>Creating Modules — Terraform by HashiCorp</em></a></blockquote><p>As you can see, in our module we accept one environment and a list of programs as inputs. <em>Inside</em> the module we perform one loop over the programs variable. Now, we can loop over our module to achieve our goal:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d4d488ec49d571c1ebdb2a521b19d84/href">https://medium.com/media/9d4d488ec49d571c1ebdb2a521b19d84/href</a></iframe><p>This results in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5c41fa8f44bb4a8681ffab6ea11801d/href">https://medium.com/media/c5c41fa8f44bb4a8681ffab6ea11801d/href</a></iframe><p>In other words, you can nest modules in order to achieve what in traditional programming would be nested looping. This can be repeated for as many layers as desired.</p><h3>Be careful with external!</h3><p>Although Terraform’s developers discourage the usage of the external data-source, it has become very popular because it allows the user to catch the output of an external program, conceptually run as a subprocess.</p><p>The major problem with external is that some developers use it to perform an action <em>once</em>. Since external is a data-source rather than a resource, it will run on <em>every</em> Terraform plan or apply. Therefore, when you design an external script to be used with external, it needs to able to detect when no action needs to be taken, otherwise the defined action will be executed each time over and over again.</p><p>Let’s see a very simple example of this problem. Consider the following Terraform script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/237f90a8066e56bc6149bf124cd693af/href">https://medium.com/media/237f90a8066e56bc6149bf124cd693af/href</a></iframe><p>The script.sh will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2c00a3528f79b30ad94cd8e69fcb506/href">https://medium.com/media/f2c00a3528f79b30ad94cd8e69fcb506/href</a></iframe><p>If we try to run terraform plan you will probably notice that the demo.txt was created. You can now run terraform apply --auto-approve as many times as you want and you will see that a new id will appear every time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c888d09922f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kiratech/five-terraform-tips-for-common-problems-7c888d09922f">Five Terraform tips for common problems</a> was originally published in <a href="https://medium.com/kiratech">kiratech.io</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>