Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Drift detection in CI/CD
📖 Scenario: You are managing infrastructure using Terraform. Sometimes, manual changes happen outside Terraform, causing drift between your declared infrastructure and the actual cloud resources.Detecting this drift early in your CI/CD pipeline helps keep your infrastructure consistent and reliable.
🎯 Goal: Build a simple Terraform configuration and a drift detection step that checks for differences between your Terraform state and real infrastructure.You will create a Terraform resource, configure a drift detection command, and output the drift check result.
📋 What You'll Learn
Create a Terraform configuration file with an AWS S3 bucket resource named exactly example_bucket.
Add a Terraform variable called bucket_name with default value my-unique-bucket-12345.
Use the terraform plan command to detect drift in the infrastructure.
Print the output of the drift detection command.
💡 Why This Matters
🌍 Real World
Infrastructure drift can cause unexpected issues in cloud environments. Detecting drift early helps maintain stable and secure infrastructure.
💼 Career
DevOps engineers and cloud engineers use drift detection to ensure infrastructure as code matches the real deployed resources, preventing configuration errors.
Progress0 / 4 steps
1
Create Terraform configuration with an S3 bucket
Create a Terraform file with a resource named aws_s3_bucket.example_bucket that uses the bucket name from a variable called bucket_name. Define the variable bucket_name with default value my-unique-bucket-12345.
Terraform
Hint
Use variable block to define bucket_name. Use resource "aws_s3_bucket" "example_bucket" to create the bucket resource.
2
Add a Terraform plan command for drift detection
Add a shell command string variable called drift_command that contains the exact command terraform plan -detailed-exitcode to detect drift.
Terraform
Hint
Use a variable block to store the shell command string for drift detection.
3
Create a local-exec provisioner to run drift detection
Add a null resource named drift_check with a local-exec provisioner that runs the command stored in the variable drift_command.
Terraform
Hint
Use null_resource with a local-exec provisioner to run the drift detection command.
4
Print the drift detection result
Add an output named drift_result that shows the result of running the drift_check resource.
Terraform
Hint
Use an output block with a value string describing the drift detection result.
Practice
(1/5)
1. What is the main purpose of drift detection in a Terraform CI/CD pipeline?
easy
A. To find differences between the Terraform code and the actual infrastructure
B. To speed up the deployment process by skipping validation
C. To automatically delete unused resources without approval
D. To generate documentation for the infrastructure
Solution
Step 1: Understand drift detection concept
Drift detection compares the current real infrastructure state with the Terraform code to find differences.
Step 2: Identify the purpose in CI/CD
In CI/CD, drift detection helps catch unexpected changes before applying new updates.
Final Answer:
To find differences between the Terraform code and the actual infrastructure -> Option A
Quick Check:
Drift detection = find differences [OK]
Hint: Drift detection = spot differences before apply [OK]
Common Mistakes:
Thinking drift detection speeds deployment
Assuming it deletes resources automatically
Confusing it with documentation generation
2. Which Terraform command is commonly used in CI/CD pipelines to detect drift before applying changes?
easy
A. terraform plan
B. terraform apply
C. terraform init
D. terraform destroy
Solution
Step 1: Recall Terraform commands
terraform plan shows the changes Terraform will make without applying them.
Step 2: Identify drift detection command
terraform plan detects differences (drift) between code and real infrastructure before apply.
Final Answer:
terraform plan -> Option A
Quick Check:
Detect drift = terraform plan [OK]
Hint: Use terraform plan to preview changes [OK]
Common Mistakes:
Using terraform apply which changes infrastructure
Confusing terraform init with drift detection
Using terraform destroy which deletes resources
3. Given the following Terraform plan output snippet in a CI/CD pipeline:
# aws_instance.example will be updated in-place
~ tags = {
- "Environment" = "dev"
+ "Environment" = "prod"
}
What does this output indicate about drift?
medium
A. Terraform will ignore the tag change
B. The instance will be destroyed and recreated
C. No drift is detected; tags remain unchanged
D. The tag "Environment" has drifted from "dev" to "prod" and will be updated
Solution
Step 1: Analyze the plan output
The '~' symbol means in-place update. The tag "Environment" changes from "dev" to "prod".
Step 2: Understand drift implication
This shows drift: the real infrastructure tag differs from code and will be updated.
Final Answer:
The tag "Environment" has drifted from "dev" to "prod" and will be updated -> Option D
Quick Check:
~ means update tag from dev to prod [OK]
Hint: Look for ~ symbol to spot in-place updates [OK]
Common Mistakes:
Thinking resource will be destroyed instead of updated
Ignoring tag changes as no drift
Assuming Terraform ignores tag differences
4. You run terraform plan in your CI/CD pipeline but it does not detect drift even though manual changes were made outside Terraform. What is the most likely cause?
medium
A. Terraform automatically ignores manual changes
B. You forgot to run terraform apply first
C. Terraform state file is outdated or corrupted
D. The provider plugin is missing
Solution
Step 1: Understand drift detection dependency
Terraform relies on the state file to compare real infrastructure with code.
Step 2: Identify cause of missed drift
If the state file is outdated or corrupted, Terraform cannot detect manual changes (drift).
Final Answer:
Terraform state file is outdated or corrupted -> Option C
Quick Check:
State file outdated = missed drift detection [OK]
Hint: Check state file freshness if drift not detected [OK]
Common Mistakes:
Assuming terraform apply affects drift detection
Believing Terraform ignores manual changes by design
5. In a CI/CD pipeline, you want to automatically detect drift and fail the pipeline if any drift is found before applying changes. Which approach best achieves this?
hard
A. Run terraform apply directly and rely on errors to detect drift
B. Run terraform plan and parse its output to detect changes, then fail if changes exist
C. Skip drift detection and always apply changes
D. Manually check infrastructure outside the pipeline
Solution
Step 1: Understand CI/CD drift detection goal
The goal is to detect drift and fail early before applying changes.
Step 2: Choose correct command and method
terraform plan shows drift without applying; parsing its output allows pipeline to fail if drift exists.
Step 3: Evaluate other options
Applying directly risks unwanted changes; manual checks are slow; skipping detection is unsafe.
Final Answer:
Run terraform plan and parse its output to detect changes, then fail if changes exist -> Option B
Quick Check:
Plan + parse output = fail on drift [OK]
Hint: Use terraform plan output to gate pipeline success [OK]