0
0
Terraformcloud~30 mins

Drift detection in CI/CD in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use an output block with a value string describing the drift detection result.