# ctx.operation

The `ctx.operation` value describes the operation that triggered a lifecycle hook, such as `terraform apply`, and
for hooks that run afterward, how it ended. It describes the parent operation, not any process the hook starts.

## Usage

```python
ctx.operation.command
ctx.operation.status
ctx.operation.exit_code
```

## Returns

A value with these attributes, or `None` when the script is not running as a hook:

- **`command`** — The canonical operation, such as `terraform apply`.
- **`status`** — The outcome, `"success"` or `"failure"`, for hooks that run after the operation. Before hooks, and hooks whose outcome is unknown, see `None`.
- **`exit_code`** — The operation's exit code as an integer for after hooks (`0` on success), otherwise `None`.
- **`error`** — The error message of a failed operation, or `None` when there is no error or the hook runs before the operation.
- **`stdout`, `stderr`** — The operation's captured output when available. Ordinary lifecycle hooks do not capture it, so these are
  usually `None`.

Terraform state and outputs are not attached to the operation.

## Examples

### Run a check only after a successful apply

```python
if ctx.operation.status != "success":
    ui.warning("Skipping smoke test: {} did not succeed".format(ctx.operation.command))
else:
    ctx.component.exec(["./smoke-test"])
```

### Branch between before and after hooks

```python
if ctx.operation.status == None:
    ui.info("About to run " + ctx.operation.command)
else:
    ui.info("{} finished with status {}".format(ctx.operation.command, ctx.operation.status))
```

### Report a failure

```python
if ctx.operation.error != None:
    fail("{} failed: {}".format(ctx.operation.command, ctx.operation.error))
```

## Related

- [`ctx.hook`](/functions/automation/ctx.hook) names the hook and its event.
- [`ctx.component`](/functions/automation/ctx.component) is the hook's component.
- [`ctx`](/functions/automation/ctx) lists every context attribute.
- [Lifecycle hooks](/scripting/lifecycle-hooks) and [stack hooks](/stacks/hooks)
