{"id":32761,"date":"2026-11-29T22:05:40","date_gmt":"2026-11-29T22:05:40","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32761"},"modified":"2026-11-29T22:05:40","modified_gmt":"2026-11-29T22:05:40","slug":"terraform-dynamaic-block-details-explaination-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-dynamaic-block-details-explaination-with-example\/","title":{"rendered":"Terraform dynamaic block details explaination with example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A Terraform <code>dynamic<\/code> block generates repeated <strong>nested blocks<\/strong> inside a resource, data source, provider or provisioner, such as <code>ingress<\/code> rules in a security group or <code>rule<\/code> blocks in an S3 lifecycle configuration. It does not create multiple resources. That is what <code>count<\/code> and <code>for_each<\/code> on the resource are for. This page fixes the invalid example above and walks through working, tested patterns: the dynamic block syntax, custom iterators, nested and conditional blocks, and common errors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform dynamic blocks are a way to dynamically generate Terraform configuration code based on the values of variables or maps. Dynamic blocks enable you to generate Terraform resources based on input values and make your configurations more flexible, reusable, and easier to manage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example to illustrate the use of dynamic blocks:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say you have a variable called <code>count<\/code> that defines the number of AWS S3 buckets that you want to create. The code below shows how you can use a dynamic block to generate the required number of S3 bucket resources based on the value of the <code>count<\/code> variable:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">variable <span class=\"hljs-string\">\"count\"<\/span> {\r\n  type = number\r\n}\r\n\r\nresource <span class=\"hljs-string\">\"aws_s3_bucket\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\r\n  count = <span class=\"hljs-keyword\">var<\/span>.count\r\n\r\n  for_each = toset(<span class=\"hljs-keyword\">var<\/span>.count)\r\n  \r\n  dynamic <span class=\"hljs-string\">\"bucket\"<\/span> {\r\n    for_each = <span class=\"hljs-keyword\">var<\/span>.count\r\n\r\n    content {\r\n      bucket = <span class=\"hljs-string\">\"bucket-${count.index}\"<\/span>\r\n    }\r\n  }\r\n}\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In the example above, the <code>dynamic<\/code> block is used to generate the S3 bucket resources. The <code>for_each<\/code> argument is used to iterate over the values of the <code>count<\/code> variable and create a new S3 bucket resource for each iteration. The <code>content<\/code> block inside the dynamic block defines the configuration for each S3 bucket. The <code>bucket<\/code> argument inside the content block is using the <code>count.index<\/code> expression to generate a unique name for each S3 bucket resource.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dynamic blocks allow you to generate Terraform configurations dynamically based on input values, making your configurations more flexible, reusable, and easier to manage.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why the example above does not work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The code above fails at <code>terraform validate<\/code> for several reasons. Treat it as an example of what not to do. Each problem is listed here so you can spot the same mistakes in your own code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Line in the example<\/th><th>Problem<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>variable &quot;count&quot;<\/code><\/td><td><code>count<\/code> is a reserved name. Terraform rejects variables named <code>count<\/code>, <code>for_each<\/code>, <code>source<\/code>, <code>version<\/code>, <code>providers<\/code>, <code>depends_on<\/code>, <code>lifecycle<\/code> and <code>locals<\/code>.<\/td><td>Use a name such as <code>bucket_count<\/code> or <code>bucket_names<\/code>.<\/td><\/tr><tr><td><code>count = var.count<\/code> together with <code>for_each<\/code><\/td><td><code>count<\/code> and <code>for_each<\/code> are mutually exclusive in the same resource.<\/td><td>Choose one. Use <code>for_each<\/code> when each instance has a stable key.<\/td><\/tr><tr><td><code>toset(var.count)<\/code><\/td><td><code>toset()<\/code> takes a list, not a number.<\/td><td>Pass a <code>list(string)<\/code> or <code>set(string)<\/code> variable.<\/td><\/tr><tr><td><code>dynamic &quot;bucket&quot;<\/code><\/td><td><code>bucket<\/code> is an argument of <code>aws_s3_bucket<\/code>, not a nested block, so there is nothing to generate. Dynamic blocks only produce nested blocks and never produce resources.<\/td><td>Set <code>bucket<\/code> directly and repeat the resource with <code>count<\/code> or <code>for_each<\/code>.<\/td><\/tr><tr><td><code>bucket-${count.index}<\/code><\/td><td>Inside a dynamic block you refer to the iterator (<code>&lt;label&gt;.key<\/code> and <code>&lt;label&gt;.value<\/code>). Also, S3 bucket names must be globally unique, so <code>bucket-0<\/code> is almost certainly taken already.<\/td><td>Use a unique prefix plus the key, for example <code>${var.prefix}-${each.key}<\/code>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating multiple S3 buckets correctly<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To create several buckets you repeat the <strong>resource<\/strong>, not a block inside it. With <code>for_each<\/code>, each bucket gets a stable address such as <code>aws_s3_bucket.this[&quot;logs&quot;]<\/code>. Removing one name from the set then destroys only that bucket. With <code>count<\/code>, removing an item from the middle of a list shifts the indexes of the items after it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">variable <span class=\"hljs-string\">\"bucket_prefix\"<\/span> {\n  type        = string\n  description = <span class=\"hljs-string\">\"Globally unique prefix, e.g. acme-prod\"<\/span>\n}\n\nvariable <span class=\"hljs-string\">\"bucket_names\"<\/span> {\n  type    = set(string)\n  <span class=\"hljs-keyword\">default<\/span> = &#91;<span class=\"hljs-string\">\"logs\"<\/span>, <span class=\"hljs-string\">\"artifacts\"<\/span>, <span class=\"hljs-string\">\"backups\"<\/span>]\n}\n\nresource <span class=\"hljs-string\">\"aws_s3_bucket\"<\/span> <span class=\"hljs-string\">\"this\"<\/span> {\n  for_each = <span class=\"hljs-keyword\">var<\/span>.bucket_names\n  bucket   = <span class=\"hljs-string\">\"${var.bucket_prefix}-${each.key}\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Equivalent with count (index-based addresses):<\/span>\n<span class=\"hljs-comment\"># variable \"bucket_count\" { type = number }<\/span>\n<span class=\"hljs-comment\"># resource \"aws_s3_bucket\" \"numbered\" {<\/span>\n<span class=\"hljs-comment\">#   count  = var.bucket_count<\/span>\n<span class=\"hljs-comment\">#   bucket = \"${var.bucket_prefix}-${count.index}\"<\/span>\n<span class=\"hljs-comment\"># }<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">For a full comparison of <code>count<\/code>, <code>for_each<\/code> and <code>for<\/code> expressions, see <a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-do-looping-iterations-in-terraform\/\">Terraform Loops and Iteration<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic block syntax<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">dynamic \"<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">nested_block_type<\/span>&gt;<\/span>\" {\n  for_each = <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">list,<\/span> <span class=\"hljs-attr\">set<\/span>, <span class=\"hljs-attr\">map<\/span> <span class=\"hljs-attr\">or<\/span> <span class=\"hljs-attr\">object<\/span> <span class=\"hljs-attr\">collection<\/span>&gt;<\/span>\n  iterator = <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">optional<\/span> <span class=\"hljs-attr\">name<\/span>, <span class=\"hljs-attr\">defaults<\/span> <span class=\"hljs-attr\">to<\/span> <span class=\"hljs-attr\">the<\/span> <span class=\"hljs-attr\">block<\/span> <span class=\"hljs-attr\">label<\/span>&gt;<\/span>\n  labels   = &#91;<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">optional,<\/span> <span class=\"hljs-attr\">for<\/span> <span class=\"hljs-attr\">blocks<\/span> <span class=\"hljs-attr\">that<\/span> <span class=\"hljs-attr\">take<\/span> <span class=\"hljs-attr\">labels<\/span>&gt;<\/span>]\n\n  content {\n    # arguments of ONE nested block\n    # use <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">iterator<\/span>&gt;<\/span>.key and <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">iterator<\/span>&gt;<\/span>.value\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Label<\/strong> (<code>&quot;ingress&quot;<\/code>, <code>&quot;rule&quot;<\/code>): the type of nested block to generate. It must be a block that the resource schema supports.<\/li>\n\n\n<li><strong><code>for_each<\/code><\/strong>: any collection. Unlike resource-level <code>for_each<\/code>, a dynamic block accepts a plain <strong>list of objects<\/strong>, so you do not need to convert it to a map.<\/li>\n\n\n<li><strong>Iterator<\/strong>: a temporary symbol that is named after the label unless you set <code>iterator<\/code>. <code>.key<\/code> is the map key or list index, and <code>.value<\/code> is the current element.<\/li>\n\n\n<li><strong><code>content<\/code><\/strong>: the body of each generated block. You can nest further <code>dynamic<\/code> blocks inside it.<\/li>\n\n\n<li>Dynamic blocks <strong>cannot<\/strong> generate meta-argument blocks such as <code>lifecycle<\/code> or <code>provisioner<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example: security group rules from a variable<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is the classic use case. Each rule in a variable becomes one <code>ingress<\/code> block. To add a port, you edit data rather than copying a block.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">variable <span class=\"hljs-string\">\"vpc_id\"<\/span> {\n  type = string\n}\n\nvariable <span class=\"hljs-string\">\"ingress_rules\"<\/span> {\n  type = <span class=\"hljs-keyword\">list<\/span>(object({\n    description = string\n    port        = number\n    protocol    = string\n    cidr_blocks = <span class=\"hljs-keyword\">list<\/span>(string)\n  }))\n  <span class=\"hljs-keyword\">default<\/span> = &#91;\n    { description = <span class=\"hljs-string\">\"HTTPS\"<\/span>, port = <span class=\"hljs-number\">443<\/span>, protocol = <span class=\"hljs-string\">\"tcp\"<\/span>, cidr_blocks = &#91;<span class=\"hljs-string\">\"0.0.0.0\/0\"<\/span>] },\n    { description = <span class=\"hljs-string\">\"SSH from office\"<\/span>, port = <span class=\"hljs-number\">22<\/span>, protocol = <span class=\"hljs-string\">\"tcp\"<\/span>, cidr_blocks = &#91;<span class=\"hljs-string\">\"203.0.113.0\/24\"<\/span>] },\n  ]\n}\n\nresource <span class=\"hljs-string\">\"aws_security_group\"<\/span> <span class=\"hljs-string\">\"web\"<\/span> {\n  name        = <span class=\"hljs-string\">\"web-sg\"<\/span>\n  description = <span class=\"hljs-string\">\"Web server security group\"<\/span>\n  vpc_id      = <span class=\"hljs-keyword\">var<\/span>.vpc_id\n\n  dynamic <span class=\"hljs-string\">\"ingress\"<\/span> {\n    for_each = <span class=\"hljs-keyword\">var<\/span>.ingress_rules\n    content {\n      description = ingress.value.description\n      from_port   = ingress.value.port\n      to_port     = ingress.value.port\n      protocol    = ingress.value.protocol\n      cidr_blocks = ingress.value.cidr_blocks\n    }\n  }\n\n  egress {\n    from_port   = <span class=\"hljs-number\">0<\/span>\n    to_port     = <span class=\"hljs-number\">0<\/span>\n    protocol    = <span class=\"hljs-string\">\"-1\"<\/span>\n    cidr_blocks = &#91;<span class=\"hljs-string\">\"0.0.0.0\/0\"<\/span>]\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">terraform init\nterraform validate\nterraform plan -<span class=\"hljs-keyword\">var<\/span>=<span class=\"hljs-string\">\"vpc_id=vpc-0123456789abcdef0\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The plan shows one security group with two <code>ingress<\/code> entries and ends with <code>Plan: 1 to add, 0 to change, 0 to destroy.<\/code> Add a third object to <code>ingress_rules<\/code> and the plan shows an in-place update to the same resource.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Current AWS provider versions also offer separate <code>aws_vpc_security_group_ingress_rule<\/code> resources. Do not mix inline <code>ingress<\/code> blocks and standalone rule resources on the same security group, because they will overwrite each other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nested and conditional dynamic blocks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In AWS provider v4 and later, S3 lifecycle rules live in <code>aws_s3_bucket_lifecycle_configuration<\/code>. That makes it a good place to show a custom iterator, a nested dynamic block, and the <code>condition ? [1] : []<\/code> pattern, which includes a block only when it is needed. The <code>optional()<\/code> type modifier requires Terraform 1.3 or later.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">variable <span class=\"hljs-string\">\"lifecycle_rules\"<\/span> {\n  type = <span class=\"hljs-keyword\">list<\/span>(object({\n    id              = string\n    prefix          = string\n    expiration_days = number\n    transition_days = optional(number) <span class=\"hljs-comment\"># null = no transition<\/span>\n  }))\n  <span class=\"hljs-keyword\">default<\/span> = &#91;\n    { id = <span class=\"hljs-string\">\"app-logs\"<\/span>, prefix = <span class=\"hljs-string\">\"app\/\"<\/span>, expiration_days = <span class=\"hljs-number\">365<\/span>, transition_days = <span class=\"hljs-number\">30<\/span> },\n    { id = <span class=\"hljs-string\">\"tmp\"<\/span>, prefix = <span class=\"hljs-string\">\"tmp\/\"<\/span>, expiration_days = <span class=\"hljs-number\">7<\/span> },\n  ]\n}\n\nresource <span class=\"hljs-string\">\"aws_s3_bucket_lifecycle_configuration\"<\/span> <span class=\"hljs-string\">\"logs\"<\/span> {\n  bucket = aws_s3_bucket.this&#91;<span class=\"hljs-string\">\"logs\"<\/span>].id\n\n  dynamic <span class=\"hljs-string\">\"rule\"<\/span> {\n    for_each = <span class=\"hljs-keyword\">var<\/span>.lifecycle_rules\n    iterator = lc               <span class=\"hljs-comment\"># custom iterator name<\/span>\n\n    content {\n      id     = lc.value.id\n      status = <span class=\"hljs-string\">\"Enabled\"<\/span>\n\n      filter {\n        prefix = lc.value.prefix\n      }\n\n      <span class=\"hljs-comment\"># Conditional block: zero or one transition<\/span>\n      dynamic <span class=\"hljs-string\">\"transition\"<\/span> {\n        for_each = lc.value.transition_days == <span class=\"hljs-keyword\">null<\/span> ? &#91;] : &#91;lc.value.transition_days]\n        content {\n          days          = transition.value\n          storage_class = <span class=\"hljs-string\">\"STANDARD_IA\"<\/span>\n        }\n      }\n\n      expiration {\n        days = lc.value.expiration_days\n      }\n    }\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The outer block uses the custom iterator <code>lc<\/code>, so the inner block can still use its default iterator <code>transition<\/code> without any ambiguity. The <code>tmp<\/code> rule gets no <code>transition<\/code> block at all. Custom iterators matter most when both levels would otherwise have the same name, or when the label is long.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Unsupported block type<\/td><td>The label is an argument, a misspelled block type, or a block the resource does not support<\/td><td>Check the resource&#039;s argument reference in the provider docs<\/td><\/tr><tr><td>Unsupported attribute on <code>each.value<\/code> \/ <code>count.index<\/code><\/td><td>Using resource-level symbols inside <code>content<\/code><\/td><td>Use <code>&lt;iterator&gt;.value<\/code> and <code>&lt;iterator&gt;.key<\/code><\/td><\/tr><tr><td>Invalid variable name<\/td><td>Declared a variable named <code>count<\/code>, <code>for_each<\/code>, <code>source<\/code> and so on<\/td><td>Rename the variable<\/td><\/tr><tr><td>The &quot;count&quot; and &quot;for_each&quot; meta-arguments are mutually-exclusive<\/td><td>Both set on one resource<\/td><td>Keep only one<\/td><\/tr><tr><td>Invalid function argument from <code>toset()<\/code><\/td><td>Passed a number or map<\/td><td>Pass a list, or use <code>keys()<\/code> or <code>range()<\/code><\/td><\/tr><tr><td>Too many or missing blocks<\/td><td>Provider expects at most one block (for example <code>versioning_configuration<\/code>)<\/td><td>Use the <code>cond ? [1] : []<\/code> pattern, not a list with many elements<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use dynamic blocks when the number of nested blocks really varies with input. For two fixed blocks, writing them out literally is easier to read.<\/li>\n\n\n<li>Give variables precise types (<code>list(object({...}))<\/code>) so mistakes fail at plan time instead of at the API.<\/li>\n\n\n<li>Avoid deep nesting. More than two levels usually means the data should be reshaped with a <code>for<\/code> expression in <code>locals<\/code> first.<\/li>\n\n\n<li>Prefer separate resources (for example per-rule security group resources) when you need independent lifecycle or ownership of each item.<\/li>\n\n\n<li>Run <code>terraform fmt<\/code> and <code>terraform validate<\/code> in CI or with <a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-configure-pre-commit-for-terraform-complete-devops-guide-with-15-practical-use-cases-commands-security-checks-and-best-practices\/\">pre-commit hooks<\/a>, and scan generated rules such as <code>0.0.0.0\/0<\/code> ingress with <a href=\"https:\/\/www.devopsschool.com\/blog\/terraform-tutorials-tfsec-for-security-scanning\/\">tfsec<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Can a Terraform dynamic block create multiple resources?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. A dynamic block only generates nested blocks inside a single resource. To create several resources, use <code>count<\/code> or <code>for_each<\/code> on the resource itself.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between for_each on a resource and for_each in a dynamic block?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Resource-level <code>for_each<\/code> creates multiple resource instances and requires a map or a set of strings. A dynamic block&#039;s <code>for_each<\/code> creates multiple nested blocks and accepts any collection, including a list of objects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I add a block only when a condition is true?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use a dynamic block with <code>for_each = var.enabled ? [1] : []<\/code>. An empty list produces no block, and a one-element list produces exactly one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What does the iterator argument do in a dynamic block?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It renames the temporary variable used inside <code>content<\/code>, which is otherwise named after the block label. This is useful for nested dynamic blocks or long labels.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use dynamic blocks for lifecycle or provisioner blocks?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Terraform processes meta-argument blocks such as <code>lifecycle<\/code> and <code>provisioner<\/code> before it evaluates expressions, so they cannot be generated dynamically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-do-looping-iterations-in-terraform\/\">Terraform Loops and Iteration: Complete Guide to count, for_each, for Expressions, and Dynamic Blocks<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/terraform-conditioning-example-program\/\">Terraform Conditional Expressions and Logic: Complete Guide with Examples<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/terraform-tutorials-tfsec-for-security-scanning\/\">Terraform Tutorials: TFSec for Security Scanning<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/infrastructure-as-code\/terraform\/\">More Terraform tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A Terraform dynamic block generates repeated nested blocks inside a resource, data source, provider or provisioner, such as ingress rules in a security group or rule blocks&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5129],"tags":[26224],"class_list":["post-32761","post","type-post","status-publish","format-standard","hentry","category-terraform","tag-hcl"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32761"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32761\/revisions"}],"predecessor-version":[{"id":80421,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32761\/revisions\/80421"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}