7 Basic Terraform Commands You Should Know

Terraform is the most widely used IaC tool out there. Learn the 7 basic commands to get started with using Terraform.

Terraform is the most popular IaC software tool out there, which works with most of the cloud platforms and helps in provisioning & maintaining your infrastructure. Read how to install Terraform on Windows & Mac the smart way before proceeding.

We will go through the basic commands you need to know to start working with Terraform. This is especially useful if you are a beginner.

Terraform Version

Running the command terraform version will display the current version of the software installed on your machine. It will also warn if the software is outdated.

terraform version

Terraform Init

The command terraform init stands for terraform initialization. This command initializes the working directory by looking at the configuration files & determining which providers and modules needs to be pulled down from the registry for the code to work properly.

In our case, we are using AWS as the provider as seen in the sample code block below. You should be in the working directory to run the init command.

cd <path to your working directory>
terraform init

Terraform Formatting

Terraform provides a command (terraform fmt) to rewrite the configuration files to a canonical format and style. You may not agree with the formatting style & hence it is an optional command.

Terraform fmt

Terraform Validate

The validate command is used to validate the configuration files in the directory for syntax errors etc. The terraform init command should be run before validating the code.

terraform validate

Terraform Plan

The terraform plan is more of a -whatif parameter in the PowerShell world. It goes through the code and output what changes will be made without actually making the change. This helps admin to figure out if the code will make any unexpected change, say delete a resource.

Everything in “+” in the output says that those resources will be created and anything in “-” is a deletion.

terraform plan

Terraform Apply

Terraform apply command, as you would have guessed by now, applies the configuration to your environment. It will deploy, update or delete resources depending on what you have specified in your Terraform config files.

terraform apply

You need to confirm by typing ‘yes’ for this command to succeed.

Terraform Destroy

Well, this is one command that we need to be very careful about. This is mostly used in testing environments, so that you can tear down whatever you have created with Terraform to stop getting charged. This command will remove everything that you have built using your code.

terraform destroy

You need to confirm by typing ‘yes’ for this command to succeed.

Now that you know the basic commands, you can setup a lab easily using the AWS Free Tier account.

Leave a Reply

Your email address will not be published. Required fields are marked *