9/20/2023: The open source version of Terraform is now OpenTofu
Want to read it with nice formatting? Check out the Notion page. Continuing from the post about Modules, let's look at Dynamic Blocks.
What are Dynamic Blocks?
It's a way to construct dynamically repeatable nested blocks in Terraform code. Think about using for_each - This is often used to make individual resources with a value to iterate over.
Is This a Dynamic Block?
I’ve done something like this, but it involved the multiple function (*) and a stand-in variable ${var.ex} .
network_interface_ids = ["${element(azurerm_network_interface.CA-NetInt.*.id, 01)}"]
The index (01) was the number of network_interface_ids one would want.
Was that unknowingly a dynamic block, or something else? By all means, comment what you think.
Apparently, It Wasn’t
resource "aws_elastic_beanstalk_environment" "tfenvtest" {
name = "tf-test-name"
application = "${aws_elastic_beanstalk_application.tftest.name}"
solution_stack_name = "64bit Amazon Linux 2018.03 v2.11.4 running Go 1.12.6"
dynamic "setting" {
for_each = var.settings
content {
namespace = setting.value["namespace"]
name = setting.value["name"]
value = setting.value["value"]
}
}
}
- There are different kinds of dynamic blocks -
settingup there is one. Others arelistandmap,and will reference different values. - for_each - The ieterative variable
- Not to be confused with the
ieteratorargument, which is option, and sets the name of a temp var. Since it’s not here, it’s defaulting tosettingup there. settinghas two attributes;keymaps the key or lists element index for the current element. It will be identical tovalueif the expression has thesetvalue.valueis the value of the current element.
- Nested
contentdefines the body of each generated block. It doesn’t seem to match the variables in theresourceblock.
We can’t generate meta-argument blocks like lifecycle or provisioner.
References:
Using Dynamic Blocks in Terraform - Ned In The Cloud
Comments
Post a Comment