1.  main configuration file (main.tf)

 

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"          # cloud provider
    }
  }
}
provider "google" {
  version = "3.5.0"                                  # provider version
  project = "<PROJECT_ID>"
  region  = "us-central1"
  zone    = "us-central1-c"
}
resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

 

resource "google_storage_bucket" "example_bucket" {
  name     = "<UNIQUE-BUCKET-NAME>"
  location = "US"
  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
}


# Create a new instance that uses the bucket
resource "google_compute_instance" "another_instance" {
  # Tells Terraform that this VM instance must be created only after the
  # storage bucket has been created.
  depends_on = [google_storage_bucket.example_bucket]      # depend option between resources.
  name         = "terraform-instance-2"
  machine_type = "e2-micro"
  boot_disk {
    initialize_params {
      image = "cos-cloud/cos-stable"
    }
  }
  network_interface {
    network = google_compute_network.vpc_network.self_link
    access_config {
    }
  }
}

 

 

 

2. terraform initialize

terraform init

 

3. create resource ( based on above main.tf )

terraform apply      (after showing plan, it can create if you answer yes at the confirmation prompt to proceed)

 

# the sign means when showing the plan.

+ : add resource

~ : update resource

- : delete resource

+/- : remove and recreate resource

 

4. destroy resource

terraform destroy

 

 

'cloud > IaC' 카테고리의 다른 글

module code for terraform  (0) 2023.08.23
google terraform IAC example  (0) 2023.08.16
terraform 설치  (0) 2023.02.23

+ Recent posts