Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Same security group, two writers
Your port 443 rule disappeared after a teammate ran terraform apply. During the outage, you added it in the console, so web-sg briefly held 80 and 443. Then apply ran and took it back to port 80 only. Two writers target the same security group, and Terraform follows only its code.
main.tf (assumed: only port 80 declared)
Here's main.tf. It declares web-sg with a single inline ingress block, port 80 from anywhere. Terraform treats this file as the desired state. And inline ingress blocks count as the complete rule set, so any rule that isn't listed here, like a console-added 443, is unwanted.
resource "aws_security_group" "web" {
name = "web-sg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}Step 2: apply compares reality with code
When apply runs, it first refreshes web-sg from AWS and finds both 80 and 443. Then it compares that with main.tf, which wants 80 only. To close the gap, it changes AWS, not the code, so 443 is removed. The state file just records the result.
terraform plan output (illustrative, abridged)
Here's the plan the teammate would have seen. The minus lines show the 443 ingress rule being removed from web-sg, and the summary says one to change, nothing added or destroyed. The revert was announced right there. Approving it without reading the drift is how the rule silently disappeared.
# aws_security_group.web will be updated in-place
~ resource "aws_security_group" "web" {
~ ingress = [
- {
- from_port = 443
- to_port = 443
},
]
}
Plan: 0 to add, 1 to change, 0 to destroy.Where the change lives decides if it survives
Terraform makes AWS match the code. With the console fix, the 443 rule lands in web-sg, the next apply finds drift, and it reverts the change, so 443 is removed. With the code fix, the PR merges, apply adds 443, and it stays. Only a change in main.tf survives.
main.tf with the 443 rule backported
The fix is to put the second ingress block in main.tf, the one for port 443, and merge it through a pull request. Now the code matches what's live in AWS. The next plan should show no change for web-sg, and apply will keep the 443 rule.
resource "aws_security_group" "web" {
name = "web-sg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}Team workflow that stops the flip-flop
Make changes in main.tf first, even during an outage. If you must edit in the console, backport it the same day, with a ticket to track it. Run scheduled or CI plans to catch drift. Restricting console write access is an optional guardrail.
- Change main.tf first, even during outages
- Emergency console edit? Backport the same day
- Run scheduled terraform plan to flag drift
Terraform makes AWS match the code, so un-coded changes get erased.
Terraform makes AWS match the code, so changes that aren't in the code get erased. That's why your 443 rule disappeared. Once it's backported into main.tf, refresh and code agree, and apply leaves web-sg alone. Every console change needs to end up in main.tf before the next apply.