Actions and query workflows
Three operations, three intentions
A managed resource describes an object whose lifecycle Terraform tracks. A provider action describes an operation exposed by a provider, for example invoking a maintenance task. A query discovers existing infrastructure. The fact that an action does not itself update managed resource state does not mean it is harmless: a remote task could modify data, send notifications or incur costs. Use the following fictional action declaration only to recognize the grammar. The example provider and operation do not exist as a tested dependency.
action "example_notify" "release" { config { message = "catalogue release reviewed" }}The type and config schema are provider-defined. Check the exact release, documentation and permission requirements before selecting any action. Terraform 1.14 introduced the actions workflow; the inherited provisional 1.14.8 baseline has not been executed.
Explicit invocation versus lifecycle trigger
The documented CLI uses -invoke with a specific action address for action-only planning and application in a supported conventional workflow. Do not run these illustrative commands; the action is fictional.
terraform plan -invoke=action.example_notify.releaseterraform apply -invoke=action.example_notify.releaseFor a resource-associated action, a lifecycle trigger selects events and action references. This incomplete static example would belong within a provider resource, not at top level.
# Inside a hypothetical resource blocklifecycle { action_trigger { events = [after_create] actions = [action.example_notify.release] }}The trigger runs during apply at the configured event, not simply when a configuration is parsed. The current docs do not support direct local invocation for Stacks; they describe lifecycle triggers in the resource configuration. Check support for the exact provider, runtime, events, condition and failure semantics. A retry may repeat an external operation; investigate the actual outcome before retrying. Do not treat an action as a pure function or an atomic transaction.
Query existing infrastructure
A .tfquery.hcl file uses list blocks for discovering provider-defined resource types. The following fictional query sets a provider configuration, a result bound and a provider-specific filter. It is not a working provider schema.
list "example_catalogue" "candidates" { provider = example limit = 10 config { environment = "test" }}The terraform query command retrieves discovered identities, with optional additional information depending on the provider and configuration. It may read sensitive metadata, use credentials, consume API quota or cost money; a result limit is not a complete safety boundary. Do not run this against unknown accounts. Keep query syntax separate from ordinary .tf resource configuration and Stack configuration.
Bulk import is a separate ownership decision
Discovery alone does not attach an object to Terraform state. Supported query workflows can generate candidate resource and import blocks. Review the generated configuration before moving it into the target root module and planning an import. Consider provider identity, intended account and state ownership, required arguments and any extra create/update/delete actions.
Fictional discovery resultscat-01: no existing owner, approved test target -> review candidatecat-02: managed in another state -> HOLDcat-03: target account unknown -> HOLDNever import an object already managed by another state without a coordinated transfer. The generation step does not authorize applying its result. Protect query output and candidate configuration as potentially sensitive. Inspect the post-import plan for unexpected change; successful import does not demonstrate application health.
Build an approval boundary
For an action, review intended effect, target, credentials, retry behavior, external verification and authorization. For query/import, distinguish permission to discover from permission to manage. Require separate approval of the import plan and ownership evidence. For either workflow, missing information means hold. The training gate is a simplified policy, not actual HCP behavior.
Fictional review: inventory query -> inspect identities -> generate candidate configuration -> confirm ownership -> reviewed import plan -> authorized applyAction review: intended operation -> target -> permissions -> retry safety -> approval -> invocation -> external outcome verificationThe offline cases model these decisions without invoking a provider. Exact patch support, schema, licensing and actual hosted operation remain unverified.
Practice and next step
Classify the cases as eligible for human review or hold, and explain why a successful query is not an import. Reference: terraform-concept-actions-and-query-workflows. Read https://developer.hashicorp.com/terraform/language/invoke-actions and https://developer.hashicorp.com/terraform/language/import/bulk. Next: OpenTofu compatibility and licensing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Actions and query workflows — knowledge check
Review Stack deployments, actions and discovery decisions.
Why can an action with no managed state change still be risky?
Where does action_trigger appear?
Which grammar defines list queries?
Does discovering a resource automatically import it?
An action times out after sending a request. What is the next step?