Consider that under terraform folder, there are dev/ prod/ global/. So how many times do we need to run terraform init
?
Based on the transcript, you have the following folders under your terraform folder:
- dev/
- prod/
- global/
You also have a V2 folder that represents a single Git repository, which contains subfolders for environments (dev, prod, global) and modules.
To answer your question, you would need to run terraform init
in each environment folder (dev, prod, global) because each environment folder has its own Terraform state file.
However, in the V2 folder, you have a more advanced structure where each module is in its own Git repository. In this case, you would need to run terraform init
in each environment folder (dev, prod, global) as well as in each module repository.
So, in total, you would need to run terraform init
at least 5 times:
- 3 times in the environment folders (dev, prod, global)
- 2 times in the module repositories (VPC and subnet)
But, if you have more modules, the number of terraform init
commands would increase accordingly.
Essential Terraform Commands for Senior Data Engineers
terraform init
Description: Initializes a Terraform configuration by setting up the backend and downloading required provider plugins.
Use Case: Run this command when starting a new Terraform project or re-initializing an existing one after changes to
provider
orbackend
configurations.Example:
terraform validate
Description: Validates the syntax and structure of Terraform configuration files.
Use Case: Use this command to check for syntax errors before applying changes.
Example:
terraform plan
Description: Generates an execution plan, showing the resources to be added, changed, or destroyed.
Use Case: Use this to preview changes before applying them to ensure correctness and avoid unintended resource modifications.
Example:
terraform plan -var-file="prod.tfvars"
terraform apply
Description: Applies the changes required to reach the desired state of the configuration.
Use Case: Use this after reviewing the execution plan to create or modify infrastructure.
Example:
terraform apply -var-file="prod.tfvars"
terraform destroy
Description: Destroys all resources managed by the Terraform configuration.
Use Case: Use this to clean up resources at the end of a project or when decommissioning infrastructure.
Example:
terraform destroy -var-file="prod.tfvars"
terraform refresh
Description: Updates the state file with the real-world status of resources.
Use Case: Use this to ensure the state file is in sync with the actual infrastructure.
Example:
terraform output
Description: Displays the output values from the state file.
Use Case: Use this to view specific resource properties or pass outputs to other tools/scripts.
Example:
terraform show
Description: Displays the current state or a saved execution plan.
Use Case: Use this to inspect resources in the state file or review a saved plan.
Example:
terraform state
Description: A suite of subcommands for managing the Terraform state file.
Use Case: Use this to move, remove, or inspect resources in the state file.
Examples:
terraform import
Description: Imports existing infrastructure into Terraform's management.
Use Case: Use this to bring manually-created or pre-existing resources under Terraform management.
Example:
terraform taint
Description: Marks a resource for recreation during the next
terraform apply
.Use Case: Use this to force the recreation of a resource after detecting a configuration drift or issue.
Example:
terraform untaint
Description: Removes the taint from a resource, preventing its recreation.
Use Case: Use this if you marked a resource as tainted but decide not to recreate it.
Example:
terraform fmt
Description: Formats Terraform configuration files to follow the standard style.
Use Case: Use this to clean up and standardize configuration file formatting.
Example:
terraform workspace
Description: Manages multiple workspaces (environments) in a single Terraform configuration.
Use Case: Use this to manage environments like
dev
,staging
, andprod
.Examples:
terraform graph
Description: Generates a visual representation of the Terraform configuration.
Use Case: Use this to visualize resource dependencies in the infrastructure.
Example:
terraform login
Description: Authenticates Terraform with Terraform Cloud or Enterprise.
Use Case: Use this to set up authentication for remote state management and collaboration.
Example:
terraform providers
Description: Displays the providers used in the current configuration.
Use Case: Use this to inspect provider dependencies and versions.
Example:
terraform state pull
andterraform state push
Description: Pulls or pushes the Terraform state file from or to the remote backend.
Use Case: Use pull to debug the latest state, and push to overwrite the state in the backend.
Examples:
terraform state pull > state.tfstate
terraform debug
Description: Enables detailed logging for troubleshooting Terraform issues.
Use Case: Use this to diagnose errors in Terraform runs and configurations.
Example:
terraform force-unlock
Description: Manually removes a lock on the state file.
Use Case: Use this to unlock the state file if a Terraform run is interrupted or stuck.
Example:
These commands cover the most critical aspects of Terraform usage for managing infrastructure, debugging issues, and maintaining best practices.