Terraform now provides new functions, making itself more powerful as a configuration mgmt tool.

Here are some memos:

To make var value join with fixed string: "test-${each.key}"

cidrsubnet

From Terraform official documents, they have an example as following and explained in none humane language way. So I'll try to interpret it from a Network Expert perspective.

cidrsubnet(prefix, newbits, netnum)

And an example below:

> cidrsubnet("172.16.0.0/12", 4, 2)
172.18.0.0/16
> cidrsubnet("10.1.2.0/24", 4, 15)
10.1.2.240/28
> cidrsubnet("fd00:fd12:3456:7890::/56", 16, 162)
fd00:fd12:3456:7800:a200::/72

As what we can see from above example, although their documents explained with thousands of tech words, this cidrsubnet(prefix=10.1.2.0/24, newbits=4, netnum=15) actually means increase 4 subnets based on prefix subnet, and use subnet number 15.

count

Official example:

resource "aws_instance" "server" {
  count = 4 # create four similar EC2 instances

  ami           = "ami-a1b2c3d4"
  instance_type = "t2.micro"

  tags {
    Name = "Server ${count.index}"
  }
}

this will create tags with server name from 0 to 3.

for_each

This is newly added on ver12.6. Official example:

resource "azurerm_resource_group" "rg" {
  for_each = {
    a_group = "eastus"
    another_group = "westus2"
  }
  name     = each.key
  location = each.value
}

this will create

resource "azurerm_resource_group" "rg[a_group]" {
  name     = "a_group"
  location = "eastus"
}
resource "azurerm_resource_group" "rg[another_group]" {
  name     = "another_group"
  location = "westus2"
}