0
0
Terraformcloud~10 mins

Availability zones data source in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Availability zones data source
Start Terraform Plan
Call data source: availability_zones
Query cloud provider for AZs
Receive list of AZs
Store AZs in Terraform state
Use AZs in resource definitions
Apply infrastructure changes
Terraform queries the cloud provider for available zones, stores them, and uses them to deploy resources in those zones.
Execution Sample
Terraform
data "aws_availability_zones" "available" {}

output "az_names" {
  value = data.aws_availability_zones.available.names
}
This code fetches the list of AWS availability zones and outputs their names.
Process Table
StepActionEvaluationResult
1Start Terraform planTerraform begins executionReady to query data sources
2Call data source aws_availability_zonesRequest AZ list from AWSAWS returns list of AZs
3Store AZ list in stateSave AZ names in Terraform stateAZ names stored: ["us-east-1a", "us-east-1b", "us-east-1c"]
4Output az_namesRead AZ names from data sourceOutput value: ["us-east-1a", "us-east-1b", "us-east-1c"]
5End planAll data sources resolvedTerraform ready to apply resources using AZs
💡 Terraform finishes data source query and stores AZs for resource use
Status Tracker
VariableStartAfter Step 2After Step 3Final
data.aws_availability_zones.available.namesundefined["us-east-1a", "us-east-1b", "us-east-1c"]["us-east-1a", "us-east-1b", "us-east-1c"]["us-east-1a", "us-east-1b", "us-east-1c"]
Key Moments - 3 Insights
Why does Terraform query availability zones before creating resources?
Terraform queries AZs first (see step 2 in execution_table) to know which zones exist so it can place resources correctly.
Can the list of availability zones change between runs?
Yes, the data source fetches live info each run (step 2), so if AWS adds or removes zones, Terraform sees the update.
What happens if the data source fails to get AZs?
Terraform plan will fail at step 2 because it cannot get required info, stopping deployment until fixed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of data.aws_availability_zones.available.names after step 3?
Anull
Bundefined
C["us-east-1a", "us-east-1b", "us-east-1c"]
D[]
💡 Hint
Check variable_tracker column 'After Step 3' for data.aws_availability_zones.available.names
At which step does Terraform receive the list of availability zones from AWS?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
See execution_table row for Step 2 describing AWS response
If AWS adds a new availability zone, how will the execution_table change?
AStep 3 will store the new AZ in the list
BStep 1 will change
CStep 4 output will remain the same
DTerraform will skip querying AZs
💡 Hint
Look at how step 3 stores AZ names in variable_tracker
Concept Snapshot
Terraform availability zones data source:
- Use data "aws_availability_zones" to fetch AZs
- Terraform queries cloud provider live each run
- AZ names stored in state for resource use
- Helps deploy resources across zones
- Output AZ names with data source attribute
Full Transcript
This visual execution shows how Terraform uses the availability zones data source. First, Terraform starts the plan and calls the data source to query AWS for available zones. AWS returns the list of zones, which Terraform stores in its state. Then Terraform outputs the zone names. This process ensures Terraform knows which zones exist before creating resources. The variable tracking shows the AZ names are undefined at start and set after the query. Key moments clarify why Terraform queries AZs first and what happens if the query fails. The quiz tests understanding of when and how AZ names are retrieved and stored.