Terraform Dynamic Block Syntax
📖 Scenario: You are setting up a Terraform configuration to create an AWS security group. You want to allow multiple ingress rules, but you want to use a dynamic block to define these rules based on a list of ports.
🎯 Goal: Build a Terraform configuration that uses a dynamic block to create multiple ingress rules inside an AWS security group resource.
📋 What You'll Learn
Create a variable list of ports called
allowed_ports with values 22, 80, and 443Create an AWS security group resource named
example_sgUse a
dynamic block named ingress inside the security group resourceEach ingress rule should allow TCP traffic on the ports from
allowed_ports from any IP💡 Why This Matters
🌍 Real World
Dynamic blocks help manage multiple similar resource blocks efficiently, such as multiple firewall rules, without repeating code.
💼 Career
Understanding dynamic blocks is essential for Terraform users to write clean, scalable infrastructure as code, a key skill for cloud engineers and DevOps professionals.
Progress0 / 4 steps