Skip to main content

Synopsis

Create or update infrastructure

Description

The terraform apply command creates or updates infrastructure according to Terraform configuration files in the current directory. By default, Terraform will generate a new plan and present it for your approval before taking any action. You can optionally provide a plan file created by a previous call to terraform plan, in which case Terraform will take the actions described in that plan without any confirmation prompt.

Usage

terraform apply [options] [plan_file]

Arguments

plan_file
string
Optional path to a saved plan file created by terraform plan -out=FILE. If provided, Terraform will apply exactly the changes described in that plan without generating a new plan or prompting for approval.

Options

Approval Options

-auto-approve
boolean
default:"false"
Skip interactive approval of plan before applying.
-input
boolean
default:"true"
Ask for input for variables if not directly set.

Plan Mode Options

If you don’t provide a saved plan file, this command also accepts all of the plan-customization options from terraform plan:
-destroy
boolean
default:"false"
Destroy Terraform-managed infrastructure. The command terraform destroy is a convenience alias for this option.
-refresh-only
boolean
default:"false"
Select the “refresh only” mode, which only updates the state to match remote objects without proposing any changes.
-refresh
boolean
default:"true"
Update the state prior to checking for differences. Set to false to skip refreshing.
-replace
string
Force replacement of a particular resource instance using its resource address. Terraform will plan to replace this resource instance instead of updating it. Can be used multiple times.
-target
string
Limit the apply operation to only the given module, resource, or resource instance and all of its dependencies. Can be used multiple times. This is for exceptional use only.

Variable Options

-var
string
Set a value for one of the input variables in the root module of the configuration. Use this option more than once to set more than one variable. Format: -var 'name=value'
-var-file
string
Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.

State Management

-lock
boolean
default:"true"
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
default:"0s"
Duration to retry a state lock. For example: ”10s” for 10 seconds.
-state
string
Path to read and save state (unless state-out is specified). Defaults to “terraform.tfstate”. Legacy option for the local backend only.
-state-out
string
Path to write state to that is different than “-state”. This can be used to preserve the old state. Legacy option for the local backend only.
-backup
string
Path to backup the existing state file before modifying. Defaults to the “-state-out” path with “.backup” extension. Set to ”-” to disable backup.

Performance Options

-parallelism
number
default:"10"
Limit the number of parallel resource operations.

Output Options

-compact-warnings
boolean
default:"false"
If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.
-json
boolean
default:"false"
Produce output in a machine-readable JSON format. When using this flag with a plan file argument, you must also specify -auto-approve.
-no-color
boolean
default:"false"
If specified, output won’t contain any color.

Examples

Basic Apply

Apply changes with interactive approval:
$ terraform apply

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.example will be created
  + resource "aws_instance" "example" {
      + ami                          = "ami-0c55b159cbfafe1f0"
      + instance_type                = "t2.micro"
      + id                           = (known after apply)
      ...
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.example: Creating...
aws_instance.example: Creation complete after 45s [id=i-1234567890abcdef0]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Auto-Approve

Apply changes without interactive approval:
$ terraform apply -auto-approve

aws_instance.example: Creating...
aws_instance.example: Creation complete after 45s [id=i-1234567890abcdef0]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Apply Saved Plan

Apply a saved plan file:
$ terraform apply "tfplan"

aws_instance.example: Creating...
aws_instance.example: Creation complete after 45s [id=i-1234567890abcdef0]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Apply with Variables

Apply with variable values:
$ terraform apply -var="instance_type=t3.small" -var-file="prod.tfvars"

Target Specific Resources

Apply changes to specific resources only:
$ terraform apply -target=aws_instance.example -auto-approve

aws_instance.example: Creating...
aws_instance.example: Creation complete after 45s

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Force Replacement

Force replacement of a resource:
$ terraform apply -replace=aws_instance.example -auto-approve

aws_instance.example: Destroying... [id=i-1234567890abcdef0]
aws_instance.example: Destruction complete after 30s
aws_instance.example: Creating...
aws_instance.example: Creation complete after 45s [id=i-0987654321fedcba0]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

Apply with Outputs

Example showing outputs after apply:
$ terraform apply -auto-approve

aws_instance.example: Creating...
aws_instance.example: Creation complete after 45s [id=i-1234567890abcdef0]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

instance_id = "i-1234567890abcdef0"
instance_public_ip = "54.123.45.67"

Exit Codes

  • 0 - Success
  • 1 - Error occurred

Build docs developers (and LLMs) love