HashiCorp Terraform Associate (004) Question 120
Single answer4f Define resource dependencies in configurationA team manages AWS infrastructure with Terraform. In one module, an EC2 instance must download a bootstrap script from an S3 bucket during its first startup. The instance configuration only uses a hard-coded bucket name in its user_data, so Terraform does not detect any reference to the aws_s3_bucket.bootstrap resource. During apply, Terraform sometimes starts creating the EC2 instance before the bucket exists, causing the bootstrap process to fail. What is the best way to ensure Terraform creates the S3 bucket before the EC2 instance while keeping the configuration clear and maintainable?
- A
Add depends_on = [aws_s3_bucket.bootstrap] to the aws_instance resource
- B
Add lifecycle { create_before_destroy = true } to the aws_instance resource
- C
Run terraform apply -target=aws_s3_bucket.bootstrap first, then run a second apply for the instance
- D
Add depends_on = [aws_instance.web] to the aws_s3_bucket resource so Terraform knows they are related
Show answer and explanation
Correct answer: A
Explanation
Terraform determines resource order primarily through its dependency graph. The preferred approach is to rely on implicit dependencies created by expression references whenever possible, because they are clearer and more precise. However, when a dependency exists in real-world behavior but is not visible in resource arguments, such as a startup script using a hard-coded bucket name, Terraform cannot infer that relationship. In those cases, explicit dependencies with depends_on are the correct solution. This aligns with Terraform documentation and best practices: use expression references for implicit dependencies when possible, and use depends_on only for hidden dependencies that Terraform cannot automatically detect.
- A. Correct.
Correct. Terraform automatically builds dependencies when one resource references another resource's attributes. In this scenario, the instance uses only a literal bucket name, so there is no implicit dependency. Adding an explicit depends_on on aws_instance.web for aws_s3_bucket.bootstrap tells Terraform to create the bucket first. This is the intended mechanism when a real dependency exists but cannot be expressed through an argument reference.
- B. Incorrect.
Incorrect. create_before_destroy changes replacement behavior for a resource that must be recreated, helping reduce downtime during updates. It does not define ordering between unrelated resource creations in the initial apply and will not ensure the S3 bucket is created before the EC2 instance.
- C. Incorrect.
Incorrect. Using -target can force a partial apply, but it is intended for exceptional situations such as recovery or troubleshooting, not for routine dependency management. Relying on manual targeted applies makes the workflow less maintainable and can lead to incomplete or misleading plans. The proper fix is to model the dependency in configuration.
- D. Incorrect.
Incorrect. This reverses the dependency direction. If the requirement is that the instance must wait for the bucket, the instance should depend on the bucket, not the other way around. Making the bucket depend on the instance would tell Terraform to create the instance first, which is the opposite of the desired behavior.