Skip to content

Draft initial documentation on resource hooks #15463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/docs/iac/concepts/options/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Resource constructors accept the following resource options:
- [deleteBeforeReplace](/docs/concepts/options/deletebeforereplace/): override the default create-before-delete behavior when replacing a resource.
- [deletedWith](/docs/concepts/options/deletedwith/): If set, the provider's Delete method will not be called for this resource if the specified resource is being deleted as well.
- [dependsOn](/docs/concepts/options/dependson/): specify additional explicit dependencies in addition to the ones in the dependency graph.
- [hooks](/docs/concepts/options/hooks/): specify a set of resource hooks that will be executed at specific points in the resource lifecycle.
- [ignoreChanges](/docs/concepts/options/ignorechanges/): declare that changes to certain properties should be ignored during a diff.
- [import](/docs/concepts/options/import/): bring an existing cloud resource into Pulumi.
- [parent](/docs/concepts/options/parent/): establish a parent/child relationship between resources.
Expand Down
95 changes: 95 additions & 0 deletions content/docs/iac/concepts/options/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title_tag: "hooks | Resource Options"
meta_desc: The hooks resource option provides a set of resource hooks linked to a resource.
title: "hooks"
h1: "Resource option: hooks"
meta_image: /images/docs/meta-images/docs-meta.png
menu:
iac:
identifier: hooks
parent: options-concepts
weight: 7
aliases:
- /docs/intro/concepts/resources/options/hooks/
- /docs/concepts/options/hooks/
---

The `hooks` resource option provides a set of resource hooks linked to a resource. Hooks are used to execute custom logic at specific points in the resource lifecycle, such as before or after creation, update, or deletion.

Each hook is a callback that gets invoked by the Pulumi engine. Hooks that execute before an action are called **before hooks** and have names beginning with `before` or `Before` depending on the language. Hooks that execute after an action are called **after hooks** and have names beginning with `after` or `After` depending on the language. Pulumi currently supports the following hook types:

* *Create hooks* are called before or after a resource is created. This may occur during the initial creation of a resource or when a resource requires replacement due to e.g. a change in an immutable property.

* *Update hooks* are called before or after a resource is updated in-place.

* *Delete hooks* are called before or after a resource is deleted. This may occur during the deletion of a resource due to a `destroy` or that resource's removal from the program, or as part of a resource replacement due to e.g. a change in an immutable property.

When a hook is executed as part of a resource operation, it receives the resource's [URN](/docs/iac/concepts/resources/names/#urns) and ID, as well as any relevant input and output properties. Hooks may return errors. If a before hook returns an error, the action it precedes will *not* be executed and the Pulumi operation will fail with that error. If an after hook returns an error, Pulumi will log a warning diagnostic and the Pulumi operation will continue. The table below illustrates the combinations of inputs, outputs, and error behaviors for each hook type:

| Hook type | Old inputs | New inputs | Old outputs | New outputs | Error behavior |
|---------------|------------|------------|-------------|-------------|-----------------------------------|
| Before create | | ✓ | | | Prevent creation, fail deployment |
| After create | | ✓ | | ✓ | Log warning, continue deployment |
| Before update | ✓ | ✓ | ✓ | | Prevent update, fail deployment |
| After update | ✓ | ✓ | ✓ | ✓ | Log warning, continue deployment |
| Before delete | ✓ | | ✓ | | Prevent deletion, fail deployment |
| After delete | ✓ | | ✓ | | Log warning, continue deployment |

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick note before I forget: we should mention that hooks can return an error. For before hooks this causes the action to fail. After hooks don't cause a failure (the action already ran!), but do log a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout. I've taken a stab at it, though the table extension isn't the prettiest. Thoughts?
image

## Health checking example

{{< chooser language "javascript,typescript,python,go,csharp,java,yaml" >}}

{{% choosable language javascript %}}

```javascript
```

{{% /choosable %}}
{{% choosable language typescript %}}

```typescript
```

{{% /choosable %}}
{{% choosable language python %}}

```python
```

{{% /choosable %}}
{{% choosable language go %}}

```go
```

{{% /choosable %}}
{{% choosable language csharp %}}

```csharp
```

{{% /choosable %}}
{{% choosable language java %}}

```java
// Pulumi Java support for hooks is coming soon
```

{{% /choosable %}}
{{% choosable language yaml %}}

```yaml
# Pulumi YAML does not support resource hooks
```

{{% /choosable %}}

{{< /chooser >}}

## Deletions and delete hooks

In order for delete hooks to run successfully, Pulumi must have access to any necessary hooks at the time of the deletion. You should take the following actions to ensure that delete hooks run as expected:

* When removing resources from your program, first remove *only* the resources you wish to delete, *leaving any delete hooks in place*. Upon running e.g. `pulumi up`, Pulumi will delete the resources and run any relevant delete hooks. Once this operation is complete, you can then remove the delete hooks from your program.

* When running `pulumi destroy`, you must pass the `--run-program` flag to instruct Pulumi to run your program and register any hooks that are to be executed. If Pulumi detects that you are trying to `destroy` a stack that contains hooks _without_ the `--run-program` flag, it will fail with an error.
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/ignorechanges.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: ignoreChanges
parent: options-concepts
weight: 7
weight: 8
aliases:
- /docs/intro/concepts/resources/options/ignorechanges/
- /docs/concepts/options/ignorechanges/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: import
parent: options-concepts
weight: 8
weight: 9
aliases:
- /docs/intro/concepts/resources/options/import/
- /docs/concepts/options/import/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/parent.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: parent
parent: options-concepts
weight: 9
weight: 10
aliases:
- /docs/intro/concepts/resources/options/parent/
- /docs/concepts/options/parent/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/protect.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: protect
parent: options-concepts
weight: 10
weight: 11
aliases:
- /docs/intro/concepts/resources/options/protect/
- /docs/concepts/options/protect/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: provider
parent: options-concepts
weight: 11
weight: 12
aliases:
- /docs/concepts/resources/options/provider/
- /docs/concepts/options/provider/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: providers
parent: options-concepts
weight: 12
weight: 13
aliases:
- /docs/concepts/resources/options/providers/
- /docs/concepts/options/providers/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/replaceonchanges.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: replaceOnChanges
parent: options-concepts
weight: 13
weight: 14
aliases:
- /docs/intro/concepts/resources/options/replaceonchanges/
- /docs/concepts/options/replaceonchanges/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/retainOnDelete.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: retainOnDelete
parent: options-concepts
weight: 14
weight: 15
aliases:
- /docs/intro/concepts/resources/options/retainondelete/
- /docs/concepts/options/retainondelete/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/transformations.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: transformations
parent: options-concepts
weight: 15
weight: 16
aliases:
- /docs/intro/concepts/resources/options/transformations/
- /docs/concepts/options/transformations/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: transforms
parent: options-concepts
weight: 15
weight: 16
aliases:
- /docs/intro/concepts/resources/options/transforms/
- /docs/concepts/options/transforms/
Expand Down
2 changes: 1 addition & 1 deletion content/docs/iac/concepts/options/version.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu:
iac:
identifier: version
parent: options-concepts
weight: 15
weight: 17
aliases:
- /docs/intro/concepts/resources/options/version/
- /docs/concepts/options/version/
Expand Down
Loading