Run tasks: other tools in the run

How HCP Terraform run tasks send a run to an outside service, such as a security scanner, at fixed stages, and how advisory and mandatory enforcement decide whether a failure stops the run.

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

Run tasks: other tools in the run

How HCP Terraform run tasks send a run to an outside service, such as a security scanner, at fixed stages, and how advisory and mandatory enforcement decide whether a failure stops the run.

0:00 / 3:41

Diagrams in this video · 2Free in archhy

Copy and edit in archhy: Four places a task can plug in (new tab)
Copy and edit in archhy: How the service answers (new tab)

After this lesson you can

  • Explain what a run task does and why teams use one
  • Name the four run stages where a task can run
  • Compare advisory and mandatory enforcement
  • Describe how an outside service reports back to a run
Transcript

Run tasks: other tools in the run

G'day. Space Llama here, and welcome to Guardrails and day two. Your team ships infrastructure by pull request. But some checks need specialist tools. Security scanners. Cost checkers. Image checkers. Today, run tasks. How other tools get a say inside a Terraform run.

What a run task is

A run task connects a third-party tool to a run in HCP Terraform, on the HashiCorp Cloud Platform. At a chosen moment, HCP Terraform sends data about the run to the service. Things like the configuration and the plan. The service looks it over, decides whether the run passes or fails, and sends its answer back. Then HCP Terraform uses that answer, and the task's enforcement level, to decide whether the run can carry on. You can find ready-made run tasks in the Terraform Registry, or build your own. One we'll meet again later is HCP Packer's task. It flags a run that would launch a revoked machine image, and can stop it.

Four places a task can plug in

Every run moves from plan to apply. Run tasks can step in at four points. One, pre-plan, before Terraform makes the plan. Good for checking the configuration files. Two, post-plan, after the plan. The most popular stop, because now the service can read exactly what will change. Three, pre-apply, just before changes are made. Four, post-apply, after the apply has finished. Each task also gets an enforcement level. Advisory means a failure only shows a warning, and the run carries on. Mandatory means a failure, or even a timeout, stops the run. With several tasks, the strictest one wins. A failed mandatory task fails the run, whatever the advisory ones said.

How the service answers

Under the hood, it's a polite conversation. HCP Terraform sends a request to the service's address, with details about the run and a link to fetch the plan. The request also carries a callback address. The service works away, then calls back with running, passed or failed, and a message. That message appears right in the run, so the author sees why. No hunting through a separate tool. The service can't keep you waiting forever. If it sends no update within ten minutes, or takes more than an hour, the task errors.

Using them well

A good habit. Start a new task as advisory. Watch what it flags for a while. Tasks with real power deserve real review. Treat a new mandatory task like any other change to production. Once the results are trusted, switch it to mandatory, and it becomes a real guardrail. You can attach a task to individual workspaces, or scope it to projects or the whole organization. The free edition of HCP Terraform includes one run task integration, on up to ten workspaces.

Recap

Let's recap. Run tasks send a run to an outside service, which answers pass or fail. They can run at four stages, before and after the plan, and before and after the apply. Advisory tasks warn. Mandatory tasks can stop the run. And the service's message shows right in the run.

Practice and what's next

That's run tasks. Other tools, inside your run, with a vote. To practice, try the free practice questions at Gazeek Labs. Next lesson, Policy as code. See you there.

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.