Terraform Basic Command

imrishav

Rishav Sinha

Posted on February 4, 2022

Terraform Basic Command

What Exactly is Terraform IaC?

Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure.

This is the definition that Hashicorp (Company working on Terraform) uses.

But in simple terms Terraform is used as IaC to create , change and imporve resources provided on different cloud services with simply writing code that is it.

Now let's understand this by a simple example.
Suppose your company wants you to deploy a web app using some instances , a provisioned Database, some cloud functions etc.
So traditionally you will go to cloud console (take AWS as example cause why not šŸ˜) and enable services by order like creating the instance, using DB services and lamada's etc.

This takes time and is pretty cumbersome to debug as we can miss step.

So using IaC services such as Terraform we can simply write code to use the above said services and all the services will be deployed automatically, not only deploy if we want to delete or change some we can simply use the code for that.
Trust me it very Simple and remove too many bottlenecks.

So this is what Terraform is and it uses in a nutshell.

Cool šŸ˜Ž šŸ˜Ž now lets see some of the commands which are generally used and you will probably use regularly while working with Terraform

Prerequisite

  • Before starting make sure you have terraform installed using the following command
terraform --version
Enter fullscreen mode Exit fullscreen mode

tf version

  • Now after verified that you have Terraform installed, create a new directory and create a file main.tf this will host our first terraform code

file

In that file put the code snippet, This snippet leverages the random provider, by HashiCorp registry, to generate a random string put name. Inside that we have optional parameters such as length. Here we're using random , if we're using some cloud service we will use that here like instances, db recourses etc.

resource "random_pet" "random" {
 length = 6
}
Enter fullscreen mode Exit fullscreen mode

code


*## * LETS GET GOING WITH COMMANDS
*


1. init

init is quite similar to git init, basically initializing a new terraform workspace.

Init

  • After that terraform will communicate with the registry and fetch all the necessary resources metadata in this case the random provider

des

As soon as this process completes you will see some changes in your folder structure.

after init


2. validate

validate validates the configuration files in your workspace in our case ie main.tf.

Validate

If we have some syntax issue it will show up. See I have changed a resource parameter which is not valid. Running it again

No validated tf file


3. plan

Now before making some actual changes to your infrastructure , we can "dry run" or check if every resource we specified in the configuration file is correct.

plan tf


4. apply

It is used to build the resources you planned for from your config file i.e. main.tf

It will spit out the following output, we can put yes to move further, terraform will built your random string resource based on
what was in your plan file.

Also you will see the output as well marked in the image in our case the pet name

apply tf


4. destroy

destroy as the name says destroy command is a easiest way to destroy all resource mentioned in the configuration. It does not delete your configuration file(s), main.tf, etc.

Destroy


That's it folks, these are the basis and the most used Terraform commands, Please heart ā¤ļø if you like the content, will publish few more with some real life examples.

ā„ļø

šŸ’– šŸ’Ŗ šŸ™… šŸš©
imrishav
Rishav Sinha

Posted on February 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Terraform Basic Command
programming Terraform Basic Command

February 4, 2022