Home » HashiCorp » Terraform Associate » Given the Terraform configuration below, in which order will the resources be created?
Given the Terraform configuration below, in which order will the resources be created?
1. resource “aws_instance” “web_server”
2. {
3. ami = “ami-b374d5a5”
4. instance_type = “t2.micro”
5. }
6. resource “aws_eip” “web_server_ip”
7. {
8. vpc = true instance = aws_instance.web_server.id
9. }
A. aws_eip will be created first aws_instance will be created second
B. aws_eip will be created first aws_instance will be created second
C. Resources will be created simultaneously
D. aws_instance will be created first aws_eip will be created second
Correct Answer: D
Explanation/Reference:
Explanation:
Implicit and Explicit Dependencies
By studying the resource attributes used in interpolation expressions, Terraform can automatically infer when one resource depends on another. In the example above, the reference to aws_instance.web_server.id creates an implicit dependency on the aws_instance named web_server.
Terraform uses this dependency information to determine the correct order in which to create the different resources.
# Example of Implicit Dependency resource “aws_instance” “web_server” { ami = “ami-b374d5a5” instance_type = “t2.micro”
} resource “aws_eip” “web_server_ip” { vpc = true instance = aws_instance.web_server.id
}I n the example above, Terraform knows that the aws_instance must be created before the aws_eip.
Implicit dependencies via interpolation expressions are the primary way to inform Terraform about these relationships, and should be used whenever possible.
Sometimes there are dependencies between resources that are not visible to Terraform. The depends_on argument is accepted by any resource and accepts a list of resources to create explicit dependencies for.
For example, perhaps an application we will run on our EC2 instance expects to use a specific Amazon S3 bucket, but that dependency is configured inside the application code and thus not visible to Terraform. In that case, we can use depends_on to explicitly declare the dependency:
# Example of Explicit Dependency
# New resource for the S3 bucket our application will use resource “aws_s3_bucket” “example” { bucket = “terraform-getting-started-guide” acl = “private”
} # Change the aws_instance we declared earlier to now include “depends_on” resource “aws_instance” “example” { ami = “ami-2757f631” instance_type = “t2.micro” # Tells Terraform that this EC2 instance must be created only after the # S3 bucket has been created. depends_on = [aws_s3_bucket.example]
}
https://learn.hashicorp.com/terraform/getting-started/dependencies.html
Download Printable PDF. VALID exam to help you PASS.
|
|