Terraspace Hooks

You can use hooks to run scripts at specific steps of the Terraspace lifecycle. These lifecycle points occur at a higher-level than the terraform commands.

Lifecycle Hooks

Hook Description
build When Terraspace builds or compiles the Terraspace project to the .terraspace-cache folder.

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/terraspace.rb

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

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

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("build",
  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.

General Form

before(COMMAND_NAME, OPTIONS)

The command name corresponds to the terraspace 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

On Boot Hook

There is also a special on_boot hook that is runs very early in the Terraspace boot process. It runs right after plugins are loaded.

It takes a block of code. One useful way to use it is switching AWS_PROFILE, GOOGLE_APPLICATION_CREDENTIALS, etc automatically based on the TS_ENV. Example:

config/env/dev.rb

Terraspace.configure do |config|
  config.hooks.on_boot do
    ENV['AWS_PROFILE'] = 'dev'
  end
end