Using Dynamic Blocks in Terraform

Morgan Lucas
2 min readApr 8, 2022

--

Want to read it with nice formatting? Check out the Notion page.

What are they?

You put inside of resource blocks, to potentially repeat multiples of a same block type.

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 — setting up there is one. Others are list and map,and will reference different values.
  • for_each — The ieterative variable
  • Not to be confused with the ieterator argument, which is option, and sets the name of a temp var. Since it’s not here, it’s defaulting to setting up there.
  • setting has two attributes;
  • keymaps the key or lists element index for the current element. It will be identical to valueif the expression has the set value.
  • value is the value of the current element.
  • Nested content defines the body of each generated block. It doesn’t seem to match the variables in the resource block.

We can’t generate meta-argument blocks like lifecycle or provisioner.

References:

Using Dynamic Blocks in Terraform — Ned In The Cloud

Terraform docs

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response