Terraform Basic Command
Tags: programming, devops, terraform, beginners
Published: 2022-02-04 18:49:43

## 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 :grin:) 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 😎 :sunglasses:	 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
```

![tf version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnht4z9vyjstt86dqi9f.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ddztl6yfio46zt29lto.png)

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
}
```


![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7zbyl4l8es0prns5mzo.png)

---


**## * <u>LETS GET GOING WITH COMMANDS</u>
**

---

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


![Init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8v0pu59djekktoui4taf.png)

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


![des](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq5ezxrbf7qqdlnf8gzw.png)

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


![after init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kprt4jo0u6rw13o28j1z.png) 

---

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


![Validate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e0fe092fmrw6tl1nhnr.png) 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oc9k7v3q85wclch1asiq.png)

---

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snaytgnj6wfyfmqexqh1.png)

---

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hn261cmfsihx1fnreus.png)

---

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5w8srhvdwsznhczve1zc.png)

---

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.
        
❄️ 
---