Takomo
  • What is Takomo?
  • Getting started
    • Installation
    • Quick start
    • Tutorial
  • Configuration
    • AWS credentials
    • Directory structure
    • Stacks and stack groups
    • Project configuration
  • Stack properties
    • Name
    • Regions
    • Template
    • Template bucket
    • Command role
    • Account ids
    • Depends
    • Parameters
    • Tags
    • Inherit tags
    • Termination protection
    • Timeout
    • Capabilities
    • Stack policy
    • Ignore
    • Obsolete
    • Hooks
    • Data
    • Schemas
  • Variables and templating
    • Introduction
    • Handlebars syntax
    • Environment variables
    • Command-line variables
    • Partials
    • Helpers
    • Available variables
  • Parameter resolvers
    • Built-in parameter resolvers
      • Stack output resolver
      • External stack output resolver
      • Command resolver
      • File contents resolver
      • Hook output resolver
      • SSM parameter resolver
      • Secret resolver
    • Custom parameter resolvers
  • Hooks
    • Built-in hooks
    • Custom hooks
    • Sharing data between hooks
  • Validation schemas
    • Custom validation schemas
  • Command-line usage
    • Common options
    • Deploy stacks
    • Undeploy stacks
    • Prune stacks
    • List stacks
    • Detect drift
    • Generate IAM policies
    • Inspect stack configuration
    • Inspect stack dependencies
  • Support
    • Getting help
    • Troubleshooting
  • Development
    • Change log
Powered by GitBook
On this page
  • Hook provider
  • Hook
  • Hook Input
  • Hook Output
  • Example

Was this helpful?

Export as PDF
  1. Hooks

Custom hooks

PreviousBuilt-in hooksNextSharing data between hooks

Last updated 3 years ago

Was this helpful?

You can provide custom hooks by placing plain JavaScript files, with .js file extension, into the hooks directory. Each file must export a . Takomo uses the provider to initialize the actual hook.

You can use all language features available in Node 14.4.0.

Hook provider

Hook provider has the following properties:

Key

Required

Type

Description

type

yes

string

Type of the hook

init

yes

function

Hook

Hook has the following properties:

Key

Required

Type

Description

execute

yes

function

Hook Input

A hook input is an object that is passed to hook's execute function. It has the following properties:

Key

Required

Type

Description

stage

yes

string

Current stack operation stage. Possible values are: before, after

operation

yes

string

Current stack operation. Possible values are: create, update, delete

status

yes

string

Current stack operation status. Possible values are: success, failed, cancelled This is defined only when the stage is after.

variables

yes

object

Mutable variables object containing command line and environment variables. The hook can modify existing variables and add new ones. After the hook is completed, the same variables object is passed to the subsequent hooks which can then access its contents. The variables are available also in the stack's template file.

ctx

yes

object

Command context object

Hook Output

A hook output is a value returned from hook's execute function. It is used to determine if the hook execution was successful and to share data between hooks. It can be either a boolean, an Error which is always considered as failure, or a detailed object with the following properties:

Key

Required

Type

Description

success

yes

boolean

A boolean determining if the hook execution was successful.

message

no

string

An informative message about the hook execution outcome.

value

no

any

A value to be exposed to other hooks.

skip

no

boolean

A boolean determining if all the remaining hooks of the current stack and the stack operation itself should be skipped.

Example

This example hook prints some debug information to the console.

Our file structure looks like this:

.
├─ stacks
│  └─ my-stack.yml
├─ hooks
│  └─ debug.js
└─ templates
   └─ my-stack.yml

The hook provider defined in hooks/debug.js looks like this:

hooks/debug.js
module.exports = {
  type: "debug",
  init: (props) => {
    console.log("Initialize debug hook")
    return {
      execute: (input) => {
        console.log("Execute debug hook!")
        console.log(`Stage:     ${input.stage}`)
        console.log(`Operation: ${input.operation}`)
        console.log(`Status:    ${input.status}`)
        console.log(JSON.stringify(props, null, 2))
        
        return {
          message: "OK",
          success: true,
          value: "Did some debugging"
        }
      }
    }
  }
}

Our custom hook is used in the stack configuration like so:

stacks/my-stack.yml
hooks:
  - name: my-hook
    type: debug

When executed, the hook exposes string "Did some debugging" in the mutable variables object.

A function that initializes the hook with properties given in a stack group or stack configuration file. The function can be either synchronous or asynchronous, and must return an instantiated .

A function that is invoked with when the hook is executed. The function can be synchronous or asynchronous and must return .

hook provider object
hook object
an hook input object
a hook output