Terraform Commands

Terraform Commands

ยท

3 min read

Table of contents

What is Terraform?

Terraform is an open-source tool that lets you define and automate your infrastructure using code. You write configurations to describe what resources you want (like servers, databases, or networks), and Terraform handles the process of creating and managing those resources across various cloud providers and platforms. It makes infrastructure management more efficient and consistent.

Task 1:

Terraform is an infrastructure-as-code (IAC) tool used for building, changing, and versioning infrastructure efficiently. It has several commands to manage your infrastructure code and the resources it provisions.

Here are some commonly used Terraform commands:

  1. Command: init
terraform init

terraform init initializes a Terraform project. It downloads necessary plugins, configures backends, and prepares your project for infrastructure management. It's the first step when working with Terrafor

  1. Command: init -upgrade
terraform init -upgrade

terraform init -upgrade updates your provider plugins to their latest versions while initializing your Terraform project. It keeps your project current and takes advantage of the latest features and bug fixes.

  1. Command: plan
terraform plan
  • Generates a execution plan description of what Terraform will do to reach the desired state define your configuration. It shows which resources will be created, updated, or destroyed.
  1. Command: Apply
terraform apply
  • Applies the changes the defined in your Terraform configuration to create or update resources based on the execution plan. It prompts for confirmation before making changes.
  1. Command: validate
terraform validate
  • Checks the syntax and configuration of your Terraform files for errors without actually creating or modifying resources.
  1. Command: fmt
terraform fmt
  • Rewrites Terraform configuration files to a canonical format and style. This applies standard formatting conventions to the code to make it easier to read and understand.
  1. Command: Destroy
terraform destroy
  • Destroys are all resources defined in your configuration, effectively tearing down your infrastructure. It also prompts for confirmation before proceeding.
  1. Command: show
terraform show
  • Inspects the current state of the infrastructure and outputs the resource information.
  1. Command: Graph
terraform graph
  • terraform graph: Creates a visual representation of the Terraform resources defined in your configuration. It outputs the generated graph in the DOT format.
  1. Command: terraform import
terraform import

terraform import is used to bring existing infrastructure resources under Terraform's control. It maps existing resources to Terraform configurations, enabling you to manage them alongside other infrastructure as code.

  1. Command: Output
terraform output
  • Reads the state file and outputs the value of a specified output variable. This is useful for fetching the value of a specific variable.
  1. Command: Provider
terraform providers

terraform providers lists the available provider plugins in your Terraform configuration, showing the configured providers and their versions. It helps confirm the provider setups in your project.

  1. Command: Workspace
terraform workspace
  • Used to create, list, and delete Terraform workspaces. Workspaces allow you to manage multiple distinct sets of infrastructure resources with the same configuration files.

    1. Command: Refresh
terraform refresh

terraform refresh updates the Terraform state file to match the current state of your infrastructure resources by querying their actual status, without making any changes to the resources themselves.

  1. Command: version
terraform version

terraform version shows the currently installed Terraform version, helping you confirm the version you are working with.

ย