Files
opentf/website/docs/cli/commands/apply.mdx
Christian Mesh c8b58e949b Address review comments
Co-authored-by: James Humphries <James@james-humphries.co.uk>
Co-authored-by: Ilia Gogotchuri <ilia.gogotchuri0@gmail.com>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2025-09-10 07:45:23 -04:00

140 lines
7.0 KiB
Plaintext

---
description: >-
The tofu apply command executes the actions proposed in a OpenTofu plan
to create, update, or destroy infrastructure.
---
# Command: apply
The `tofu apply` command executes the actions proposed in a OpenTofu
plan.
## Usage
Usage: `tofu apply [options] [plan file]`
### Automatic Plan Mode
When you run `tofu apply` without passing a saved plan file, OpenTofu automatically creates a new execution plan as if you had run [`tofu plan`](plan.mdx), prompts you to approve that plan, and takes the indicated actions. You can use all of the [planning modes](plan.mdx#planning-modes) and
[planning options](plan.mdx#planning-options) to customize how OpenTofu will create the plan.
You can pass the `-auto-approve` option to instruct OpenTofu to apply the plan without asking for confirmation.
:::danger Warning
If you use `-auto-approve`, we recommend making sure that no one can change your infrastructure outside of your OpenTofu workflow. This minimizes the risk of unpredictable changes and configuration drift.
:::
### Saved Plan Mode
When you pass a [saved plan file](plan.mdx#out-filename) to `tofu apply`, OpenTofu takes the actions in the saved plan without prompting you for confirmation. You may want to use this two-step workflow when running OpenTofu in automation.
Use [`tofu show`](show.mdx) to inspect a saved plan file before applying it.
When using a saved plan, you cannot specify any additional planning modes or options. These options only affect OpenTofu's decisions about which
actions to take, and the plan file contains the final results of those
decisions.
#### Ephemeral variables
Since ephemeral variables can't be stored in a planfile, any ephemeral variables set during the generation of a planfile from `tofu plan` must also be set when running tofu apply.
For example, if `-var` has been used to provide an ephemeral variable `token` to create the planfile (`tofu plan -var "token=$MY_TOKEN"`), the same argument _must_
be set for the apply command (`tofu apply -var "token=$MY_TOKEN" planfile`).
If required ephemeral variables are not provided to the apply command, OpenTofu will exit with a clear message of additional variables that must be specified.
### Plan Options
Without a saved plan file, `tofu apply` supports all planning modes and planning options available for `tofu plan`.
- **[Planning Modes](plan.mdx#planning-modes):** These include `-destroy`, which creates a plan to destroy all remote objects, and `-refresh-only`, which creates a plan to update OpenTofu state and root module output values.
- **[Planning Options](plan.mdx#planning-options):** These include specifying which resource instances OpenTofu should replace, setting OpenTofu input variables, etc.
### Apply Options
The following options change how the apply command executes and reports on the apply operation.
- `-auto-approve` - Skips interactive approval of plan before applying. This
option is ignored when you pass a previously-saved plan file, because
OpenTofu considers you passing the plan file as the approval and so
will never prompt in that case.
- `-compact-warnings` - Shows any warning messages in a compact form which
includes only the summary messages, unless the warnings are accompanied by
at least one error and thus the warning text might be useful context for
the errors.
- `-consolidate-warnings=false` - If OpenTofu produces any warnings, no
consolidation will be performed. All locations, for all warnings will
be listed. Enabled by default.
- `-consolidate-errors` - If OpenTofu produces any errors, attempt to
consolidate similar messages into a single item.
- `-input=false` - Disables all of OpenTofu's interactive prompts. Note that
this also prevents OpenTofu from prompting for interactive approval of a
plan, so OpenTofu will conservatively assume that you do not wish to
apply the plan, causing the operation to fail.
- `-json` - Enables the [machine readable JSON UI](../../internals/machine-readable-ui.mdx) output.
This implies `-input=false`, so the configuration must have no unassigned
variable values to continue. To enable this flag, you must also either enable
the `-auto-approve` flag or specify a previously-saved plan.
- `-lock=false` - Don't hold a state lock during the operation. This is
dangerous if others might concurrently run commands against the same
workspace.
- `-lock-timeout=DURATION` - Unless locking is disabled with `-lock=false`,
instructs OpenTofu to retry acquiring a lock for a period of time before
returning an error. The duration syntax is a number followed by a time
unit letter, such as "3s" for three seconds.
- `-no-color` - Disables terminal formatting sequences in the output. Use this
if you are running OpenTofu in a context where its output will be
rendered by a system that cannot interpret terminal formatting.
- `-concise` - Disables progress-related messages in the output.
- `-parallelism=n` - Limit the number of concurrent operation as OpenTofu
[walks the graph](../../internals/graph.mdx#walking-the-graph). Defaults to
10\.
- `-var 'foo=bar'` - Set a variable in the OpenTofu configuration.
This flag can be set multiple times.
- `-var-file=foo` - Set variables in the OpenTofu configuration from a file.
If "terraform.tfvars" or any ".auto.tfvars" files are present, they will
be automatically loaded.
- `-show-sensitive` - If specified, sensitive values will not be
redacted in te UI output.
- `-deprecation` - Specify what type of warnings are shown.
Accepted values: "module:all", "module:local", "module:none". Default: module:all. When "module:all" is selected,
OpenTofu will show the deprecation warnings for all modules. When "module:local" is selected,
the warnings will be shown only for the modules that are imported with a relative
path. When "module:none" is selected, all the deprecation warnings will be dropped.
- All [planning modes](plan.mdx#planning-modes) and
[planning options](plan.mdx#planning-options) for
`tofu plan` - Customize how OpenTofu will create the plan. Only available when you run `tofu apply` without a saved plan file.
For configurations using
[the `local` backend](../../language/settings/backends/local.mdx) only,
`tofu apply` also accepts the legacy options
[`-state`, `-state-out`, and `-backup`](../../language/settings/backends/local.mdx#command-line-arguments).
### Environment variables
You can further customize behavior of `apply` command by using [environment variables](../config/environment-variables.mdx). For example, the [TF_STATE_PERSIST_INTERVAL](../config/environment-variables.mdx#tf_state_persist_interval) environment variable allows to specify the interval between state persistence.
## Passing a Different Configuration Directory
If your workflow relies on overriding the root module directory, use
[the `-chdir` global option](index.mdx#switching-working-directory-with-chdir)
instead, which works across all commands and makes OpenTofu consistently look
in the given directory for all files it would normally read or write in the
current working directory.