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
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, seeNone. exit_code- The operation's exit code as an integer for after hooks (
0on success), otherwiseNone. error- The error message of a failed operation, or
Nonewhen there is no error or the hook runs before the operation. stdout,stderrThe 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
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
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
if ctx.operation.error != None:
fail("{} failed: {}".format(ctx.operation.command, ctx.operation.error))
Related
ctx.hooknames the hook and its event.ctx.componentis the hook's component.ctxlists every context attribute.- Lifecycle hooks and stack hooks