Terraform Hooks

Terraspace calls out to the terraform command. You can execute commands before and after each command with CLI hooks.

Generator

To help you get started quickly, you can generate starter hook code. Check out the examples in the Hooks Generator Docs.

Example

config/hooks/terraform.rb

before("init",
  execute: "echo hi",
)

after("apply",
  execute: "echo bye"
)

So you can customize terraspace to call your commands.

exit on fail

By default, if the hook commands fail, then terraspace will exit with the original hook error code. You can change this behavior with the exit_on_fail option.

before("init",
  execute: "/command/will/fail/but/will/continue",
  exit_on_fail: false,
)

In this case, regardless of the hook command succeeding or failing, terraspace will continue right along.

Mutiple Commands at Once

You can also specify multiple commands at once:

before("init", "apply",
  execute: "echo hi",
)

General Form

before(COMMAND_NAME, OPTIONS)

The command name corresponds to the terraform commands.

Hook Options

Name Description
label A human-friendly label so you can see what hooks is being run.
execute The script or command to run. IE: path/to/some/script.sh
exit_on_fail Whether or not to continue process if the script returns an failed exit code.

Ruby Hooks

Instead of using a script for the hook execute option, you can also use a Ruby object. This provides some more control over the current process. See: Ruby Hooks