Write your first Terraform configuration
Learn the init, plan, apply and destroy cycle on your own computer, with no cloud account needed.
· Updated
Terraform creates and changes infrastructure from files you write, so servers, networks and DNS records are described in code and reviewed like code. This guide uses the local provider, which only writes a file on your computer, so you can learn the cycle without a cloud account.
Before you start
- Terraform installed (see Set up your DevOps workstation).
- An empty folder to work in.
Steps
Save this as
main.tfin the empty folder:terraform { required_providers { local = { source = "hashicorp/local" version = "~> 2.5" } } } resource "local_file" "hello" { filename = "${path.module}/hello.txt" content = "Hello from Terraform\n" }Download the provider:
terraform initPreview the change. Terraform shows
1 to addand changes nothing yet:terraform planMake the change. Type
yeswhen asked:terraform applyCheck the result:
hello.txtnow exists, with the text frommain.tf.Change the
contentline, then runterraform planagain. Terraform shows exactly what will change before you apply it.Remove everything Terraform created:
terraform destroy
What happens next
Terraform recorded what it made in terraform.tfstate. That file is how it knows what exists, so on a real project it's kept in shared remote storage, never edited by hand and never committed to Git.
Warning
State files can contain passwords and keys. Add *.tfstate and *.tfstate.* to your .gitignore.