Learn Terraform CLI Commands

Terraform Initialization and Setup

  1. terraform init
    • Purpose: Prepares the working directory containing Terraform configuration files, downloading necessary provider plugins and setting up the backend for storing the state.
    • Example: To initialize your Terraform project, run terraform init in the directory where your configuration files are located.
  2. terraform fmt
    • Purpose: Ensures that Terraform configuration files are formatted according to standard conventions.
    • Example: Use terraform fmt to reformat all .tf files in your directory.
  3. terraform validate
    • Purpose: Checks the Terraform configuration for syntax errors and logical inconsistencies.
    • Example: Execute terraform validate to confirm that your configuration files are correctly written.
  4. terraform version
    • Purpose: Displays the version of Terraform installed, along with versions of any installed provider plugins.
    • Example: Run terraform version to see the version of Terraform you’re currently using.

Terraform Planning and Execution

  1. terraform plan
    • Purpose: Generates an execution plan, outlining the changes Terraform will make to your infrastructure to match the desired state.
    • Example: Run terraform plan to preview the actions Terraform will take before actually applying them.
  2. terraform apply
    • Purpose: Executes the actions required to bring your infrastructure into the desired state as defined in your configuration files.
    • Example: Use terraform apply to create or modify infrastructure according to the plan.
  3. terraform apply -auto-approve
    • Purpose: Applies the configuration changes automatically without requiring manual approval.
    • Example: Run terraform apply -auto-approve when you need to apply changes in automated scripts without user intervention.
  4. terraform refresh
    • Purpose: Synchronizes the state file with the actual state of the infrastructure, ensuring that Terraform has the latest information.
    • Example: Run terraform refresh to update the state file before making further changes.
  5. terraform destroy
    • Purpose: Destroys all resources managed by Terraform, effectively reversing the apply process.
    • Example: Use terraform destroy to clean up and remove all infrastructure defined in your configuration.
  6. terraform destroy -auto-approve
    • Purpose: Destroys resources without asking for confirmation, useful for automation scenarios.
    • Example: Run terraform destroy -auto-approve when you want to remove resources automatically.

Terraform Statefile Management

  1. terraform state list
    • Purpose: Displays a list of all resources currently tracked by Terraform in the state file.
    • Example: Use terraform state list to see all the infrastructure components managed by Terraform.
  2. terraform state show
    • Purpose: Provides detailed information about a specific resource in the state file.
    • Example: Run terraform state show aws_instance.my_instance to view the properties of a specific resource.
  3. terraform state rm
    • Purpose: Removes a resource from the Terraform state file without deleting the actual resource.
    • Example: Use terraform state rm aws_instance.my_instance to stop tracking a resource.
  4. terraform state mv
    • Purpose: Moves a resource to a different name or module within the state file.
    • Example: Run terraform state mv aws_instance.my_instance aws_instance.new_name to rename a resource in the state.
  5. terraform taint
    • Purpose: Marks a resource as needing to be destroyed and recreated on the next terraform apply.
    • Example: Use terraform taint aws_instance.my_instance to force the recreation of a specific resource.
  6. terraform untaint
    • Purpose: Removes the taint from a resource, preventing its recreation.
    • Example: Run terraform untaint aws_instance.my_instance to cancel the taint on a resource.
  7. terraform import
    • Purpose: Brings an existing resource into Terraform management by adding it to the state file.
    • Example: Use terraform import aws_instance.my_instance i-1234567890abcdef0 to import an existing AWS instance.

Terraform Modules and Workspaces

  1. terraform get
    • Purpose: Fetches and updates the modules required for the Terraform configuration.
    • Example: Run terraform get after adding new modules to ensure they are properly downloaded and available.
  2. terraform workspace list
    • Purpose: Lists all available workspaces within the current Terraform project.
    • Example: Use terraform workspace list to see the different workspaces you’ve created.
  3. terraform workspace new
    • Purpose: Creates a new workspace, allowing you to manage different states for different environments.
    • Example: Run terraform workspace new production to create a new workspace called production.
  4. terraform workspace select
    • Purpose: Switches the current Terraform environment to a different workspace.
    • Example: Use terraform workspace select production to work in the production workspace.
  5. terraform workspace delete
    • Purpose: Deletes a workspace that is no longer needed.
    • Example: Run terraform workspace delete production to remove the production workspace.

Terraform File and Configuration Management

  1. terraform output
    • Purpose: Displays the output values defined in the Terraform configuration.
    • Example: Use terraform output to view the values of outputs like IP addresses or URLs that are configured for retrieval after an apply operation.