DEV Community

karthik sunil
karthik sunil

Posted on

Getting Start With Terraform

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 file
  • Also create file

Bonus Tip! : Install HashiCorp Terraform Extension in Vs code for auto suggesion

Image description

Before jumping into the Step 3 There are certain things to know

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

  • The file will provide the support materials such as the project id , version credentials etc etc to the file

Step 3

  • Go to registry.terraform
  • Inside that you will find a USE PROVIDER Button click that and copy paste the code inside the 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 = ""

Enter fullscreen mode Exit fullscreen mode
  • 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

  • Then Press Add Key The click the Create new key

Image description

  • 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

Step 6

  • Copy and paste the below code template inside 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

Enter fullscreen mode Exit fullscreen mode

Step 7

  • Perform The following Commands

terraform --init

terraform --plan

terraform --apply

  • Check the gcloud console if the new VM is present or not

Top comments (0)