Page cover

1.3.2 - Terraform Basics

Last updated Jan 25, 2025

Youtube Video | ~29 min

https://www.youtube.com/watch?v=Y2ux7gq3Z0o&list=PL3MmuxUbc_hJed7dXYoJw8DoCuVHhGEQb&index=12&pp=iAQB

✍️ This video we set up a service account in our Google Cloud. We will then create a Main.tf file to configure our Google Cloud Infrastructure.

We create a keys/my-creds.json with private information. Be sure to not push this to github or anywhere public. Use a terraform gitignore to be safe https://github.com/github/gitignore/blob/main/Terraform.gitignore

📜 Main.tf

Terraform provider for Google Cloud - The Google Cloud provider is used to configure your Google Cloud infrastructure.

https://registry.terraform.io/providers/hashicorp/google/latest/docs

To install this provider, copy and paste this code into your Terraform configuration. Then, run terraform init.

Terraform 0.13+

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "6.17.0"
    }
  }
}

provider "google" {
  # Configuration options
  provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
}
}

Where my-project-id comes from your GCP Dashboard.

◼️ Terminal you can now run terraform init

+ Add a new bucket in Google cloud storage service (GCS) - in the same script append

resource "google_storage_bucket" "auto-expire" {
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
  }

  lifecycle_rule {
    condition {
      age = 1
    }
    action {
      type = "AbortIncompleteMultipartUpload"
    }
  }
}

Where auto-expire & name need to be changed to a unique value, where name needs to be globally unique

👀 Note in 1.3.3 we will create variables for these inputs

◼️ Terminal you can now run terraform plan and then terraform apply

👀 We can also generate the proposed changes and auto-executing the plan by using terraform apply -auto-approve

You should now see this bucket created on your GCP

◼️ Terminal you can now run terraform destroy once you are done

Last updated