Untitled
unknown
plain_text
18 days ago
4.1 kB
3
Indexable
## Terraform init - Terraform init command is used to initialize a working directory, downloads the necessary provider plugins and module, setting up the backend for storing infrastructure's state. - Must run terraform init after changing provider, module or backend configuration. - Terraform init create: - **.terraform directory** -> Terraform uses this directory to managed cached provider plugins and modules. -> Not committed to version control systems as it contains machine-specific details and can be created by running terraform init. - **.terraform.lock.hcl** (dependency lock file): - Providers and Modules - both of these dependency types can be published and updated independently from Terraform itself and from the configurations that depend on them. For that reason, Terraform must determine which versions of those dependencies are potentially compatible with the current configuration and which versions are currently selected for use. - Version constraints within the configuration itself determine which versions of dependencies are potentially compatible, but after selecting a specific version of each dependency, Terraform remembers and write it to dependency lock file -> so that it can make the same decisions again in future (by default). - Should commit this file to version control system to ensure that when we run terraform init again in future, Terraform will select exactly the same provider versions. -> Use -upgrade option if we want Terraform to ignore the dependency lock file and consider installing newer versions. ### Provider/Module - Selecting Plugins: Terraform considers both the **version constraints in the configuration** (terraform.tf) and the **version selections recorded in the lock file** (.terraform.lock.hcl if exists) -> chooses a version for each plugin: - If any acceptable versions are installed -> Terraform use the newest **installed** version that meets the constraint. - If no acceptable versions are installed and the plugin is one of the providers distributed by HashiCorp -> Terraform downloads the newest **acceptable** version from Terraform Registry. - If no acceptable versions are installed and the plugin is not distributed in the Terraform Registry -> **init fails** and the user must manually install an appropriate version. - Upgrading Plugins: - When run `terraform init -upgrade`, it re-checks the Terraform Registry for newer acceptable versions and downloads them if available. ### Backend - During init, the root configuration directory is consulted for backend configuration and the chosen backend is initialized using the given configuration settings. - By default, Terraform uses a local backend and saves its state file to a terraform.tfstate file located in the working directory. - Re-running init with an already-initialized backend will update the working directory to use the new backend settings: - -migrate-state: copy existing state to the new backend. - -reconfigure: disregards any existing configuration, preventing migration of any existing state. ## Terraform validate - Terraform validate command validates the configuration files in a directory, referring only to the Terraform configuration files. - False positives -> terraform validate reports successfully but our plan/apply may still fail. - JSON validation output -> produce the output of a terraform validate in JSON format. ## Terraform plan + apply + destroy - Steps: - Update the state from the previous run to reflect any changes made outside of Terraform -> that's called "**refreshing**" in Terraform terminology. - Comparing that updated state with the desired state described by the configuration. In case of any differences -> generating a proposed set of actions to change the real remote objects to match the desired state. - refresh-only -> disable the second step, but still performing the first -> Terraform will update the state to match changes made outside of Terraform. - Actions: - + resource will be created - - resource will be destroyed - ~ resource will be updated in-place - -/+ resource will be destroyed and re-created.
Editor is loading...
Leave a Comment