📖 Scenario: You are setting up a cloud network security group to control access to your servers. You want to allow multiple ports for incoming traffic, but the list of ports might change. Using dynamic blocks in Terraform helps you manage this easily.
🎯 Goal: Create a Terraform configuration for an AWS security group that uses a dynamic block to add multiple ingress rules for specified ports.
📋 What You'll Learn
Create a variable with a list of ports
Define an AWS security group resource
Use a dynamic block to add ingress rules for each port in the list
Set the protocol to TCP and allow traffic from anywhere (0.0.0.0/0)
💡 Why This Matters
🌍 Real World
Managing security groups with dynamic blocks helps automate and simplify cloud network security configurations, especially when the allowed ports change frequently.
💼 Career
Cloud engineers and DevOps professionals often use Terraform dynamic blocks to write clean, scalable infrastructure code for security groups and other resources.
Progress0 / 4 steps
1
Create a variable with a list of ports
Create a Terraform variable called allowed_ports with the list [22, 80, 443].
Terraform
Hint
Use variable "allowed_ports" and set default to the list of ports.
2
Define an AWS security group resource
Define a resource called aws_security_group named example with name = "example-sg" and description = "Example security group".
Terraform
Hint
Use resource "aws_security_group" "example" and set name and description.
3
Add a dynamic block for ingress rules
Inside the aws_security_group.example resource, add a dynamic block named ingress that iterates over var.allowed_ports. For each port, set from_port and to_port to the port number, protocol to "tcp", and cidr_blocks to ["0.0.0.0/0"].
Terraform
Hint
Use dynamic "ingress" with for_each = var.allowed_ports and set the block content accordingly.
4
Add an egress rule to allow all outbound traffic
Inside the aws_security_group.example resource, add an egress block that allows all outbound traffic by setting from_port and to_port to 0, protocol to "-1", and cidr_blocks to ["0.0.0.0/0"].
Terraform
Hint
Add an egress block with the specified settings to allow all outbound traffic.
Practice
(1/5)
1. What is the main purpose of using dynamic blocks in Terraform security groups?
easy
A. To delete all security group rules at once
B. To manually write each security group rule one by one
C. To encrypt security group rules for security
D. To create multiple similar security group rules from a list automatically
Solution
Step 1: Understand the role of dynamic blocks
Dynamic blocks allow repeating a block of code multiple times based on a list or map input.
Step 2: Apply this to security groups
In security groups, dynamic blocks help create many similar rules without writing each rule manually.
Final Answer:
To create multiple similar security group rules from a list automatically -> Option D
Quick Check:
Dynamic blocks automate repeated blocks [OK]
Hint: Dynamic blocks repeat code for lists automatically [OK]
Common Mistakes:
Thinking dynamic blocks delete rules
Believing dynamic blocks encrypt rules
Confusing dynamic blocks with manual rule writing
2. Which of the following is the correct syntax to start a dynamic block named ingress in a Terraform security group resource?
easy
A. dynamic ingress {
B. dynamic "ingress" {
C. dynamic ingress() {
D. dynamic "ingress"() {
Solution
Step 1: Recall Terraform dynamic block syntax
Dynamic blocks use the keyword dynamic followed by the block name in quotes and curly braces.
Step 2: Match syntax for ingress block
The correct syntax is dynamic "ingress" { to define a dynamic ingress block.
Final Answer:
dynamic "ingress" { -> Option B
Quick Check:
Dynamic block name must be in quotes [OK]
Hint: Use quotes around block name in dynamic block [OK]
Common Mistakes:
Omitting quotes around block name
Adding parentheses after block name
Using incorrect keywords
3. Given the following Terraform snippet, what will be the number of ingress rules created?
Inside a dynamic block named "ingress", the loop variable is ingress, not rule.
Step 2: Correct variable reference
References should be ingress.value.from and ingress.value.to to access current item values.
Final Answer:
Incorrect reference to loop variable; should use ingress.value instead of rule.value -> Option C
Quick Check:
Loop variable matches dynamic block name [OK]
Hint: Loop variable = dynamic block name [OK]
Common Mistakes:
Using wrong loop variable name
Omitting quotes around dynamic block name
Misunderstanding for_each data types
5. You want to create a security group with dynamic ingress rules from a list of maps, but only include rules where enabled is true. Which Terraform code snippet correctly uses a dynamic block with filtering?