Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Meta-arguments overview
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to learn how to use meta-arguments to control resource behavior.
🎯 Goal: Build a Terraform configuration that uses meta-arguments count, depends_on, and lifecycle to manage resource creation and dependencies.
📋 What You'll Learn
Create a resource with count meta-argument
Add a depends_on meta-argument to a resource
Use the lifecycle meta-argument with prevent_destroy
💡 Why This Matters
🌍 Real World
Meta-arguments help manage complex cloud infrastructure by controlling how and when resources are created, updated, or destroyed.
💼 Career
Understanding meta-arguments is essential for cloud engineers and DevOps professionals to write safe and efficient Terraform code.
Progress0 / 4 steps
1
Create an AWS S3 bucket resource
Create a resource block called aws_s3_bucket with the name example_bucket and set the bucket attribute to "my-example-bucket-12345".
Terraform
Hint
Use the resource keyword, specify the resource type aws_s3_bucket, and give it the name example_bucket. Set the bucket attribute inside the block.
2
Add a count meta-argument to create two buckets
Add the meta-argument count with the value 2 to the aws_s3_bucket.example_bucket resource to create two buckets.
Terraform
Hint
Add count = 2 inside the resource block to create two instances.
3
Create a second resource with depends_on
Create a resource block called aws_s3_bucket_policy named example_policy. Use the depends_on meta-argument to depend on aws_s3_bucket.example_bucket. Set the bucket attribute to aws_s3_bucket.example_bucket[0].id and policy to an empty JSON string "{}".
Terraform
Hint
Use depends_on to make sure the policy is created after the bucket. Use the first bucket's ID for the bucket attribute.
4
Add lifecycle meta-argument with prevent_destroy
Add a lifecycle block inside the aws_s3_bucket.example_bucket resource. Inside it, set prevent_destroy = true to prevent accidental deletion.
Terraform
Hint
Inside the resource block, add a lifecycle block with prevent_destroy = true.
Practice
(1/5)
1. Which meta-argument in Terraform allows you to create multiple instances of a resource easily using an integer?
easy
A. count
B. depends_on
C. lifecycle
D. for_each
Solution
Step 1: Understand the purpose of count
The count meta-argument lets you specify how many copies of a resource to create.
Step 2: Compare with other meta-arguments
depends_on manages dependencies, lifecycle controls resource behavior, and for_each creates multiple instances but with keys, not just count.
Final Answer:
count -> Option A
Quick Check:
Multiple instances = count [OK]
Hint: Use count to repeat resources by number [OK]
Common Mistakes:
Confusing count with depends_on
Thinking lifecycle creates multiple resources
Mixing count and for_each without understanding difference
2. Which of the following is the correct syntax to use the depends_on meta-argument in a resource block?
easy
A. depends_on = ["aws_instance.example"]
B. depends_on = aws_instance.example
C. depends_on = [aws_instance.example]
D. depends_on = [aws_instance.example.id]
Solution
Step 1: Recall depends_on syntax
depends_on expects a list of resource references without quotes.
Step 2: Analyze options
depends_on = [aws_instance.example] uses a list with resource reference correctly. depends_on = aws_instance.example misses list brackets. depends_on = ["aws_instance.example"] uses quotes making it a string, not a reference. depends_on = [aws_instance.example.id] uses an attribute which is invalid here.
Final Answer:
depends_on = [aws_instance.example] -> Option C
Quick Check:
List of resource references = depends_on = [aws_instance.example] [OK]
Hint: Always use square brackets with resource references for depends_on [OK]
Common Mistakes:
Omitting brackets for depends_on list
Using quotes around resource names
Referencing resource attributes instead of resource itself
for_each requires a map or set of strings, not a list.
Step 2: Analyze provided value
The value is a list ["a", "b"], which is invalid for for_each. It should be a set or map.
Final Answer:
for_each must be a map or set, not a list -> Option D
Quick Check:
for_each needs map/set, not list [OK]
Hint: Use map or set for for_each, not list [OK]
Common Mistakes:
Using list instead of set/map for for_each
Misunderstanding each.value usage
Confusing count and for_each requirements
5. You want to create multiple AWS EC2 instances with different names and ensure one instance is always created before the others. Which meta-arguments combination should you use?
hard
A. Use for_each with a map of names and depends_on to order creation
B. Use count with a number and lifecycle to order creation
C. Use depends_on only, no count or for_each
D. Use lifecycle with create_before_destroy only
Solution
Step 1: Choose meta-argument for multiple named instances
for_each with a map lets you create instances with specific names as keys.
Step 2: Ensure creation order
depends_on lets you specify that one resource must be created before others.
Final Answer:
Use for_each with a map of names and depends_on to order creation -> Option A
Quick Check:
Multiple named + order = for_each + depends_on [OK]
Hint: Combine for_each and depends_on for named ordered resources [OK]