Labs · DevOps
Lab: Change a Terraform-Managed Security Group Outside Terraform, Detect the Drift with plan -refresh-only, and Fix It by Reverting or by Updating the Code
Course: DevOps · Chapter 42: Terraform Drift and Troubleshooting · Lesson: 42.1 Drift: when AWS no longer matches the code
Chapter 42 explains drift, TF_LOG and the common errors; this lab creates drift on purpose and fixes it.
Chala mitrano! Ratri koNitari console madhe port ughadla, ani code la mahitach nahi. Aaj drift pakduya.
Suppose we are…
Suppose we are on an Infosys support team. During a night incident an engineer opened port 22 to the world "just for five minutes" and forgot. The Terraform code still says the port is closed. That gap between code and cloud is drift.
In this lab we create a security group with Terraform, make two changes outside Terraform, see them in the plan, and fix them properly.
Goal of this lab
By the end you will have:
- A security group created by Terraform in the default VPC.
- Two manual changes (an extra SSH rule and a tag) seen in
terraform plan. - The extra rule removed by Terraform, and the tag decision moved into code.
What you need
- Terraform 1.6 or newer (
terraform version). - AWS CLI v2 signed in to your own AWS account with an IAM user or SSO role that may create these resources (
aws sts get-caller-identityworks). Never use the root user, and never paste access keys into .tf files. - Region Mumbai (
ap-south-1) is used; changeregionif you prefer another. - 35 minutes.
Cost and cleanup
A security group is free. Small charges are possible even on the free tier. Finish the lab in one sitting and always run the Clean up part at the end.
Part 1: Create the security group
Create a folder and
main.tf:main.tf
terraform { required_version = ">= 1.6" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "ap-south-1" } data "aws_vpc" "default" { default = true } resource "aws_security_group" "web" { name = "drift-lab-web" description = "HTTP only" vpc_id = data.aws_vpc.default.id ingress { description = "HTTP" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "drift-lab-web", owner = "platform" } } output "sg_id" { value = aws_security_group.web.id }mkdir tf-drift-lab && cd tf-drift-labThis lab uses inline
ingressblocks on purpose: Terraform then owns the full rule list and reports any extra rule as drift.Apply:
terraform init terraform applyWhat you should see:
Apply complete! Resources: 1 addedand thesg_idoutput.
Part 2: Make drift outside Terraform
Add an SSH rule open to the world and change a tag with the CLI (this plays the "night incident"):
SG=$(terraform output -raw sg_id) aws ec2 authorize-security-group-ingress --group-id $SG --protocol tcp --port 22 --cidr 0.0.0.0/0 aws ec2 create-tags --resources $SG --tags Key=owner,Value=night-shiftSee the drift without planning any change:
terraform plan -refresh-onlyWhat you should see:
Objects have changed outside of Terraformwith the new port 22 rule andowner = "night-shift".See what a normal plan wants to do:
terraform planWhat you should see:
~ update in-placethat removes the port 22 rule and setsownerback toplatform;Plan: 0 to add, 1 to change, 0 to destroy.
Part 3: Fix it the right way
Decide per change. The open SSH rule is wrong: let Terraform remove it. The tag change is a real ownership change: put it in code. Edit
main.tfsoowner = "night-shift", then:terraform planWhat you should see: the plan now only removes the port 22 rule.
Apply and confirm:
terraform apply aws ec2 describe-security-groups --group-ids $SG --query "SecurityGroups[0].IpPermissions[].FromPort" terraform planWhat you should see:
[ 80 ]only, thenNo changes.
Clean up: destroy everything
Delete everything this lab created. Type
yeswhen asked:terraform destroyWhat you should see:
Destroy complete! Resources: 1 destroyed.
Ravindra Bagale's Tip
Run terraform plan on a schedule (a nightly pipeline) and alert when it is not "No changes". Drift found the same night is a small fix; drift found in six months is an outage.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Running terraform apply -refresh-only without reading it | The bad SSH rule is accepted into state | Read the refresh-only plan; fix the code or revert first |
| Fixing drift by clicking in the console again | Code and cloud still differ | Change code, then apply |
| Mixing inline rules and separate rule resources on one group | Rules fight each other on every apply | Use one style per security group |
Self-check checklist
0 of 5 done
Try-at-home challenge
Run a plan with TF_LOG=DEBUG TF_LOG_PATH=tf.log terraform plan, search the log for DescribeSecurityGroups, then unset both variables and delete the log (it can contain sensitive details).
Samajla ka? Code is the truth; drift is a question you answer on purpose. Aata pudhe jaauya real AWS guides.