Introduction

Terraform, an open-source infrastructure as code software tool created by HashiCorp, enables users to define and provision a datacenter infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform manages external resources (such as public cloud infrastructure, private cloud infrastructure, network appliances, software as a service, and more) with a ‘provider’ model that is easy to expand.

Understanding Terraform’s core commands is essential for anyone looking to automate their infrastructure management. This article provides an overview of the most commonly used Terraform commands, their purpose, and how they fit into the lifecycle of managing infrastructure.

Terraform Initialization: terraform init

The terraform init command is used to initialize a Terraform working directory. This command performs several different initialization steps in order to prepare a directory for Terraform usage. It will download and install any required provider plugins for the project, based on the code defined in the configuration files. It’s the first command that should be run after writing new Terraform configurations or cloning an existing one from version control.

Terraform Validation: terraform validate

The terraform validate command is used to validate the syntax of the Terraform files. It checks that a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It’s a useful tool for catching errors early in the development cycle.

Terraform Planning: terraform plan

The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files. This command is a dry run and does not make any changes to real resources, but it shows what actions Terraform will take to change the infrastructure to match the configuration.

Terraform Applying: terraform apply

The terraform apply command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan. This command is the primary method by which Terraform will make changes to your infrastructure, making it one of the most important commands to understand and use carefully.

Terraform Destruction: terraform destroy

The terraform destroy command is used to destroy the Terraform-managed infrastructure. This command will terminate and remove all resources defined in the Terraform configuration, which can be useful when you no longer need the infrastructure or wish to recreate it from scratch.

Terraform State Management

Terraform relies on a state file to keep track of the resources it manages. While not directly a command, understanding how Terraform interacts with its state file is crucial. Several commands are used for state management:

  • terraform state list: Lists resources in the state.
  • terraform state show: Shows the attributes of a single resource in the state.
  • terraform state rm: Removes items from the state file.
  • terraform state mv: Moves items in the state file.

Terraform Workspace: terraform workspace

The terraform workspace command is used for working with Terraform workspaces. Workspaces allow for the management of certain states separately from each other, which can be useful in managing different environments (such as staging and production) within the same configuration.

Terraform Import: terraform import

The terraform import command is used to import existing infrastructure into Terraform management. This can be useful for bringing real-world infrastructure under Terraform management without needing to recreate resources.

Terraform Output: terraform output

The terraform output command is used to extract the value of an output variable from the state file. This can be useful for scripting or when you need to use information about your infrastructure managed by Terraform in other places.

Conclusion

Terraform’s suite of commands offers a powerful toolkit for defining, launching, and managing infrastructure as code. By understanding and utilizing these commands, teams can automate their infrastructure management processes, reducing human error and increasing efficiency. As Terraform continues to evolve, staying updated with the latest commands and features is key to leveraging the full power of infrastructure as code.