0
0
Terraformcloud~10 mins

Terraform's declarative approach - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform's declarative approach
Write desired state in config
Run terraform plan
Terraform compares current state
Shows changes needed
Run terraform apply
Terraform makes changes to match desired state
Infrastructure matches config
End
Terraform lets you write what you want your cloud setup to look like. It then figures out what to change to make it so.
Execution Sample
Terraform
resource "aws_s3_bucket" "mybucket" {
  bucket = "my-unique-bucket-123"
  acl    = "private"
}
This code declares a private S3 bucket named 'my-unique-bucket-123'. Terraform will create or update the bucket to match this.
Process Table
StepActionTerraform StatePlanned ChangeResult
1Read config fileNo bucket existsCreate bucket 'my-unique-bucket-123'Plan shows bucket creation
2Run terraform applyNo bucket existsCreate bucket 'my-unique-bucket-123'Bucket created in cloud
3Run terraform plan againBucket exists as declaredNo changes neededPlan shows no changes
4Change acl to 'public-read' in configBucket acl is 'private'Update bucket acl to 'public-read'Plan shows acl update
5Run terraform applyBucket acl is 'private'Update bucket acl to 'public-read'Bucket acl updated
6Run terraform planBucket acl is 'public-read'No changes neededPlan shows no changes
💡 Terraform stops planning when the actual infrastructure matches the declared desired state.
Status Tracker
VariableStartAfter Step 2After Step 5Final
Bucket existenceFalseTrueTrueTrue
Bucket aclN/Aprivatepublic-readpublic-read
Key Moments - 3 Insights
Why does Terraform show changes even if I only wrote the desired state once?
Terraform compares the current real infrastructure with your desired state (see execution_table step 1). It plans changes to make them match.
What happens if I run 'terraform apply' without changing the config?
Terraform sees no difference between current and desired state (step 3), so it does nothing.
How does Terraform know what to change when I update the config?
It compares the old state with the new desired state and plans only the differences (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the planned change at step 4?
AUpdate bucket acl to 'public-read'
BCreate a new bucket
CDelete the bucket
DNo changes needed
💡 Hint
Check the 'Planned Change' column at step 4 in the execution_table.
At which step does Terraform first create the bucket?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Result' column to see when the bucket is created.
If you run 'terraform plan' after step 6, what will it show?
APlan to delete the bucket
BNo changes needed
CPlan to create a new bucket
DPlan to update bucket acl to 'private'
💡 Hint
See the 'Planned Change' at step 6 in the execution_table.
Concept Snapshot
Terraform's declarative approach means you write what you want, not how to do it.
Terraform compares your desired state with the real state.
It plans only the changes needed to match.
Apply makes those changes.
Repeated plans show no changes if states match.
Full Transcript
Terraform uses a declarative approach where you write configuration files describing the desired cloud infrastructure. When you run 'terraform plan', it compares this desired state with the current real infrastructure state. It then shows what changes it will make to match your configuration. Running 'terraform apply' executes those changes. If you run 'terraform plan' again without changes, it shows no changes needed. If you update your config, Terraform plans only the differences and applies them. This way, you manage infrastructure by declaring the end state, and Terraform handles the steps to get there.