# Built-in hooks

You can use hooks to execute actions at specific stages of stack deploy and undeploy operations. Takomo has two built-in hooks, and you can also implement your own.

In a stack configuration, you choose which type of hook to use by providing value to the `type` property. You also need to give a name for each hook. In addition to these two mandatory properties, each hook type may have its own set of additional properties.

## Command hook

The command hook executes the specified shell command.

### Properties

Here are the properties of the Command hook:

| Key                    | Required | Type    | Description                                                                                                                                                     |
| ---------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type                   | yes      | string  | Type of the hook, this must be **cmd**.                                                                                                                         |
| name                   | yes      | string  | Name of the hook.                                                                                                                                               |
| command                | yes      | string  | Shell command to execute.                                                                                                                                       |
| cwd                    | no       | string  | Path to the working directory from where the shell command is executed. Relative to the current project directory.                                              |
| exposeStackCredentials | no       | boolean | Make the current stack's AWS credentials available for the shell command. Defaults to false.                                                                    |
| exposeStackRegion      | no       | boolean | Make the current stack's region available for the shell command. Defaults to false.                                                                             |
| capture                | no       | string  | Controls how to capture the output of the executed shell command. By default, all output is captured. To capture only the last line, set this to **last-line.** |

### Environment variables available in the shell command

The following environment variables are available in the shell command:

| Name                     | Description                                                                                                                                                                                                                                                                                                |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| TKM\_COMMAND\_STAGE      | The current stack operation stage.                                                                                                                                                                                                                                                                         |
| TKM\_COMMAND\_OPERATION  | The current stack operation.                                                                                                                                                                                                                                                                               |
| TKM\_COMMAND\_STATUS     | The current stack operation status, not present in before stage                                                                                                                                                                                                                                            |
| AWS\_ACCESS\_KEY\_ID     | If `exposeStackCredentials` is **true**, this will hold the access key id of credentials of the current stack.                                                                                                                                                                                             |
| AWS\_SECRET\_ACCESS\_KEY | If `exposeStackCredentials` is **true**, this will hold the secret access key of credentials of the current stack.                                                                                                                                                                                         |
| AWS\_SESSION\_TOKEN      | If `exposeStackCredentials` is **true**, this will hold the session token of credentials of the current stack.                                                                                                                                                                                             |
| AWS\_SECURITY\_TOKEN     | If `exposeStackCredentials` is **true**, this will hold the session token of credentials of the current stack.                                                                                                                                                                                             |
| AWS\_DEFAULT\_REGION     | If `exposeStackRegion` is **true**, this will hold the region of the current stack.                                                                                                                                                                                                                        |
| TKM\_HOOK\_{hook-name}   | Values returned from previous hooks are exposed in environment variables where the {hook-name} placeholder is replaced with the hook's name. Hooks whose name has unsafe characters not compatible with a pattern /^\[a-zA-Z\_]+\[a-zA-Z0-&#x39;*\_*]\*$/ are not exposed and a warning is logged instead. |

Any output the hook prints to the stdout is captured and exposed to other hooks.

### Examples

A command hook that runs a simple shell command:

```yaml
- name: my-hook
  type: cmd
  command: echo 'hello world'
```

A command hook that exposes the current stack's AWS credentials to the shell command:

```yaml
- name: my-another-hook
  type: cmd
  exposeStackCredentials: true
  command: aws sts get-caller-identity
```

## Checksum hook

The checksum hook calculates a checksum from a specified directory. The checksum is calculated recursively, i.e. all files and directories under the specified directory are included in the checksum.

### Properties

Here are the properties of the Checksum hook:

| Key      | Required | Type   | Description                                                                                                                        |
| -------- | -------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------- |
| type     | yes      | string | Type of the hook, this must be **checksum**                                                                                        |
| name     | yes      | string | Name of the hook                                                                                                                   |
| dir      | yes      | string | Path to the directory from where the checksum should be calculated. The path can be absolute or relative to the project directory. |
| encoding | no       | string | Encoding used to encode the calculated checksum. Supported values are **base64** (default) and **hex**.                            |

### Examples

Calculate a checksum from a directory lambda/scripts located in the project directory.

```yaml
- name: my-checksum
  type: checksum
  dir: lambda/scripts
```

The same as above but using the hex encoding.

```yaml
- name: my-hex-checksum
  type: checksum
  dir: lambda/scripts
  encoding: hex
```

## See also

* [Configuring hooks in stack configuration](https://docs.takomo.io/stack-properties/hooks)
