Terraform Apply

Once you’ve reviewed and approved your Terraform plan, the next step is to deploy those changes. The terraform apply command takes the execution plan and makes it a reality, creating, updating, or destroying resources as defined in your configuration files. This command is where the magic happens, turning your infrastructure code into actual cloud resources.

Why Use terraform apply?

  • Deploys the infrastructure defined in your configuration files.
  • Ensures changes align with your approved plan.
  • Offers options for automation and fine-tuning during execution.

How to Use It?

  • To apply changes interactively: Run terraform apply. Terraform will prompt you to confirm the deployment after displaying the proposed changes.
  • To skip the confirmation step: Use the -auto-approve flag, e.g., terraform apply -auto-approve.
  • To apply a saved plan file: Use terraform apply myplan.tfplan, where myplan.tfplan is the plan file you saved using terraform plan -out.

Example: Deploying Infrastructure

Imagine you’ve used terraform plan to generate a plan for an EC2 instance and saved it to a file called example.tfplan. To deploy this infrastructure, run:

terraform apply example.tfplan

If you didn’t save a plan file, you can directly run terraform apply, and Terraform will display the changes. For example:

Terraform will perform the following actions:

  # aws_instance.example will be created
  + resource "aws_instance" "example" {
      + ami           = "ami-123456"
      + instance_type = "t2.micro"
      ...
    }

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: 

After typing yes, Terraform will execute the plan and deploy your infrastructure.

Use Case

Suppose you’re managing a CI/CD pipeline that needs to deploy infrastructure without manual intervention. By using terraform apply -auto-approve, you can automate the deployment process while ensuring the plan aligns with pre-approved changes.

Conclusion

The terraform apply command bridges the gap between planning and execution, making it the cornerstone of Terraform workflows. Whether deploying interactively or through automation, this command transforms your infrastructure code into a live environment.