Pages

Wednesday, April 17, 2024

Demystifying Terraform: What Happens When You Run terraform init and terraform apply

Demystifying Terraform: What Happens When You Run terraform init and terraform apply


When you run terraform init and terraform apply, here's what happens internally:

1. terraform init:

  • Initializes a Terraform working directory by scanning the configuration files in the directory.
  • Installs necessary providers specified in the configuration, downloading the appropriate provider plugins required to interact with specified resources.
  • Prepares backend: If specified, it initializes the backend used for state locking and storing the state file.
  • Modules Installation: Downloads and installs any modules referenced in the configuration.

2. terraform apply:

  • Load Configuration: Reads the Terraform configuration files to understand the desired state of the resources.
  • Refresh State: Updates the local state file against real-world resources, ensuring it matches the latest status known to Terraform.
  • Plan Execution: Creates an execution plan to determine what actions are necessary to achieve the desired state specified in the configuration.
  • Resource Management: Executes the plan to create, update, or delete resources as necessary to match the desired state.
  • State Update: Updates the state file to reflect the changes made and outputs the final state of resources.

These commands are foundational to managing infrastructure with Terraform, setting up the environment and applying configurations to manage resources effectively.

No comments:

Post a Comment