Providers and Resources
How Terraform providers are declared, pinned and configured, how resource blocks become real objects, and how to read create, update-in-place and replace plans.
By The Dark DebuggerUpdated 25 min read
This tutorial covers the two halves every Terraform configuration is built from:
providers, the plugins that talk to real platforms, and resources, the blocks
that describe what should exist. It assumes you've worked through the
Terraform Introduction and
Variables, Locals, Outputs, and Expressions.
Most examples run locally with the random and local providers; the last part shows
the same ideas with the AWS provider, validated and planned without an AWS account.
Why providers deserve your attention
Here's a situation that plays out on teams all the time. Two engineers check out the
same commit, run terraform plan, and get different results: one sees "No changes",
the other sees several resources about to be updated in place. Same files, same
commands. The difference is underneath the code: one machine downloaded a newer
release of the provider plugin, and that release computes a default differently.
Your configuration is only half of the system. The other half is the provider, a separate program that translates your declarative blocks into API calls and evolves on its own release schedule. If you don't pin it and lock it, you're building on a moving foundation. Resources are the half you write, and reading how they change in a plan is the core skill of day-to-day Terraform work.
What a provider is
Terraform's core knows nothing about servers, buckets or DNS records. That knowledge lives in providers: separate executables that Terraform downloads, starts as child processes, and talks to over a local RPC protocol. Each provider publishes a schema (which resource types exist, which arguments they accept, which attributes they return) and implements create, read, update and delete for each type.
Providers are identified by a source address with three parts:
| Part | Example | Meaning |
|---|---|---|
| Hostname | registry.terraform.io | The registry to download from. Optional; this is the default. |
| Namespace | hashicorp | The publisher. Partners and the community publish under their own names. |
| Type | random | The provider name, and the prefix of its resource types (random_pet). |
So hashicorp/random is shorthand for registry.terraform.io/hashicorp/random. One
provider is special: terraform.io/builtin/terraform ships inside the Terraform
binary and supplies terraform_data, which is why the introduction needed no
download.
Declaring providers with required_providers
Our local example generates release metadata for the checkout service: a random
release name, an API token, a record of who owns the release, and a file holding the
token. Here is the complete main.tf:
terraform {
required_version = ">= 1.6"
required_providers {
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
local = {
source = "hashicorp/local"
version = "~> 2.5"
}
}
}
variable "service" {
type = string
default = "checkout"
}
resource "random_pet" "release" {
prefix = var.service
length = 2
}
resource "random_password" "api_token" {
length = 24
special = false
}
resource "terraform_data" "release_info" {
input = {
release = random_pet.release.id
owner = "payments-team"
}
}
resource "local_sensitive_file" "api_token" {
filename = "${path.module}/out/${random_pet.release.id}.token"
content = random_password.api_token.result
file_permission = "0600"
}
output "release_name" {
value = random_pet.release.id
}The required_providers block maps a local name (random, local) to a source
address and a version constraint. The local name is what the resource type prefixes
refer to: random_pet belongs to whichever provider you called random. You can ask
Terraform what a configuration needs before anything is installed:
terraform providersProviders required by configuration:
.
├── provider[registry.terraform.io/hashicorp/local] ~> 2.5
├── provider[terraform.io/builtin/terraform]
└── provider[registry.terraform.io/hashicorp/random] ~> 3.6The built-in provider appears because of terraform_data, even though we never
declared it.
Version constraints
Every registry provider you use should carry a version constraint. The operators:
| Constraint | Allows | Use it for |
|---|---|---|
= 3.9.1 (or 3.9.1) | Exactly that version | Rarely; the lock file already pins exact versions. |
>= 3.6 | 3.6 and anything newer, including 4.x and 5.x | Minimum versions in shared modules. |
~> 3.6 | 3.6 and newer, but below 4.0 | Root configurations. The usual choice. |
~> 3.6.0 | 3.6.0 and newer, but below 3.7 | When even minor releases need review. |
>= 3.6, < 3.9 | A range; combine with commas | Excluding a known-bad release. |
!= 3.8.0 | Anything except that version | Same, usually alongside another constraint. |
~> (the "pessimistic" operator) is the one to learn precisely: it allows the
rightmost version component to grow. ~> 3.6 lets minor and patch releases in and
blocks a new major version, which is where breaking changes go. My rule: ~> MAJOR.MINOR
in every root configuration, and a looser >= floor in reusable
modules so they don't fight the root's choice.
terraform init and the dependency lock file
init reads required_providers, picks the newest version that satisfies each
constraint, downloads it, and checks its signature:
terraform initInitializing the backend...
Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/local versions matching "~> 2.5"...
- Finding hashicorp/random versions matching "~> 3.6"...
- Installing hashicorp/local v2.9.1...
- Installed hashicorp/local v2.9.1 (signed by HashiCorp)
- Installing hashicorp/random v3.9.1...
- Installed hashicorp/random v3.9.1 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
[…]The plugins land in .terraform/providers/, which you never commit. The lock file
is different: commit it. Its first entry looks like this (hashes trimmed):
provider "registry.terraform.io/hashicorp/local" {
version = "2.9.1"
constraints = "~> 2.5"
hashes = [
"h1:qGLHCuYSus+uHNnoEL4SuJqOs5yrNOcB7gnuHoVqizo=",
"zh:25606c7a5e308144fb627f6e31611bb52ff72bb9ae2d27af39673ab1a6b3c1bf",
[…]
]
}constraints is your policy; version is the decision init made under that policy.
Once the lock file exists, every later init, on any machine, installs exactly
2.9.1 even if 2.10.0 is released tomorrow, and it verifies the download against
the recorded hashes. That's both reproducibility and a quiet supply-chain defence: a
tampered binary won't match. Moving to a newer release is a deliberate act,
terraform init -upgrade, which belongs in its own pull request so reviewers see the
lock file change and the plan that comes with it.
terraform version shows what this directory is actually using:
terraform versionTerraform v1.17.1
on linux_amd64
+ provider registry.terraform.io/hashicorp/local v2.9.1
+ provider registry.terraform.io/hashicorp/random v3.9.1Resources: addresses, arguments and attributes
A resource block has a type (random_pet), a name you choose (release) and
a body of arguments. Together, type and name form the resource's address,
random_pet.release, which is how everything refers to it: other resources, outputs,
the state file, and commands like terraform state show. Names must be unique per
type within a module, and they never leave your configuration.
The plan shows the difference between what you set and what the provider will tell you:
terraform plan[…]
# random_pet.release will be created
+ resource "random_pet" "release" {
+ id = (known after apply)
+ length = 2
+ prefix = "checkout"
+ separator = "-"
}
# terraform_data.release_info will be created
+ resource "terraform_data" "release_info" {
+ id = (known after apply)
+ input = {
+ owner = "payments-team"
+ release = (known after apply)
}
+ output = (known after apply)
}
Plan: 4 to add, 0 to change, 0 to destroy.
[…]- Arguments are inputs:
lengthandprefixcame from our block.separatoris an argument too; we didn't set it, so the provider's default"-"appears. - Attributes are outputs the provider computes:
iddoesn't exist until the resource is created, hence(known after apply).
You read attributes with TYPE.NAME.ATTRIBUTE: random_pet.release.id,
random_password.api_token.result. The provider's documentation on the Terraform
Registry lists both for every resource type, under "Argument Reference" and
"Attribute Reference". Keep it open while you write.
References create the dependency graph
Look at what referenced what: release_info and the token file both use
random_pet.release.id, and the file also uses random_password.api_token.result.
Terraform reads those references and builds a dependency graph, which decides the
order of operations no matter how the blocks are arranged in the file:
terraform apply -auto-approverandom_pet.release: Creating...
random_password.api_token: Creating...
random_pet.release: Creation complete after 0s [id=checkout-optimum-orca]
terraform_data.release_info: Creating...
terraform_data.release_info: Creation complete after 0s [id=efe675b3-074d-b28a-2164-179db5b4dcdf]
random_password.api_token: Creation complete after 1s [id=none]
local_sensitive_file.api_token: Creating...
local_sensitive_file.api_token: Creation complete after 0s [id=cce8be6d8a7e15b172ac682abc8994aff61dd5ed]
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
Outputs:
release_name = "checkout-optimum-orca"The two random_* resources have no dependencies, so Terraform started them in
parallel. release_info started as soon as the pet name existed, without waiting for
the password. The token file waited for both. You never wrote an ordering; the
references are the ordering. (The password's id is the literal string none;
the random provider doesn't give passwords a meaningful ID, which is a nice reminder
that attribute values are whatever the provider decides.)
When a dependency is real but no reference expresses it, there's depends_on. Treat
it as a last resort; Data Sources, Dependencies, and Lifecycle Rules
explains why, and how to see the graph with terraform graph.
Reading plans: update in place versus replace
Every change to an existing resource is one of two kinds, and the plan tells you
which. I changed owner in release_info from "payments-team" to
"checkout-team":
terraform plan[…]
# terraform_data.release_info will be updated in-place
~ resource "terraform_data" "release_info" {
id = "efe675b3-074d-b28a-2164-179db5b4dcdf"
~ input = {
~ owner = "payments-team" -> "checkout-team"
# (1 unchanged attribute hidden)
}
~ output = {
- owner = "payments-team"
- release = "checkout-optimum-orca"
} -> (known after apply)
}
Plan: 0 to add, 1 to change, 0 to destroy.~ means the provider can modify the existing object: the id stays the same. Next
I also raised the password length from 24 to 32:
[…]
# local_sensitive_file.api_token must be replaced
-/+ resource "local_sensitive_file" "api_token" {
~ content = (sensitive value) # forces replacement
[…]
# random_password.api_token must be replaced
-/+ resource "random_password" "api_token" {
~ bcrypt_hash = (sensitive value)
~ id = "none" -> (known after apply)
~ length = 24 -> 32 # forces replacement
~ result = (sensitive value)
# (9 unchanged attributes hidden)
}
# terraform_data.release_info will be updated in-place
[…]
Plan: 2 to add, 1 to change, 2 to destroy.-/+ means replace: destroy the existing object and create a new one. The
# forces replacement comment points at the argument responsible. Whether an
argument can change in place is decided by the provider, per argument, and it
usually mirrors the real API: you can retag an EC2 instance, but you can't change its
AMI; you can add tags to an S3 bucket, but renaming it means a new bucket. Replacement
also ripples outwards. The token file never changed in our configuration, but its
content comes from the password, and a new password means a new file.
The apply output shows the default order for a replacement, destroy first, then create, with dependents destroyed before the things they depend on:
terraform_data.release_info: Modifying... [id=efe675b3-074d-b28a-2164-179db5b4dcdf]
terraform_data.release_info: Modifications complete after 0s [id=efe675b3-074d-b28a-2164-179db5b4dcdf]
local_sensitive_file.api_token: Destroying... [id=cce8be6d8a7e15b172ac682abc8994aff61dd5ed]
local_sensitive_file.api_token: Destruction complete after 0s
random_password.api_token: Destroying... [id=none]
random_password.api_token: Destruction complete after 0s
random_password.api_token: Creating...
random_password.api_token: Creation complete after 0s [id=none]
local_sensitive_file.api_token: Creating...
local_sensitive_file.api_token: Creation complete after 0s [id=811a1ccc3c515c134f9428cc80568572da2cbadc]
Apply complete! Resources: 2 added, 1 changed, 2 destroyed.For a token file that's harmless. For a load balancer it's an outage window, which
is what create_before_destroy exists to fix (covered in
Data Sources, Dependencies, and Lifecycle Rules).
Here's the ripple at its widest. Changing the service name changes the pet's
prefix, and everything downstream reacts:
terraform plan -var 'service=cart'[…]
# local_sensitive_file.api_token must be replaced
-/+ resource "local_sensitive_file" "api_token" {
[…]
~ filename = "./out/checkout-optimum-orca.token" -> (known after apply) # forces replacement
[…]
# random_pet.release must be replaced
-/+ resource "random_pet" "release" {
~ id = "checkout-optimum-orca" -> (known after apply)
~ prefix = "checkout" -> "cart" # forces replacement
# (2 unchanged attributes hidden)
}
# terraform_data.release_info will be updated in-place
~ resource "terraform_data" "release_info" {
[…]
Plan: 2 to add, 1 to change, 2 to destroy.A one-word change in a variable replaced two resources. On real infrastructure, this is the plan you read twice. The full set of symbols you'll meet:
| Symbol | Meaning |
|---|---|
+ | Create |
~ | Update in place; the object keeps its identity |
-/+ | Replace: destroy, then create |
+/- | Replace: create the new one first, then destroy (create_before_destroy) |
- | Destroy |
<= | Read a data source during apply |
Inspecting what Terraform manages
A few read-only commands answer "what is Terraform managing here, and with which provider?":
terraform state list
terraform state show random_pet.releaselocal_sensitive_file.api_token
random_password.api_token
random_pet.release
terraform_data.release_info
# random_pet.release:
resource "random_pet" "release" {
id = "checkout-optimum-orca"
length = 2
prefix = "checkout"
separator = "-"
}terraform show prints every resource in that format. Its -json form is what you
script against. This lists each resource with the provider that owns it:
terraform show -json | jq -r '.values.root_module.resources[] | "\(.address) \(.provider_name)"'local_sensitive_file.api_token registry.terraform.io/hashicorp/local
random_password.api_token registry.terraform.io/hashicorp/random
random_pet.release registry.terraform.io/hashicorp/random
terraform_data.release_info terraform.io/builtin/terraformThe same thing on AWS
Local providers need no configuration, so we haven't written a provider block yet.
Cloud providers do: at minimum a region, plus credentials from the environment. Here
is a realistic start for the checkout service's log storage on AWS: a bucket with
versioning and public access blocked in us-east-1, plus a disaster-recovery bucket
in eu-west-1.
terraform {
required_version = ">= 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
# Default configuration: every aws_* resource uses this unless told otherwise.
provider "aws" {
region = "us-east-1"
default_tags {
tags = {
service = "checkout"
managed_by = "terraform"
}
}
}
# Second configuration of the same provider, selected with provider = aws.dr
provider "aws" {
alias = "dr"
region = "eu-west-1"
default_tags {
tags = {
service = "checkout"
managed_by = "terraform"
}
}
}
resource "aws_s3_bucket" "logs" {
bucket = "checkout-logs-4f7c2a"
}
resource "aws_s3_bucket_versioning" "logs" {
bucket = aws_s3_bucket.logs.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_public_access_block" "logs" {
bucket = aws_s3_bucket.logs.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket" "logs_dr" {
provider = aws.dr
bucket = "checkout-logs-dr-4f7c2a"
}Everything you learned locally carries over:
- Provider configuration. The un-aliased
provider "aws"block is the default.default_tagsstamps every taggable resource it creates, so cost reports and ownership lookups work without anyone remembering to tag. - Aliases. The second block has
alias = "dr", andlogs_dropts into it withprovider = aws.dr. Use aliases whenever one configuration needs the same provider twice with different settings: another region, or another account with different credentials. - References and arguments. Versioning and the public access block are separate
resources that reference
aws_s3_bucket.logs.id, so Terraform creates the bucket first.
I ran init and validate on this configuration:
terraform init
terraform validate[…]
- Finding hashicorp/aws versions matching "~> 6.0"...
- Installing hashicorp/aws v6.72.0...
- Installed hashicorp/aws v6.72.0 (signed by HashiCorp)
[…]
Success! The configuration is valid.validate needs no credentials. plan does, because the provider configures itself
before planning. To plan without an AWS account, I added an override file:
Terraform merges any file named override.tf or ending in _override.tf into the
matching blocks of the main configuration. This one gives both provider
configurations fake keys and turns off the calls that would check them:
# mock_override.tf
# Plan-only settings so `terraform plan` works without an AWS account.
# Override files merge into the matching provider blocks in main.tf.
# Never commit this to a real project.
provider "aws" {
access_key = "mock_access_key"
secret_key = "mock_secret_key"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
}
provider "aws" {
alias = "dr"
access_key = "mock_access_key"
secret_key = "mock_secret_key"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
}Planning brand-new resources needs no API calls, so the plan works:
terraform plan -input=false[…]
# aws_s3_bucket.logs_dr will be created
+ resource "aws_s3_bucket" "logs_dr" {
+ acceleration_status = (known after apply)
+ acl = (known after apply)
+ arn = (known after apply)
+ bucket = "checkout-logs-dr-4f7c2a"
[…]
+ region = "eu-west-1"
+ request_payer = (known after apply)
+ tags_all = {
+ "managed_by" = "terraform"
+ "service" = "checkout"
}
[…]
# aws_s3_bucket_versioning.logs will be created
+ resource "aws_s3_bucket_versioning" "logs" {
+ bucket = (known after apply)
+ id = (known after apply)
+ region = "us-east-1"
+ versioning_configuration {
+ mfa_delete = (known after apply)
+ status = "Enabled"
}
}
Plan: 4 to add, 0 to change, 0 to destroy.Notice region = "eu-west-1" on the DR bucket (from the alias) and tags_all, which
is the merge of default_tags and the resource's own tags. Also notice how many
attributes are (known after apply): the ARN and the domain names only exist once
AWS has created the bucket.
Version 6 of the AWS provider added a shortcut worth knowing: most resources accept a
region argument of their own. I validated and planned this alternative to the
alias:
resource "aws_s3_bucket" "logs_west" {
region = "us-west-2"
bucket = "checkout-logs-west-4f7c2a"
}The plan showed + region = "us-west-2" for that bucket while it still used the
default provider's credentials and tags. For "same account, another region", that's
simpler than an alias. You still need an alias when the second configuration differs
in anything else, most often credentials for a different account.
Where you'll use this
- Multi-region and disaster recovery. The AWS example above is the skeleton of a real DR setup: primary resources on the default provider, standby copies on an aliased one, all in one plan so they can't drift apart.
- Teams on mixed platforms. The lock file records hashes only for the platforms
that ran
init. If developers on Apple Silicon Macs and CI on Linux share a repository, one side eventually hits a checksum mismatch. Record both up front:
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64 hashicorp/random- Fetching hashicorp/random 3.9.1 for linux_amd64...
- Retrieved hashicorp/random 3.9.1 for linux_amd64 (signed by HashiCorp)
- Fetching hashicorp/random 3.9.1 for darwin_arm64...
- Retrieved hashicorp/random 3.9.1 for darwin_arm64 (signed by HashiCorp)
- Obtained hashicorp/random checksums for linux_amd64; All checksums for this platform were already tracked in the lock file
- Obtained hashicorp/random checksums for darwin_arm64; Additional checksums for this platform are now tracked in the lock file
Success! Terraform has updated the lock file.
Review the changes in .terraform.lock.hcl and then commit to your
version control system to retain the new checksums.- Faster CI. Provider binaries are large; the AWS provider alone is tens of
megabytes, downloaded again for every fresh working directory. Setting
TF_PLUGIN_CACHE_DIRto a cached directory lets everyinitreuse one download. I used a shared cache for every run in this article;initoutput looks identical either way. - Controlled upgrades. Run
terraform init -upgradeon a branch, commit the lock file change, and let the plan in the pull request show whether the new provider release changes anything.
Common mistakes
A third-party provider without required_providers
If you use a resource type without declaring its provider, Terraform assumes the
hashicorp namespace. I tried a docker_container resource with no
required_providers block:
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider
│ hashicorp/docker: provider registry registry.terraform.io does not have a
│ provider named registry.terraform.io/hashicorp/docker
│
│ Did you intend to use kreuzwerker/docker? If so, you must specify that
│ source address in each module which requires that provider. To see which
│ modules are currently depending on hashicorp/docker, run the following
│ command:
│ terraform providers
╵The fix is to declare it: docker = { source = "kreuzwerker/docker", version = "..." }.
Declare every provider explicitly, HashiCorp's included; it also gives you somewhere
to put the version constraint.
Constraint and lock file disagree
I tightened random to ~> 3.5.0 while the lock file still said 3.9.1, then ran
terraform init:
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider
│ hashicorp/random: locked provider registry.terraform.io/hashicorp/random
│ 3.9.1 does not match configured version constraint ~> 3.5.0; must use
│ terraform init -upgrade to allow selection of new versions
│
│ To see which modules are currently depending on hashicorp/random and what
│ versions are specified, run the following command:
│ terraform providers
╵init won't silently change a locked version, even downwards. terraform init -upgrade re-selects within the new constraint. If the constraint matches nothing at
all (I tried 99.0.0), -upgrade fails with no available releases match the given constraints 99.0.0: check the provider's page on the registry for real version
numbers.
"No valid credential sources found"
With no credentials in the environment and no mock settings, the AWS provider fails
during plan, once per provider configuration:
╷
│ Error: No valid credential sources found
│
│ with provider["registry.terraform.io/hashicorp/aws"].dr,
│ on main.tf line 25, in provider "aws":
│ 25: provider "aws" {
│
│ Please see https://registry.terraform.io/providers/hashicorp/aws
│ for more information about providing credentials.
│
│ Error: failed to refresh cached credentials, no EC2 IMDS role found,
│ operation error ec2imds: GetMetadata, access disabled to EC2 IMDS via
│ client option, or "AWS_EC2_METADATA_DISABLED" environment variable
│
╵The provider looked in environment variables, the shared credentials file and the
EC2 instance metadata service, and found nothing. Supply credentials through one of
those (in CI, preferably short-lived ones from an OIDC role), never as literals in a
.tf file. The flip side matters too: if your shell does have credentials, a plain
terraform plan uses them, so check which account you're pointed at before you
apply.
Pinning the built-in provider
Copying the pinning habit to terraform_data's provider fails:
╷
│ Error: Invalid dependency on built-in provider
│
│ Cannot use terraform.io/builtin/terraform: built-in providers do not
│ support explicit version constraints.
╵The built-in provider's version is your Terraform version, so constrain that with
required_version instead. While you're cleaning up old configurations, watch for
deprecation warnings too: setting acl = "private" directly on an aws_s3_bucket
still validates on provider 6.72, but with Warning: Argument is deprecated ... acl is deprecated. Use the aws_s3_bucket_acl resource instead.
Summary
- Providers are separate plugins identified by
hostname/namespace/type; declare every one inrequired_providerswith a source and a version constraint. ~> 3.6allows 3.6 up to (not including) 4.0;~> 3.6.0allows only 3.6.x. Use~> MAJOR.MINORin root configurations.- Commit
.terraform.lock.hcl: it pins exact versions and checksums for everyone. Change versions deliberately withterraform init -upgrade, and record extra platforms withterraform providers lock -platform=.... - A resource's address is
TYPE.NAME; arguments are what you set, attributes are what the provider returns, often(known after apply). - References between resources build the dependency graph that orders operations; independent resources are created in parallel.
~updates the existing object;-/+destroys and recreates it, and# forces replacementnames the argument responsible. Replacement ripples to everything that references the replaced resource.- Provider blocks configure a provider;
aliasplusprovider = aws.NAMEgives one configuration several regions or accounts. AWS provider v6 also accepts a per-resourceregion.
Exercises
The lock file records random 3.9.1. For each constraint below, predict whether a
plain terraform init (no -upgrade) succeeds, then test by editing
required_providers: ~> 3.6.0, >= 3.0, < 4.0, ~> 4.0, ~> 3.9.
Show Solution
A loop saves the editing. It restores the original file each time, swaps in one
constraint, and keeps only the line of init output that matters:
cp main.tf main.tf.orig
for c in '~> 3.6.0' '>= 3.0, < 4.0' '~> 4.0' '~> 3.9'; do
cp main.tf.orig main.tf
sed -i "s/version = \"~> 3.6\"/version = \"$c\"/" main.tf
echo "$c"
terraform init -no-color 2>&1 | grep -E 'does not match|successfully initialized'
done
cp main.tf.orig main.tf~> 3.6.0
3.9.1 does not match configured version constraint ~> 3.6.0; must use
>= 3.0, < 4.0
Terraform has been successfully initialized!
~> 4.0
3.9.1 does not match configured version constraint ~> 4.0; must use terraform
~> 3.9
Terraform has been successfully initialized!~> 3.6.0 means 3.6.x only, so 3.9.1 is outside it;
~> 4.0 needs at least 4.0. The other two include 3.9.1, and init reuses the
locked version without downloading anything new.
Add a random_integer named port (between 8000 and 8999) and record its result in
terraform_data.release_info as port. Before applying, answer: which symbol will
each changed resource get, and which will be created or modified first?
Show Solution
resource "random_integer" "port" {
min = 8000
max = 8999
}
resource "terraform_data" "release_info" {
input = {
release = random_pet.release.id
owner = "checkout-team"
port = random_integer.port.result
}
}[…]
# random_integer.port will be created
+ resource "random_integer" "port" {
[…]
# terraform_data.release_info will be updated in-place
~ resource "terraform_data" "release_info" {
id = "efe675b3-074d-b28a-2164-179db5b4dcdf"
~ input = {
+ port = (known after apply)
# (2 unchanged attributes hidden)
}
[…]
Plan: 1 to add, 1 to change, 0 to destroy.
random_integer.port: Creating...
random_integer.port: Creation complete after 0s [id=8738]
terraform_data.release_info: Modifying... [id=efe675b3-074d-b28a-2164-179db5b4dcdf]
terraform_data.release_info: Modifications complete after 0s [id=efe675b3-074d-b28a-2164-179db5b4dcdf]
Apply complete! Resources: 1 added, 1 changed, 0 destroyed.release_info is updated in place (terraform_data can change input without
replacement), and it waits for random_integer.port because it references the
port's result. (My owner was already checkout-team from the article's
walkthrough; yours may differ.)
Without running anything, predict the plan if you change random_pet.release's
length from 2 to 3: which resources are replaced, which are updated in place,
which are untouched, and what does the summary line say? Then check.
Hint
Follow the references outwards from random_pet.release.id. For each resource that
uses it, ask whether the argument that changes can be updated in place.
Show Solution
After changing length = 2 to length = 3, I filtered the plan down to the resource
headers, the replacement reasons and the summary:
terraform plan | grep -E '^ # |^Plan|forces replacement' # local_sensitive_file.api_token must be replaced
~ filename = "./out/checkout-optimum-orca.token" -> (known after apply) # forces replacement
# random_pet.release must be replaced
~ length = 2 -> 3 # forces replacement
# terraform_data.release_info will be updated in-place
Plan: 2 to add, 1 to change, 2 to destroy.The pet is replaced because length can't change in place; the token
file is replaced because its filename contains the pet name; release_info only
updates in place. random_password.api_token doesn't reference the pet, so it's
untouched.
In the AWS configuration, add an analytics bucket in us-west-2 using a new
provider alias named west. Prove with terraform validate and a mocked
terraform plan that it lands in the right region, then say how you would do the
same thing without an alias.
Show Solution
I put the alias, with the same plan-only mock settings as mock_override.tf, in a
new file:
provider "aws" {
alias = "west"
region = "us-west-2"
# plan-only mock settings, as in mock_override.tf
access_key = "mock_access_key"
secret_key = "mock_secret_key"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
}
resource "aws_s3_bucket" "analytics" {
provider = aws.west
bucket = "checkout-analytics-4f7c2a"
}terraform validate
terraform plan -input=false | grep -E '# aws_s3_bucket|region |^Plan'Success! The configuration is valid.
# aws_s3_bucket.analytics will be created
+ bucket_region = (known after apply)
+ region = "us-west-2"
# aws_s3_bucket.logs will be created
+ bucket_region = (known after apply)
+ region = "us-east-1"
# aws_s3_bucket.logs_dr will be created
+ bucket_region = (known after apply)
+ region = "eu-west-1"
# aws_s3_bucket_public_access_block.logs will be created
+ region = "us-east-1"
# aws_s3_bucket_versioning.logs will be created
Plan: 5 to add, 0 to change, 0 to destroy.Each bucket plans into the region of the provider configuration it uses. Without an
alias, on AWS provider v6 you'd set region = "us-west-2" on the bucket itself, as
in the logs_west example. One more detail in the full plan: the analytics bucket
shows tags_all = (known after apply) instead of the service and managed_by
tags, because the west provider block doesn't define default_tags. Every alias is
a complete, separate configuration, so keep them few and consistent.
Tested with: Terraform v1.17.1 on Ubuntu 24.04 (WSL2, linux_amd64), November 2026,
with hashicorp/random v3.9.1, hashicorp/local v2.9.1 and hashicorp/aws v6.72.0.
Every local example was applied and every output is from a real run, trimmed where
marked […]. The AWS configurations were only run through init, validate and
terraform plan with the mock provider settings shown, with my shell's AWS
credential files disabled; nothing was applied to AWS. Provider downloads came from a
shared plugin cache, which doesn't change init's output.
Written by The Dark Debugger
Engineer documenting the things that took me too long to learn, so they take you less time. Every Terraform tutorial here is tested end to end before publishing.