2022-10-09

Terraform Core Commands

Introduction

Terraform allows you to automate your infrastructure tasks, making it easier to manage complex infrastructure environments.

In this article, I'll explore the essential Terraform commands that include terraform init, terraform plan, terraform apply, terraform destroy, terraform state, terraform validate, terraform fmt, and terraform providers.

We'll also discuss the options of the terraform apply command that enable you to customize how Terraform applies changes.

terraform init

terraform init initializes a new or existing Terraform project by downloading the necessary providers and modules, creating a .terraform directory to store configuration files, and generating a terraform.tfstate file to store the current state of your infrastructure.
csharp

bash
$ terraform init

terraform plan

terraform plan analyzes your Terraform code and shows you what resources will be created, modified, or destroyed.

bash
$ terraform plan

terraform apply

terraform apply applies the changes that Terraform planned in the previous step by creating, modifying, or destroying the resources that you've defined in your Terraform code.

bash
$ terraform apply

In addition to the basic usage of terraform apply, there are several options that you can use to customize how Terraform applies changes.

-auto-approve

This option automatically approves and applies the changes without prompting for confirmation.

bash
$ terraform apply -auto-approve

-var

This option allows you to specify variable values at the command line.

bash
$ terraform apply -var="region=us-west-2"

-target

This option allows you to apply changes to a specific resource instead of applying changes to all resources.

bash
$ terraform apply -target=aws_instance.example

-input=false

This option disables user input, such as confirmation prompts or variable input.

bash
$ terraform apply -input=false

-parallelism=n

This option allows you to control the number of resource operations that Terraform performs in parallel.

bash
$ terraform apply -parallelism=10

-refresh=false

This option disables the refreshing of state data before applying changes.

bash
$ terraform apply -refresh=false

terraform destroy

terraform destroy destroys the resources that Terraform has created. This command is useful when you want to tear down your infrastructure or start fresh.

bash
$ terraform destroy

terraform state

terraform state manages the state of your infrastructure by allowing you to view and modify the state of your resources in the terraform.tfstate file.

bash
$ terraform state list

terraform validate

terraform validate validates your Terraform code by checking for syntax errors, invalid references, and other issues.

bash
$ terraform validate

terraform fmt

terraform fmt formats your Terraform code consistently and makes it easier to read and understand.

bash
$ terraform fmt

terraform providers

terraform providers manages the providers that you use in your Terraform code by allowing you to list, install, and update providers.

bash
$ terraform providers

References

https://developer.hashicorp.com/terraform/cli/commands

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!