liveBook · Manning

Terraform apply

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

terraform apply is the Terraform command that executes the actions specified by an execution plan.

Use `terraform apply` when a configuration is ready to become infrastructure. Terraform executes the actions in an execution plan, walking its dependency graph to bring resources into line with the configuration. It is the deployment step in the usual `init`, `plan`, `apply` workflow. section 5.5 section 1.7

Choose how the plan reaches `apply`:

  • Create a plan and pass the saved plan file to the apply command. section 5.5
  • Run `terraform apply` without a plan file to generate a plan in the same command. Terraform asks for confirmation by default, leaving a chance to review changes. This mode also accepts planning options, including variable inputs, resource targeting, destroy, and refresh-only. section 5.5.1

Apply a local module in a test workspace

A workspace can call a local module, look up the default AWS VPC and one of its subnets, and pass the selected subnet ID into the module. Run `terraform init` in that workspace, then `terraform apply`; review the proposed changes before confirming. The workspace can expose a module output such as the resulting instance ARN. section 3.8.4

Listing 3.36 Testing our local module
data "aws_vpc" "default" {                                   #1
  default = true                                             #1
}                                                            #1
 
 
data "aws_subnets" "default" {                               #1
  filter {                                                   #1
    name   = "vpc-id"                                        #1
    values = [data.aws_vpc.default.id]                       #1
  }                                                          #1
}                                                            #1
 
module "test_instance" {
  source    = "../"
  subnet_id = data.aws_subnets.default.ids[0]                #2
}
 
output "aws_instance_arn" {                                  #3
  value = module.test_instance.aws_instance_arn
}

The configuration shows the data lookups, relative module source, subnet argument, and output used by the test workspace. section 3.8.4

Avoid `-auto-approve` merely to skip review: without a saved plan file, `apply` creates a plan and immediately acts on it. Even a preceding, separate `terraform plan` may no longer describe the same situation if changes occurred outside the workflow. For automation, `apply -json` emits one JSON event per line as work progresses, so a wrapper can stream messages and handle warnings or errors rather than waiting for one final result. section 5.5.1 section 11.1.7

An apply also matters in Terraform tests: a `run` block applies by default. Switching it to `command = plan` can make assertions fail when they depend on output values unavailable until after application. section 9.4.1

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains saved-plan and combined-command workflows, approval choices, and streamed output; stops short of detailing how individual provider actions execute.
Examples
Uses an AWS module test workspace, local TLS resource creation, and a Terraform test assertion dependent on an applied output.
What it assumes
Assumes a Terraform configuration and introduces the `init`, `plan`, `apply` sequence before using a module workspace.
What you can do after
Operate/configure it to review and apply infrastructure changes, or stream apply events through an automation wrapper.
How it connects
Connects apply to execution plans, dependency graphs, modules, resource targeting, and Terraform tests.

Also explained in

Compare how 2 books explain terraform apply →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.