Terraform Field Guide · Chapter 2 of 12
Providers, resources and your first apply
Write a real configuration and understand every line of it: what a provider is, the anatomy of a resource, what init actually downloads, how one resource feeds another, and how to read a plan without guessing.
- 40 min
- Beginner
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Terraform in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
Providers: how Terraform talks to a platform
Terraform itself knows nothing about AWS. The terraform binary, called core, reads your files, works out the order of operations and keeps state. Everything platform-specific lives in a provider: a separate plugin that turns a resource block into API calls.
There are providers for AWS, Azure, Google Cloud, Cloudflare, GitHub, Datadog, Kubernetes and thousands more, all listed in the Terraform Registry. One configuration can use several at once: create a server on AWS and its DNS record on Cloudflare in the same apply.
terraform {
required_version = ">= 1.10"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "eu-west-1"
default_tags {
tags = { managed_by = "terraform" }
}
}- The oldest Terraform this code is allowed to run on. Stops someone on an old version from writing state in an older format.
- Where to download it: the namespace and name in the Registry.
- Any 6.x. A new major version can rename or remove arguments, so you upgrade to it on purpose, not by accident.
- Provider configuration. Credentials do not go here: the AWS provider reads them from your environment or ~/.aws, like the AWS CLI does.
- Tags added to every AWS resource this provider creates. Handy for finding everything Terraform owns.
Anatomy of a resource
A resource is one thing Terraform manages: a bucket, an instance, a DNS record, a GitHub repository. Here are two, where the second depends on the first:
resource "aws_s3_bucket" "logs" {
bucket_prefix = "acme-logs-"
}
resource "aws_s3_bucket_versioning" "logs" {
bucket = aws_s3_bucket.logs.id
versioning_configuration {
status = "Enabled"
}
}- The type comes from the provider (aws_ prefix). "logs" is your local name. Together they form the address aws_s3_bucket.logs, unique in this configuration.
- An argument: something you set. AWS adds a unique suffix, so the real name is only known after creation.
- A reference to an attribute of another resource. It passes the real bucket name in, and it tells Terraform to create the bucket first.
- A nested block. The provider docs list which blocks and arguments each resource accepts.
Two words to keep straight. Arguments are what you set in the block. Attributes are what the resource exposes after it exists, such as id and arn. Every attribute is listed under "Attribute Reference" on the resource's page in the Registry, and that page is the one you will have open most.
What init does
terraform init prepares a folder. It reads required_providers, downloads each provider into a hidden .terraform/ folder, sets up the backend where state will live (next chapter), and writes .terraform.lock.hcl.
| File | What it is | In Git? |
|---|---|---|
*.tf | Your configuration | Yes |
.terraform.lock.hcl | Exact provider versions and checksums | Yes |
.terraform/ | Downloaded provider binaries and modules | No |
terraform.tfstate | State, if it is local | No. Never. Chapter 3 explains why |
Run init once in a new folder, again after changing providers or modules, and with -upgrade when you want newer provider versions inside your constraints.
Inside an apply
Here is the configuration from above, run against a pretend AWS account. Press the buttons in order, and try plan before init first. Watch two things: which API calls the provider makes, and when (known after apply) turns into a real value.
Interactive · Inside an apply
Run each command and watch what the provider does
main.tf
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 6.0" } } } provider "aws" { region = "eu-west-1" } resource "aws_s3_bucket" "logs" { bucket_prefix = "acme-logs-" } resource "aws_s3_bucket_versioning" "logs" { bucket = aws_s3_bucket.logs.id versioning_configuration { status = "Enabled" } }
Attributes as Terraform knows them
API calls made by the AWS provider, simplified
The reference did two jobs. It carried a value that did not exist until AWS made it, and it set the order: versioning had to wait for the bucket. You never wrote "create the bucket first". Chapter 5 shows how Terraform builds that order for hundreds of resources.
Reading a plan
A plan is the most important thing Terraform prints. Learn the six symbols and you can read any of them:
| Symbol | Meaning | How worried to be |
|---|---|---|
+ | Create | Check the count is what you expected |
~ | Update in place | Usually safe. Read which attributes change |
-/+ | Destroy, then create a replacement | Stop. Downtime, and data loss for anything stateful |
+/- | Create the replacement, then destroy the old one | Safer than -/+, but still a new resource with a new ID |
- | Destroy | Stop and confirm it is meant |
<= | Read a data source | Harmless: it only reads |
Inside a replacement, look for # forces replacement next to an attribute. That is the one argument the platform cannot change on an existing resource, such as a bucket's name or a database's engine. The last line, Plan: 2 to add, 1 to change, 0 to destroy., is the quickest sanity check: if you changed one tag and it says 1 to destroy, something is wrong.
Hands-on: two providers, one apply
Still no cloud account needed. The random provider generates a value, and the local provider writes it into a file, so you can see a reference and (known after apply) for free.
terraform {
required_providers {
local = { source = "hashicorp/local", version = "~> 2.5" }
random = { source = "hashicorp/random", version = "~> 3.6" }
}
}
resource "random_pet" "server" {
length = 2
}
resource "local_file" "inventory" {
filename = "${path.module}/inventory.txt"
content = "web server: ${random_pet.server.id}\n"
}- A second provider. init downloads both.
- Generates a name like "proud-gecko" once, then keeps it in state. It only changes if you replace it.
- A reference inside a string. It makes local_file wait for random_pet.
terraform init
terraform plan
terraform apply -auto-approve
cat inventory.txt
# local_file.inventory will be created
+ resource "local_file" "inventory" {
+ content = (known after apply)
+ filename = "./inventory.txt"
...
}
# random_pet.server will be created
+ resource "random_pet" "server" {
+ id = (known after apply)
+ length = 2
+ separator = "-"
}
Plan: 2 to add, 0 to change, 0 to destroy.
random_pet.server: Creating...
random_pet.server: Creation complete after 0s [id=proud-gecko]
local_file.inventory: Creating...
local_file.inventory: Creation complete after 0s [id=2be95051e8d5...]
web server: proud-geckocontent = (known after apply)- The file's content depends on a name that does not exist yet, so the plan cannot show it.
-auto-approve- Skips the yes prompt. Fine for a throwaway lab; in real work, read the plan first.
Creation complete- random_pet finishes before local_file starts. That is the reference setting the order.
Run terraform apply again: No changes. The name is in state, so it stays the same until you ask for a new one.
terraform apply -replace=random_pet.server
# local_file.inventory must be replaced
-/+ resource "local_file" "inventory" {
~ content = <<-EOT
web server: proud-gecko
EOT -> (known after apply) # forces replacement
...
}
# random_pet.server will be replaced, as requested
-/+ resource "random_pet" "server" {
~ id = "proud-gecko" -> (known after apply)
# (2 unchanged attributes hidden)
}
Plan: 2 to add, 0 to change, 2 to destroy.-replace- Forces one resource to be recreated, for example a server you suspect is broken. Everything that references it follows.
# forces replacement- The local provider cannot edit a file in place, so a new name means a new file.
Have an AWS account? The same loop with a real bucket
An empty S3 bucket costs nothing. Configure credentials first (aws configure or aws sso login), then in a new folder:
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
provider "aws" {
region = "eu-west-1"
}
resource "aws_s3_bucket" "practice" {
bucket_prefix = "tf-practice-"
force_destroy = true
}
output "bucket" {
value = aws_s3_bucket.practice.bucket
}- Lets destroy delete the bucket even if it has objects in it. Good for labs, dangerous for real data.
- Prints the generated name after apply. Chapter 4 covers outputs.
terraform init
terraform apply
terraform destroy
Open the S3 console between apply and destroy: the bucket is there, with a tf-practice- name. Then destroy it.
Mini mission
Add a third resource that depends on both
Add a random_integer resource that picks a port between 8000 and 8999, and change the file so it reads web server: proud-gecko on port 8123. Before you apply, predict the plan: how many to add, change and destroy?
Reveal one safe solution
resource "random_integer" "port" {
min = 8000
max = 8999
}
resource "local_file" "inventory" {
filename = "${path.module}/inventory.txt"
content = "web server: ${random_pet.server.id} on port ${random_integer.port.result}\n"
}The plan is 2 to add, 0 to change, 1 to destroy: one new random_integer, and the file is replaced because its content changes. random_pet is untouched, so the name stays the same. The result attribute is listed on the random_integer page in the Registry.
Check yourself
Quiz
0 of 5 answered