DEV Community

karthik sunil
karthik sunil

Posted on

1

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 main.tf file
  • Also create provider.tf 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 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
  • 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 = ""
}

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

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay