📖 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 443
Create an AWS security group resource named example_sg
Use a dynamic block named ingress inside the security group resource
Each 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
1
Create the allowed_ports variable
Create a Terraform variable called allowed_ports of type list(number) with the exact values [22, 80, 443].
Terraform
Hint
Use variable "allowed_ports" with type = list(number) and set default to [22, 80, 443].
2
Create the AWS security group resource
Create a Terraform resource of type aws_security_group named example_sg with a name attribute set to "example-sg".
Terraform
Hint
Use resource "aws_security_group" "example_sg" and set name = "example-sg".
3
Add the dynamic block for ingress rules
Inside the aws_security_group.example_sg resource, add a dynamic block named ingress that iterates over var.allowed_ports. For each port, create an ingress rule with from_port and to_port set to the port number, protocol set to "tcp", and cidr_blocks set to ["0.0.0.0/0"].
Terraform
Hint
Use dynamic "ingress" with for_each = var.allowed_ports. Inside content, set from_port and to_port to ingress.value.
4
Complete the security group with description
Add a description attribute to the aws_security_group.example_sg resource with the exact value "Example security group with dynamic ingress rules".
Terraform
Hint
Add description = "Example security group with dynamic ingress rules" inside the resource block.
Practice
(1/5)
1. What is the main purpose of a dynamic block in Terraform?
easy
A. To generate multiple nested blocks from a list or map dynamically
B. To create a new Terraform provider
C. To define variables with default values
D. To execute shell commands during deployment
Solution
Step 1: Understand the role of dynamic blocks
Dynamic blocks allow repeating nested blocks based on a collection like a list or map.
Step 2: Compare with other options
Options A, B, and D describe unrelated Terraform features.
Final Answer:
To generate multiple nested blocks from a list or map dynamically -> Option A
Quick Check:
Dynamic block = repeated nested blocks [OK]
Hint: Dynamic blocks repeat nested blocks from lists/maps [OK]
Common Mistakes:
Confusing dynamic blocks with variable declarations
Thinking dynamic blocks create providers
Assuming dynamic blocks run commands
2. Which of the following is the correct syntax to start a dynamic block named ingress in Terraform?
A. The content block should use braces, not an assignment
B. The variable egress is undefined inside the block
C. Missing quotes around "egress" in dynamic block declaration
D. for_each cannot be used with variables
Solution
Step 1: Check dynamic block content syntax
The content must be a block, not an assignment. It should be content { ... }, not content = { ... }.
Step 2: Verify other parts
Variable egress is valid as the iterator, quotes are present, and for_each can use variables.
Final Answer:
The content block should use braces, not an assignment -> Option A
Quick Check:
content must be a block, not an equals assignment [OK]
Hint: Use content { ... } block, not content = { ... } [OK]
Common Mistakes:
Writing content as an assignment instead of a block
Assuming iterator variable is undefined
Forgetting quotes around dynamic block name
5. You want to create multiple network_interface blocks dynamically in an AWS instance resource using a list of maps with keys device_index and subnet_id. Which dynamic block correctly achieves this?