Drift, health and checks

How real infrastructure drifts away from its code, how health assessments find drift and failing checks on a schedule, the free way to see drift with a refresh-only plan, and how checks and tests catch mistakes.

GazeekTERRAFORM AND PACKER: INFRASTRUCTURE AS CODE·LESSON 15
LESSON 15

Drift, health and checks

How real infrastructure drifts away from its code, how health assessments find drift and failing checks on a schedule, the free way to see drift with a refresh-only plan, and how checks and tests catch mistakes.

0:00 / 3:54

Diagrams in this video · 2Free in archhy

Copy and edit in archhy: What drift is (new tab)
Copy and edit in archhy: Detect, notify, decide, apply (new tab)

After this lesson you can

  • Explain what configuration drift is and how it happens
  • Describe drift detection and continuous validation
  • Fix drift by overwriting it or accepting it into the code
  • Use refresh-only plans, check blocks and terraform test
Transcript

Drift, health and checks

Space Llama here. Your infrastructure is built, reviewed and guarded. Lovely. Then, at two in the morning, someone fixes something by hand in the console. Today, drift. How to notice it, what to do about it, and how checks and tests keep you honest.

What drift is

Drift is when your real infrastructure no longer matches your Terraform configuration. It happens when changes are made outside Terraform's normal process. Here, the code says both web servers are small. But during an incident, someone resized web-2 by hand, in the console. Now the code and the cloud disagree. The next apply will put web-2 back to small. It's in the plan, if anyone reads it closely. The fix isn't to ban consoles. It's to notice, and then decide.

Detect, notify, decide, apply

Handling drift is a loop. One, detect. Compare the real infrastructure with the code. Two, notify. Tell the people who own that workspace. Three, decide. If the change was a mistake, overwrite it. Apply the code again, and the cloud goes back. If the change was right, accept it. Update the code to match, so the next apply keeps it. Four, apply. Code and cloud agree again. Then round you go. In the Standard and Premium editions of HCP Terraform, on the HashiCorp Cloud Platform, health assessments run this check for you, on a schedule. On any edition, terraform plan with the refresh-only option shows you what changed outside Terraform. When you accept a change, update the code. Terraform apply with refresh-only can also bring the state up to date, without touching any infrastructure.

Health assessments

Health assessments do two jobs. Drift detection asks, does the real infrastructure still match the configuration? Continuous validation asks, do the conditions in my configuration still pass? Results appear on the workspace, with a warning in the workspace list when something has drifted or failed. They need remote or agent execution, and the workspace's last run must have succeeded. And they never get in the way. If a run starts during an assessment, the assessment steps aside and tries again later.

Checks and tests

Drift is one kind of surprise. Bugs in your own code are another. Inside the configuration, preconditions and postconditions check your assumptions, and can stop an operation. Check blocks are gentler. When one fails, Terraform warns and carries on. Continuous validation reruns them for you. Then there's terraform test. You write test files ending in dot tftest dot hcl. Tests usually build short-lived real infrastructure, check it, and destroy it. Or they can just plan, for a quick check. Checks catch problems in running infrastructure. Tests catch mistakes before they ship.

Recap

Let's recap. Drift is when the real infrastructure no longer matches the code, usually after a change by hand. Detect it, then overwrite it, or accept it into the code. Health assessments can watch for it on a schedule. And anyone can run terraform plan with refresh-only. And check blocks, conditions and terraform test catch problems before and after they ship.

Practice and what's next

That's drift, health and checks, and the end of Guardrails and day two. To practice, try the free practice questions at Gazeek Labs. Next, a new part, Packer and golden images, starting with Golden images: why Packer. See you there. More videos on the channel. Alpaca Mango. I'll be there. I'm always there. Mate.

Gazeek Flash · free

Exam questions on your phone

Free questions on Guardrails and day two: quick-fire batches and timed exams, on the web or Android.