Given the Terraform configuration below, which option correctly describes the order in which Terraform resolves the data sources?
data "aws_vpc" "main" { filter { name = "tag:Name" values = ["main-vpc"] } } data "aws_subnet" "selected" { filter { name = "vpc-id" values = [data.aws_vpc.main.id] } filter { name = "tag:Environment" values = ["prod"] } }
Think about which data source provides input to the other.
The aws_subnet.selected data source uses the VPC ID from aws_vpc.main, so Terraform must resolve aws_vpc.main first to get the ID before it can resolve aws_subnet.selected.
What happens if a Terraform data source references an attribute from another data source that is not declared or does not exist?
data "aws_subnet" "example" { filter { name = "vpc-id" values = [data.aws_vpc.missing.id] } }
Consider what happens when Terraform cannot find a referenced resource or data source.
Terraform requires all referenced data sources to be declared and valid. If a data source references a missing or undeclared data source, Terraform throws an error during plan or apply.
You have a Terraform configuration that needs to fetch VPC IDs from multiple AWS regions dynamically using data sources. Which approach ensures correct dependency resolution and avoids race conditions?
Think about how Terraform handles dynamic blocks and implicit dependencies.
Using for_each with a local variable list allows Terraform to create data sources per region dynamically. Terraform's implicit dependency graph manages resolution order without manual depends_on, avoiding race conditions.
Which of the following is a security risk when using data source dependencies in Terraform configurations that fetch sensitive information?
Consider where Terraform stores data source results and how sensitive data is handled.
Terraform stores all data source results in its state file. If sensitive data is fetched and stored unencrypted, it risks exposure. Public info or referencing across modules is not a security risk by itself.
In a large Terraform project with many data sources depending on each other, what is the best practice to optimize plan and apply times while ensuring correct dependency resolution?
Think about modular design and Terraform's dependency management.
Grouping related data sources into modules with clear inputs and outputs helps Terraform manage dependencies efficiently. Explicit depends_on everywhere can slow down plans. Hardcoding IDs reduces flexibility and is error-prone.