By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
For engineers who need to keep infrastructure in sync with reality—before it breaks in production.
Refresh in Terraform means syncing your state file (terraform.tfstate) with the actual cloud resources. Drift detection is the process of identifying when real-world infrastructure diverges from what’s defined in your Terraform code.
terraform.tfstate
terraform apply
terraform plan
Superpower: Refresh and drift detection let you: ? Catch manual changes before they cause outages. ? Automate compliance (e.g., "No public S3 buckets allowed"). ? Avoid "works on my machine" disasters when deploying to prod.
terraform refresh
terraform plan -refresh-only
terraform state
terraform state list
terraform state show
terraform state rm
ignore_changes
tags
hcl lifecycle { ignore_changes = [tags] }
terraform import
>= 1.0.0
Detect drift on an EC2 instance that was manually modified in the AWS Console.
# main.tf provider "aws" { region = "us-east-1" } resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 instance_type = "t2.micro" tags = { Name = "terraform-example" } }
i-1234567890abcdef0
bash terraform import aws_instance.example i-1234567890abcdef0
bash terraform state show aws_instance.example
t2.micro
t2.small
Environment = "staging"
Run a refresh-only plan:
Expected Output:
aws_instance.example: Refreshing state... [id=i-1234567890abcdef0] Terraform detected the following changes made outside of Terraform: # aws_instance.example has been changed ~ resource "aws_instance" "example" { id = "i-1234567890abcdef0" ~ instance_type = "t2.micro" -> "t2.small" ~ tags = { + "Environment" = "staging" "Name" = "terraform-example" } # (other unchanged attributes) } This is a refresh-only plan, so Terraform will not take any actions to undo these. If you were expecting these changes then you can apply this plan to record the updated values in the Terraform state without changing any remote objects.
Terraform will revert the instance type to t2.micro and remove the Environment tag.
Environment
If the manual change was intentional, update main.tf:
main.tf
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.small" # Updated to match reality tags = { Name = "terraform-example" Environment = "staging" # Added } }
Then run:
*
hcl tags = { Owner = "team-infra" Environment = "prod" Terraform = "true" }
aws s3 cp s3://my-bucket/terraform.tfstate ./backups/
bash export TF_LOG=DEBUG terraform plan -refresh-only
-refresh-only
apply
aws s3 sync
Correct: "It syncs the state file with real-world resources."
"How do you detect drift?"
Correct: "Run terraform plan -refresh-only."
"What happens if you delete a resource manually?"
plan
security_group_ids
Scenario: You have an S3 bucket (my-company-logs) that was manually configured to enable versioning. Your Terraform code doesn’t have versioning enabled. Detect and fix the drift.
my-company-logs
Solution:1. Import the bucket into state: bash terraform import aws_s3_bucket.logs my-company-logs2. Run a refresh-only plan: bash terraform plan -refresh-only3. Update your Terraform config to match reality: hcl resource "aws_s3_bucket" "logs" { bucket = "my-company-logs" versioning { enabled = true # Added to match manual change } }4. Apply: bash terraform apply
bash terraform import aws_s3_bucket.logs my-company-logs
bash terraform plan -refresh-only
hcl resource "aws_s3_bucket" "logs" { bucket = "my-company-logs" versioning { enabled = true # Added to match manual change } }
bash terraform apply
Why It Works: terraform import brings the bucket under Terraform management, and plan -refresh-only reveals the drift. Updating the config ensures future apply commands won’t revert the change.
plan -refresh-only
terraform state show aws_instance.web
terraform import aws_s3_bucket.logs my-bucket
lifecycle { ignore_changes = [tags] }
terraform state rm aws_instance.old
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.