📖 Scenario: You are managing cloud resources using Terraform. You want to understand how meta-arguments like count and depends_on control how resources are created and managed.
🎯 Goal: Build a Terraform configuration that creates multiple instances of a resource using count and controls the creation order using depends_on.
📋 What You'll Learn
Create a resource block with a count meta-argument
Add a depends_on meta-argument to control resource creation order
Use exact resource names and meta-argument syntax
Demonstrate how meta-arguments affect resource behavior
💡 Why This Matters
🌍 Real World
Cloud engineers use meta-arguments in Terraform to efficiently manage multiple resources and their dependencies in real cloud environments.
💼 Career
Understanding meta-arguments is essential for infrastructure as code roles to write clear, maintainable, and predictable Terraform configurations.
Progress0 / 4 steps
1
Create a resource with exact name and properties
Create a Terraform resource block named aws_instance with the resource name example. Set the ami attribute to "ami-12345678" and the instance_type attribute to "t2.micro".
Terraform
Hint
Use the resource block with the exact resource type and name. Set the ami and instance_type attributes exactly as shown.
2
Add a count meta-argument to create multiple instances
Add the meta-argument count to the aws_instance.example resource block and set it to 3 to create three instances.
Terraform
Hint
Add count = 3 inside the resource block to create three copies of the resource.
3
Create a second resource that depends on the first
Create a second resource block named aws_security_group with the resource name example_sg. Set the name attribute to "example-sg". Add a depends_on meta-argument that depends on aws_instance.example.
Terraform
Hint
Use depends_on with a list containing aws_instance.example to ensure the security group is created after the instances.
4
Add a tag to the instances to complete the configuration
Add a tags attribute to the aws_instance.example resource block. Set it to a map with the key "Name" and the value "ExampleInstance".
Terraform
Hint
Add a tags block with the key Name and value ExampleInstance inside the aws_instance.example resource.
Practice
(1/5)
1. What is the main purpose of Terraform meta-arguments like count and depends_on?
easy
A. To control how and when resources are created or changed
B. To define the cloud provider credentials
C. To write the main configuration of a resource
D. To specify the output values of a module
Solution
Step 1: Understand meta-arguments role
Meta-arguments like count and depends_on control resource creation behavior, not resource content.
Step 2: Differentiate from other config parts
Provider credentials and outputs are separate from meta-arguments which manage resource lifecycle and dependencies.
Final Answer:
To control how and when resources are created or changed -> Option A
Quick Check:
Meta-arguments control resource behavior = C [OK]
Hint: Meta-arguments manage resource timing and count [OK]
Common Mistakes:
Confusing meta-arguments with provider settings
Thinking meta-arguments define resource content
Mixing outputs with meta-arguments
2. Which of the following is the correct syntax to use the count meta-argument in a resource block?
easy
A. count: 3
B. count(3)
C. count => 3
D. count = 3
Solution
Step 1: Recall Terraform attribute syntax
Terraform uses key = value syntax inside blocks for attributes and meta-arguments.
Step 2: Check each option
Only count = 3 matches correct Terraform syntax; others use invalid symbols or functions.
Final Answer:
count = 3 -> Option D
Quick Check:
Meta-argument syntax uses equals sign = [OK]
Hint: Use equals sign for meta-arguments like count = 3 [OK]
5. You want to create multiple AWS IAM users from a list, but only for those names that start with "dev". Which meta-argument combination best controls this behavior?
hard
A. Use count with a conditional expression filtering names starting with "dev"
B. Use for_each with a filtered map of users starting with "dev"
C. Use depends_on to wait for user list creation
D. Use lifecycle with ignore_changes on user names
Solution
Step 1: Understand filtering with meta-arguments
for_each allows iterating over a map or set, so filtering the list first and passing it to for_each creates resources only for desired items.
Step 2: Why not count or depends_on
count with condition is less flexible and can cause index issues; depends_on controls order, not filtering; lifecycle controls resource changes, not creation filtering.
Final Answer:
Use for_each with a filtered map of users starting with "dev" -> Option B
Quick Check:
for_each with filtered map controls selective resource creation [OK]
Hint: Use for_each with filtered map for selective resources [OK]
Common Mistakes:
Using count with condition causing resource indexing errors
Misusing depends_on for filtering
Confusing lifecycle with creation control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.