Mastering Terraform: Top 20 Essential Commands with Examples for Beginners

Praveen Dandu
3 min readJul 12, 2023

--

Introduction:
Terraform is a powerful infrastructure-as-code (IaC) tool that enables you to define and provision infrastructure resources across various cloud providers. Whether you’re new to Terraform or looking to expand your knowledge, understanding the essential commands is crucial for a successful infrastructure deployment. In this blog, we’ll explore the top 20 Terraform commands with examples that will help beginners start working confidently.

1. terraform init:
The `terraform init` command initializes a new or existing Terraform working directory. It downloads the necessary provider plugins and sets up the backend configuration.

Example:

$ terraform init

2. terraform validate:
The `terraform validate` command checks the syntax and validates the configuration files in the current directory, ensuring they adhere to Terraform’s language conventions.

Example:

$ terraform validate

3. terraform plan:
The `terraform plan` command generates an execution plan that previews the changes Terraform will apply to your infrastructure. It helps you understand what resources will be created, modified, or destroyed.

Example:

$ terraform plan

4. terraform apply:
The `terraform apply` command applies the changes described in your Terraform configuration to your infrastructure. It creates or modifies resources as necessary.

Example:

$ terraform apply

5. terraform destroy:
The `terraform destroy` command destroys the infrastructure managed by your Terraform configuration. It removes all the resources that were created.

Example:

$ terraform destroy

6. terraform get:
The `terraform get` command retrieves and updates any modules referenced in your Terraform configuration.

Example:

$ terraform get

7. terraform workspace:
The `terraform workspace` command allows you to manage workspaces in Terraform. Workspaces provide isolation and separation of resources.

Example:

$ terraform workspace new dev
$ terraform workspace select dev

8. terraform output:
The `terraform output` command displays the outputs defined in your Terraform configuration.

Example:

$ terraform output

9. terraform import:
The `terraform import` command imports existing infrastructure into your Terraform state. It helps you manage resources that were not created using Terraform.

Example:

$ terraform import aws_instance.example i-abcd1234

10. terraform state:
The `terraform state` command allows you to manage and inspect the Terraform state. It provides insights into the current state of your infrastructure.

Example:

$ terraform state list
$ terraform state show aws_instance.example

11. terraform graph:
The `terraform graph` command generates a visual representation of your Terraform configuration as a graph.

Example:

$ terraform graph | dot -Tsvg > graph.svg

12. terraform taint:
The `terraform taint` command marks a resource as tainted, forcing it to be destroyed and recreated on the next `terraform apply`.

Example:

$ terraform taint aws_instance.example

13. terraform providers:
The `terraform providers` command lists the providers used in the current configuration and their version information.

Example:

$ terraform providers

14. terraform fmt:
The `terraform fmt` command automatically formats your Terraform configuration files to follow the standard style conventions.

Example:

$ terraform fmt

15. terraform workspace list:
The `terraform workspace list` command lists all the available workspaces in your Terraform project.

Example:

$ terraform workspace list

16. terraform refresh:
The `terraform refresh` command reconciles the state Terraform knows about with the real-world infrastructure.

Example:

$ terraform refresh

17. terraform import:
The `terraform import` command allows you to import existing resources into Terraform’s state.

Example:

$ terraform import aws_instance.example i-abcd1234

18. terraform state rm:
The `terraform state rm` command removes resources from Terraform’s state, making them eligible for recreation.

Example:

$ terraform state rm aws_instance.example

19. terraform output:
The `terraform output` command retrieves the value of an output variable from the state.

Example:

$ terraform output my_output

20. terraform version:
The `terraform version` command displays the version of Terraform installed on your system.

Example:

$ terraform version

Conclusion:
With these top 20 Terraform commands and examples, you’re well-equipped to start your journey into infrastructure provisioning and management using Terraform. By mastering these commands, you’ll have a solid foundation for building and deploying infrastructure resources in a consistent, scalable, and efficient manner. Remember to explore Terraform’s extensive documentation and community resources for further guidance and best practices. Happy Terraforming!

--

--

Praveen Dandu
Praveen Dandu

Written by Praveen Dandu

🚀 DevOps Engineer | Automating Infrastructure, Streamlining Deployments | Continuous Integration & Delivery Specialist https://www.linkedin.com/in/pravin24/

Responses (2)