Getting Start With Terraform
Tags: cloud, terraform, gcp, infrastructureascode
Published: 2023-09-26 06:49:41

**Step 1**

To getting start with Terraform You need to install 
- Terraform Lates Version (Terraform v1.5.7)
- Gcloud Latest Version (Google Cloud SDK 447.0.0)

To check the installation is completed or not try these commands

`terraform -version
`
`gcloud --version
`
The commands will throw the versions number

---

**Step 2**
- Open VS Code 
- Create a new folder give name whatever you want (eg : Infra )
- Inside the folder create **main.tf** file
- Also create **provider.tf** file

---

_Bonus Tip! : Install HashiCorp Terraform Extension in Vs code for auto suggesion_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqphd3h1dqc5zscom2c6.png)
---
Before jumping into the Step 3 There are certain things to know

- The **main.tf** is the file where we describe the resources such as the region, zone, machine-type, startup-script and all

- The **provider.tf** file will provide the support materials such as the project id , version credentials etc etc to the **main.tf** file

---

**Step 3**

- Go to [registry.terraform](https://registry.terraform.io/providers/hashicorp/vsphere/latest)
- Inside that you will find a **USE PROVIDER** Button click that and copy paste the code inside the _provider.tf_ file

or copy and paste the below one

```
terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "4.83.0"
    }
  }
}

provider "google" {
  project = "YOUR PROJECT ID"
  region = "us-central1"
  zone = "us-central1-a"
  credentials = ""
}

```
- Here you can change the 
  - project
  - region
  - zone
  - credentials
- For the credentials you need to generate the key file
---
**Step 4**

Follow the Steps for Generating key 
- Go to your gcloud console 
- Go to IAM & Admin the click Service account
- Inside Service account 
- Click manage key inside the 3 dots

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mela26eyuamk1qzno6pf.png)
- Then Press Add Key The click the Create new key

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wwokw3804fcwn32uxnv.png)

- It will ask for json format click that and download

---
**Step 5**
- Rename the file as _keys.json_
- copy and paste the keys.json file into the project directory 
- give the file path to _credentials_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82y0sh8qah8ce1ruif8a.png)

---

**Step 6**

- Copy and paste the below code template inside main.tf file

```
resource "google_compute_instance" "vm-from-terraform" {
    name = "vm-from-terraform"
    machine_type = "e2-medium"
    zone = "us-central1-a"
    boot_disk {
      initialize_params {
        image = "debian-cloud/debian-11"
        labels = {
          my_label = "value"
        }
      }
    }
    network_interface {
      network = "default"
    }
    metadata_startup_script = "echo hi > /test.txt"
    #allow_stopping_for_update = true
}

```
---

**Step 7**

- Perform The following Commands

`terraform --init
`
`terraform --plan
`
`terraform --apply
`
- Check the gcloud console if the new VM is present or not