Dive deep into terraform.tfstate to understand how Terraform maps your code to real-world resources and why state matters.
Set up a configuration that creates several related resources so we can explore state.
mkdir -p ~/terraform-practice/lesson-201cd ~/terraform-practice/lesson-201cat > main.tf << 'EOF'
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.4"
}
random = {
source = "hashicorp/random"
version = "~> 3.5"
}
}
}
resource "random_id" "app" {
byte_length = 4
}
resource "local_file" "config" {
filename = "${path.module}/app-${random_id.app.hex}.conf"
content = "app_id = ${random_id.app.hex}\nenvironment = development"
}
resource "local_file" "readme" {
filename = "${path.module}/README.txt"
content = "Application ID: ${random_id.app.hex}"
}
EOFThis configuration uses two providers (local and random) and creates three related resources. The random_id is used by both local_file resources, creating dependencies.
A main.tf file with providers and three interconnected resources.