Followers

Terraform - dynamic blocks

Dynamic Blocks   are more or less another way to implement   for  a  loop. Here are a few facts    dynamic block   which you should keep in ...

Dynamic Blocks are more or less another way to implement for a loop. Here are a few facts  dynamic block which you should keep in mind -

  1. Collections - You need to have collections .e.g. - list, map, set
  2. Iterator - To create a dynamic block you need to define an iterator.
  3. Content - Content is something onto which you wanna iterate.

Here is the syntax of dynamic block -

Before we implement our first terraform dynamic block let's first see an example without dynamic block.

In this example, we are going to create two ingress rules for the aws_security_group. Both ingress rules are exactly the same apart from the port numbers .i.e. - 80 and 443. So if we do not use dynamic block then we need to create two ingress rules blocks inside the terraform file.

provider "aws" {
  region ="us-east-1"
}
//security
resource "aws_security_group" "sg1" {

 ingress {
      description = "ingress_rule_1"
      from_port   = 443
      to_port     = 443
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
   }
   
   ingress {
      description = "ingress_rule_2"
      from_port   = 80
      to_port     = 80
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
   }
   tags = {
     "Name" = "Static Inbound Rule"
   }
}



The same terraform file can be improved by using dynamic block, now look at the following terraform file -
provider "aws" {
  region ="us-east-1"
}
//locals
locals {
   ingress_rules = [{
      port        = 443
      description = "Ingress rules for port 443"
   },
   {
      port        = 80
      description = "Ingree rules for port 80"
   }]
}
//security
resource "aws_security_group" "sg1" {
dynamic "ingress" {
      for_each = local.ingress_rules

      content {
         description = ingress.value.description
         from_port   = ingress.value.port
         to_port     = ingress.value.port
         protocol    = "tcp"
         cidr_blocks = ["0.0.0.0/0"]
      }
   }
   tags = {
     "Name" = "Dynamic Inbound Rule"
   }
}


Now you can imagine, if you need to define more than 2 ingress rules then using dynamic block can help you to reduce the line of code inside your terraform file.

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,Windows,1,YAML Basics,1,
ltr
item
DevOpsWorld: Terraform - dynamic blocks
Terraform - dynamic blocks
https://jhooq.com/wp-content/uploads/terraform/dynamic-block/terraform-dynamic-block.webp
DevOpsWorld
https://www.devopsworld.co.in/2022/09/terraform-dynamic-blocks.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/09/terraform-dynamic-blocks.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content