<?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 Raphael Viana on Medium]]></title>
        <description><![CDATA[Stories by Raphael Viana on Medium]]></description>
        <link>https://medium.com/@rnvdev?source=rss-78f15b3407c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8-B8Sn3RfO2kb402TWQsFg.jpeg</url>
            <title>Stories by Raphael Viana on Medium</title>
            <link>https://medium.com/@rnvdev?source=rss-78f15b3407c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:47:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rnvdev/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[Terraform: The dynamic way to provisioning resources [part.1]]]></title>
            <link>https://medium.com/raphaai/terraform-the-dynamic-way-to-provisioning-resources-part-1-5c26b01390e9?source=rss-78f15b3407c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c26b01390e9</guid>
            <category><![CDATA[platform-engineering]]></category>
            <category><![CDATA[iac]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Raphael Viana]]></dc:creator>
            <pubDate>Fri, 25 Oct 2024 16:25:20 GMT</pubDate>
            <atom:updated>2024-10-29T12:54:13.529Z</atom:updated>
            <content:encoded><![CDATA[<p>How dynamic blocks can save time and code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Rl7aqceQZl84jepy" /><figcaption>Photo by <a href="https://unsplash.com/@deneskozma?utm_source=medium&amp;utm_medium=referral">Denes Kozma</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>It’s important keep in mind a specific feel, if a simple implementation damands so much code, problably somethig it’s wrong.</p><p>Ok, Ok it’s depend about scenario that we talk about. But in Terraform pobably always there is a better way to do something more clear and semantic.</p><p><strong>So, what are more benefic? It’s create the same block of code multiples times, or create dynamically whats you want? Easy question right?<br>Thats the reason why daynamic blocks exists.</strong></p><h3>Our resource</h3><p>We’ll use <strong><em>aws_ecs_service</em></strong> resource as example:</p><pre>resource &quot;aws_ecs_service&quot; &quot;ecs_service&quot; {<br>  name                               = var.name<br>  cluster                            = var.cluster<br>  task_definition                    = var.task_definition<br>  launch_type                        = var.launch_type<br>  desired_count                      = var.desired_count<br>  platform_version                   = var.platform_version<br>  scheduling_strategy                = var.scheduling_strategy<br>  iam_role                           = var.iam_role<br>  health_check_grace_period_seconds  = var.health_check_grace_period_seconds<br>  deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent<br>  deployment_maximum_percent         = var.deployment_maximum_percent<br><br>  network_configuration {<br>    subnets          = var.subnets<br>    security_groups  = var.security_groups<br>    assign_public_ip = var.assign_public_ip<br>  }<br><br>  service_registries {<br>    registry_arn = var.service_registry_arn<br>  }<br><br>  deployment_controller {<br>    type = var.deployment_controller_type<br>  }<br><br>  dynamic &quot;load_balancer&quot; {<br>    for_each = var.load_balancer<br>    content {<br>      target_group_arn = load_balancer.value.target_group_arn<br>      container_name   = load_balancer.value.container_name<br>      container_port   = load_balancer.value.container_port<br>    }<br>  }<br><br>  tags = local.tags<br>}</pre><p>We’re using this example because it’s possible use multiple target groups throught load_balancers blocks, as AWS said here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*PfqbkM4-IzEjJrx1EA4ADQ.png" /><figcaption><a href="https://aws.amazon.com/about-aws/whats-new/2019/07/amazon-ecs-services-now-support-multiple-load-balancer-target-groups/">https://aws.amazon.com/about-aws/whats-new/2019/07/amazon-ecs-services-now-support-multiple-load-balancer-target-groups/</a></figcaption></figure><h3>Dynamic block</h3><p>So, <strong><em>load_balancer</em></strong> block it’s a little different. Let’s dive in this part:</p><pre>dynamic &quot;load_balancer&quot; {<br>    for_each = var.load_balancer<br>    content {<br>      target_group_arn = load_balancer.value.target_group_arn<br>      container_name   = load_balancer.value.container_name<br>      container_port   = load_balancer.value.container_port<br>    }<br>  }</pre><p>Load_balancer block cam receive a list with multiples <strong>objects</strong> with those parameters:<br><strong><em>- target_group_arn<br>- container_name<br>- container_port</em></strong></p><pre>[<br> {<br>   target_group_arn = &quot;tg_arn_example&quot;<br>   container_name   = &quot;container_name_example&quot;<br>   container_port   = 80<br> },<br> {<br>   target_group_arn = &quot;tg_arn_example2&quot;<br>   container_name   = &quot;container_name_example2&quot;<br>   container_port   = 8080<br> }<br>]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*NMviHuFV9C4sCz53pXz7pQ.png" /><figcaption>Terraform aws provider version warning.</figcaption></figure><h3>Variable</h3><p>This dynamic block works as a loop, runs <strong>N times</strong> according the number of objects in a list (<strong><em>for_each = var.load_balancer)</em></strong>.</p><p>To be more clear, look the <strong>variable</strong> structure:</p><pre>variable &quot;load_balancer&quot; {<br>  description = &quot;List of load balancers&quot;<br>  type = list(object({<br>    target_group_arn = string<br>    container_name   = string<br>    container_port   = number<br>  }))<br>}</pre><p>As you can see, the variable<strong><em> load_balancer</em></strong>, it’s of type: <strong>list</strong> of <strong>objects.<br></strong>So we can feed this variable like this:</p><pre>load_balancer = [<br> {<br>   target_group_arn = &quot;tg_arn_example&quot;<br>   container_name   = &quot;container_name_example&quot;<br>   container_port   = 80<br> },<br> {<br>   target_group_arn = &quot;tg_arn_example2&quot;<br>   container_name   = &quot;container_name_example2&quot;<br>   container_port   = 8080<br> }<br>]</pre><p>In another words: <strong><em>load_balancer = [</em></strong><em>{object_1}</em><strong><em>,</em></strong><em>{object_2}</em><strong><em>]</em></strong></p><h3>Guardrails</h3><p>Your can create guardrails inside variable to keep cosistency, maybe this code can be a new module in your company, so it’s important to use <strong>validation blocks</strong>.</p><p>According to AWS documentation, this resource (ecs_service) can recive one or more load balancers block in your configuration body. <br>For educational purpose, imagine we have a rule that each ecs_service only will can has two tg’ s (target groups), we should check this condition before use it.</p><pre>  validation {<br>    condition     = length(var.load_balancer) &lt;=2<br>    error_message = &quot;Then load_balancer variable can hold in max 2 objects&quot;<br>  }</pre><p><strong>Validation </strong>block are based in two <strong>parameters</strong>, first: <strong>condition</strong> <em>(when logical operation occurs) </em>and second: <strong>error_massage</strong> <em>(if condition returns false, this message will be show to user, in terminal or pipeline log)</em>.</p><p>The whole structure looks like this:</p><pre>variable &quot;load_balancer&quot; {<br>  description = &quot;List of load balancers&quot;<br>  type = list(object({<br>    target_group_arn = string<br>    container_name   = string<br>    container_port   = number<br>  }))<br><br> validation {<br>    condition     = length(var.load_balancer) &lt;=2<br>    error_message = &quot;Then load_balancer variable can hold in max 2 objects&quot;<br>  }<br>}</pre><p><strong>Length function </strong>will perform this operation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*ohLzwwimE7qho8v21qKN7w.png" /></figure><p>In this article we learnd how create a dynamic block, how dynamic block works and handles inputs structures to feed it. And the advantages of using this approach to keep code clean and mantainable.</p><p>Would you like to see more Terraform and another cool tech stuffs? Share and follow me! See you in the next article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c26b01390e9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/raphaai/terraform-the-dynamic-way-to-provisioning-resources-part-1-5c26b01390e9">Terraform: The dynamic way to provisioning resources [part.1]</a> was originally published in <a href="https://medium.com/raphaai">raphaai</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>