In this post, I will explain what the terraform apply
command is used for, what it does, the available options, and when to run it.
The terraform apply
command executes the actions proposed in a terraform plan
. It is used to deploy your infrastructure. Typically apply
should be run after terraform init
and terraform plan
.
If the apply
command is run without any options it will run a terraform plan
first, ask the user to confirm the planned actions, and then execute those changes once confirmed.
The apply
command can also be used with a previously generated planfile, from the terraform plan -out=<path to file>
.
terraform apply
— Creates or updates infrastructure depending on the configuration files. By default, a plan will be generated first and will need to be approved before it is applied.
terraform apply -auto-approve
— Apply changes without having to interactively type ‘yes’ to the plan. Useful in automation CI/CD pipelines.
terraform apply <planfilename>
— Provide the file generated using the terraform plan -out
command. If provided, Terraform will take the actions in the plan without any confirmation prompts.
terraform apply -var="my_variable=test"
— Pass in a variable value.
terraform apply -var-file="varfile.tfvars"
— Pass in variables contained in a file.
terraform apply -target=”module.appgw.0"
— Apply changes only to the targeted resource. Read more about Terraform target option.
terraform apply -lock=false
— Do not hold a state lock during the Terraform apply operation. Use with caution if other engineers might run concurrent commands against the same workspace.
terraform apply -parallelism=<n>
— Specify the number of operations run in parallel.
If there are no changes in the configuration files compared to the current Terraform state, then no changes will be made to the infrastructure. Terraform is a declarative language, so it is absolutely safe to run the apply
command multiple times.
In automation terraform apply
can be run after the plan stage, passing in the plan output file. If there is no plan stage prior to the apply stage (not recommended), then the terraform apply -auto-approve
option can be used. Another useful option to avoid encoding issues when running pipelines in some CI/CD systems, i.e. Azure DevOps, is the terraform apply -no-color
option, as the colored output is not handled correctly by the build agents.
A more robust approach to automating your Terraform workflows end-to-end would be to use Spacelift, a collaborative infrastructure delivery tool. Spacelift provides a more mature way of automating the whole infrastructure provisioning lifecycle. Its flexible and robust workflow allows teams to get up to speed quickly and collaborate efficiently. Check out the Documentation and start automating your infrastructure delivery easily!
terraform apply
is a core command in the Terraform workflow, used for deploying infrastructure as described in your configuration files.
Manage Terraform Better and Faster
If you are struggling with Terraform automation and management, check out Spacelift. It helps you manage Terraform state, build more complex workflows, and adds several must-have capabilities for end-to-end infrastructure management.
Terraform CLI Commands Cheatsheet
Initialize/ plan/ apply your IaC, manage modules, state, and more.