Bird
Raised Fist0
Terraformcloud~20 mins

Depends_on for explicit dependencies in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Depends_on Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of depends_on in Terraform

What is the main purpose of using depends_on in a Terraform resource configuration?

ATo create a resource only if another resource exists in the cloud provider
BTo explicitly specify resource creation order when Terraform cannot infer it automatically
CTo define variables that are used inside resource blocks
DTo automatically delete dependent resources when a resource is destroyed
Attempts:
2 left
💡 Hint

Think about situations where Terraform might not know which resource to create first.

❓ Configuration
intermediate
2:00remaining
Identifying the effect of depends_on in resource creation

Given the following Terraform snippet, what will be the order of resource creation?

resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

resource "aws_eip" "ip" {
  instance = aws_instance.web.id
  depends_on = [aws_instance.web]
}
ATerraform will throw an error due to circular dependency
BThe <code>aws_eip.ip</code> is created first, then <code>aws_instance.web</code>
CBoth resources are created simultaneously
DThe <code>aws_instance.web</code> is created first, then <code>aws_eip.ip</code>
Attempts:
2 left
💡 Hint

Look at the depends_on and the implicit reference inside aws_eip.

❓ Architecture
advanced
2:30remaining
Using depends_on to manage complex resource dependencies

You have three resources: db, app, and monitoring. The app must be created after db, and monitoring must be created after both db and app. Which depends_on configuration correctly enforces this?

A<code>app depends_on = [db]</code> and <code>monitoring depends_on = [db, app]</code>
B<code>app depends_on = [monitoring]</code> and <code>monitoring depends_on = [db]</code>
C<code>db depends_on = [app]</code> and <code>monitoring depends_on = [app]</code>
D<code>monitoring depends_on = [db]</code> only
Attempts:
2 left
💡 Hint

Think about the order: db → app → monitoring.

❓ security
advanced
2:30remaining
Impact of missing depends_on on security group rules

You have a security group resource and an instance resource. The instance uses the security group ID. If you forget to add depends_on for the instance on the security group, what could happen?

ATerraform might try to create the instance before the security group, causing a failure
BTerraform will automatically detect the dependency and create resources in correct order
CThe security group will be deleted before the instance is created
DTerraform will ignore the security group and create the instance without it
Attempts:
2 left
💡 Hint

Consider if Terraform can always detect dependencies from resource attributes.

❓ service_behavior
expert
3:00remaining
Effect of depends_on on resource destruction order

Consider two Terraform resources: resource "aws_lb" "load_balancer" and resource "aws_lb_target_group" "target_group". The load balancer depends on the target group using depends_on. When destroying the infrastructure, what is the destruction order?

AThe load balancer is destroyed first, then the target group
BTerraform will fail to destroy due to dependency conflict
CThe target group is destroyed first, then the load balancer
DBoth are destroyed simultaneously
Attempts:
2 left
💡 Hint

Think about how depends_on affects destruction order compared to creation order.

Practice

(1/5)
1. What is the main purpose of depends_on in Terraform?
easy
A. To delete resources automatically
B. To define the resource type
C. To set resource properties
D. To explicitly specify the order in which resources are created

Solution

  1. Step 1: Understand resource creation order

    Terraform creates resources in parallel by default, but sometimes one resource must be created before another.
  2. Step 2: Role of depends_on

    The depends_on attribute explicitly tells Terraform to wait for one resource before creating another.
  3. Final Answer:

    To explicitly specify the order in which resources are created -> Option D
  4. Quick Check:

    depends_on controls creation order = A [OK]
Hint: depends_on sets creation order explicitly [OK]
Common Mistakes:
  • Confusing depends_on with resource type definition
  • Thinking depends_on sets resource properties
  • Assuming depends_on deletes resources
2. Which of the following is the correct syntax to add depends_on for a resource named aws_instance.example?
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"]

Solution

  1. Step 1: Understand depends_on syntax

    The depends_on attribute expects a list of resource references.
  2. Step 2: Correct list format

    Resources must be inside square brackets as unquoted references, like [aws_instance.example].
  3. Final Answer:

    depends_on = [aws_instance.example] -> Option C
  4. Quick Check:

    depends_on requires list of resource references = B [OK]
Hint: Use list of unquoted resource references for depends_on [OK]
Common Mistakes:
  • Omitting square brackets around resource reference
  • Using curly braces instead of square brackets
  • Not using a list format
3. Given the following Terraform snippet, what will happen during apply?
resource "aws_s3_bucket" "bucket" {
  bucket = "my-bucket"
}

resource "aws_s3_bucket_policy" "policy" {
  bucket = aws_s3_bucket.bucket.id
  policy = "..."
  depends_on = [aws_s3_bucket.bucket]
}
medium
A. The bucket will be created before the bucket policy
B. The bucket policy will be created before the bucket
C. Both resources will be created simultaneously
D. Terraform will throw a syntax error

Solution

  1. Step 1: Analyze depends_on usage

    The bucket policy resource explicitly depends on the bucket resource using depends_on, so the bucket must exist first.
  2. Step 2: Understand creation order

    Terraform will create the bucket first, then the bucket policy, ensuring the policy applies to an existing bucket.
  3. Final Answer:

    The bucket will be created before the bucket policy -> Option A
  4. Quick Check:

    depends_on enforces bucket before policy = B [OK]
Hint: depends_on enforces creation order, bucket before policy [OK]
Common Mistakes:
  • Assuming simultaneous creation despite depends_on
  • Thinking depends_on reverses order
  • Confusing resource references with strings
4. You wrote this Terraform code but get an error:
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

resource "aws_eip" "ip" {
  instance = aws_instance.web.id
  depends_on = aws_instance.web
}

What is the problem?
medium
A. depends_on should be a list of resource references
B. instance attribute is incorrect
C. Resource names cannot be used in depends_on
D. The AMI ID is invalid

Solution

  1. Step 1: Check depends_on syntax

    The depends_on attribute must be a list, even if it has one resource.
  2. Step 2: Identify error in code

    Here, depends_on = aws_instance.web is missing square brackets, causing a syntax error.
  3. Final Answer:

    depends_on should be a list of resource references -> Option A
  4. Quick Check:

    depends_on needs list syntax = A [OK]
Hint: Always use square brackets for depends_on list [OK]
Common Mistakes:
  • Using single resource without list brackets
  • Confusing depends_on with resource attributes
  • Ignoring Terraform syntax requirements
5. You have two resources: aws_db_instance.db and aws_ecs_service.ecs. The ECS service must start only after the database is fully created. How should you configure depends_on in aws_ecs_service.ecs to ensure this?
hard
A. depends_on = aws_db_instance.db
B. depends_on = [aws_db_instance.db]
C. depends_on = ["aws_db_instance.db"]
D. depends_on = {"aws_db_instance.db"}

Solution

  1. Step 1: Understand depends_on format

    Terraform requires depends_on to be a list of resource references.
  2. Step 2: Correct usage for ECS service

    To make ECS depend on the DB, use depends_on = [aws_db_instance.db] inside the ECS resource block.
  3. Final Answer:

    depends_on = [aws_db_instance.db] -> Option B
  4. Quick Check:

    depends_on list of resource references for explicit dependency = A [OK]
Hint: Use list of unquoted resource references for depends_on [OK]
Common Mistakes:
  • Omitting square brackets around resource reference
  • Omitting square brackets
  • Using curly braces instead of brackets
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard