{"id":32761,"date":"2023-02-11T20:43:23","date_gmt":"2023-02-11T20:43:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32761"},"modified":"2023-02-11T20:43:25","modified_gmt":"2023-02-11T20:43:25","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>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>Here&#8217;s an example to illustrate the use of dynamic blocks:<\/p>\n\n\n\n<p>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>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>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","protected":false},"excerpt":{"rendered":"<p>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. Here&#8217;s an example to illustrate the use of dynamic blocks: Let&#8217;s say&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-32761","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_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":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32761\/revisions"}],"predecessor-version":[{"id":32762,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32761\/revisions\/32762"}],"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}]}}