Ruby Hooks

The hook execute option can also be provided Ruby code instead of a string. Here’s how it works:

  1. When the execute option is a String: Terraspace will shell out and run it as a script.
  2. When the execute option is a Ruby object: Terraspace will perform the call method on the object or class.

Ruby Class

When the Ruby object is a class with an instance method call, Terraspace creates a new instance of the class and runs its call method. Example:


class EnvExporter
  def call
    ENV['SECRET_FOO'] = "bar"

  execute: EnvExporter,

Terraspace will do something like this:

The example sets the SECRET_FOO environment variable. It is then available from ERB in the project files.

Ruby Object

When the Ruby object, Terraspace expects it to have a call method and will run it. Example:


  execute: lambda { ENV['SECRET_FOO'] = "hi2" }

Terraspace will do something like this:


Process Context

The context in which the hook runs is worth highlighting. When the execute option is a String, Terraspace runs the script in a new child process. This the script is an independent process, and whatever is done to its environment is segregated.

When the execute option a Ruby object, then Terraspace runs the hook within the same process. It means the hook can affect the same environment. IE: Setting environment variables.

More tools: