【GCP和Terraform】【入门篇②】Terraform的依赖关系和VPC配置

上次

【GCP&Terraform】【入门篇①】使用Terraform创建GCP虚拟机
https://qiita.com/SHA_AKA/items/86bf08e9cf35c3b7fd6f

这次的目标

(horizontal writing)

目标:今回

    1. 关于文件main.tf的构成

 

    1. 资源的依赖关系:显式和隐式的依赖关系

 

    1. 隐式的依赖关系:创建VPC和创建VM

 

    显式的依赖关系:创建存储桶和创建VM

关于主要配置文件main.tf

与上次相同,使用GCP启动Cloud Shell,并创建一个名为main.tf的配置文件。

touch main.tf

在main.tf文件中添加以下内容。

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

terraform {} 块是必需的,它用于让 Terraform 了解从 Terraform Registry 下载的提供程序。
在上述配置中,google 提供程序的源被定义为 registry.terraform.io/hashicorp/google 的简写形式 hashicorp/google。

provider "google" {
#  version = "3.5.0"
  project = "<PROJECT_ID>"
  region  = "asia-northeast3"
  zone    = "asia-northeast3-c"
}

已更换为项目 ID。
还可以设置 region/zone。
version 参数是可选的。
(最近因为 asia-northeast1 的资源经常不足,所以转移到了 asia-northeast3 进行避难。)
使用指定的提供程序(在这种情况下是 Google)的配置,该提供程序负责创建和管理资源。
如果在 Terraform 配置中要管理多个提供程序的资源,则可以指定多个 provider 块。

2. 关于资源的依赖关系:明确和隐含的依赖关系

您可以自动推断Terraform资源之间的依赖关系。隐式依赖是Terraform识别依赖关系的主要方式,并且我们尽可能使用它。

如果在资源之间存在Terraform无法“看到”的依赖关系,您可以创建明确的依赖关系,通过向资源添加depends_on参数,并指定依赖资源的列表。

3. 隐含依赖关系:创建VPC

在 main.tf 中添加 VPC 资源。

resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

将Compute实例资源添加到main.tf。

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
  network_interface {
    network = google_compute_network.vpc_network.self_link
    access_config {
      nat_ip = google_compute_address.vm_static_ip.address
    }
  }
}

在main.tf文件中为VM实例分配静态IP并添加到配置中。

resource "google_compute_address" "vm_static_ip" {
  name = "terraform-static-ip"
}

Terraform会读取此配置并执行以下操作:
1. 在创建vm_instance之前,先创建VPC和vm_static_ip。
2. 将vm_static_ip的属性保存为状态。
3. 将nat_ip设置为vm_static_ip.address属性的值。

执行命令terraform apply将会产生如下输出示例。

...
google_compute_address.vm_static_ip: Creating...
google_compute_network.vpc_network: Creating...
google_compute_address.vm_static_ip: Creation complete after 5s [id=projects/<projectid>/regions/asia-northeast3/addresses/terraform-static-ip]
google_compute_network.vpc_network: Still creating... [10s elapsed]
google_compute_network.vpc_network: Still creating... [20s elapsed]
google_compute_network.vpc_network: Still creating... [30s elapsed]
google_compute_network.vpc_network: Creation complete after 37s [id=projects/<projectid>/global/networks/terraform-network]
google_compute_instance.vm_instance: Creating...
google_compute_instance.vm_instance: Still creating... [10s elapsed]
google_compute_instance.vm_instance: Creation complete after 14s [id=projects/<projectid>/zones/asia-northeast3-c/instances/terraform-instance]

4. 明确的依赖关系:创建存储桶和虚拟机

举个例子,假设您的应用程序在实例上运行,并使用特定的云存储桶。如果这种依赖关系在应用程序代码中进行了设置,那么 Terraform 就无法感知到它。在这种情况下,您需要使用 depends_on 来显式声明这种依赖关系。
您可以在配置文件中添加如下内容:

# アプリケーションで使用するストレージ バケット用の新しいリソース
resource "google_storage_bucket" "example_bucket" {
  name     = "<UNIQUE-BUCKET-NAME>"
  location = "US"
  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
}
# このバケットを使用する新しいインスタンスを作成する
resource "google_compute_instance" "webapp_instance" {
  # この VM インスタンスの作成は、ストレージ バケットの作成後にのみ行うよう
  # Terraform に指示する
  depends_on = [google_storage_bucket.example_bucket]
  name         = "terraform-instance-2"
  machine_type = "f1-micro"
  boot_disk {
    initialize_params {
      image =  "debian-cloud/debian-9"
    }
  }
  network_interface {
    network = google_compute_network.vpc_network.self_link
    access_config {
    }
  }
}

在其中,depends_on = [google_storage_bucket.example_bucket]表示明确的依赖关系。
执行terraform apply命令后,将产生如下输出示例。

...
google_storage_bucket.example_bucket: Creating...
google_storage_bucket.example_bucket: Creation complete after 1s [id=shadev202205261433]
google_compute_instance.webapp_instance: Creating...
google_compute_instance.webapp_instance: Still creating... [10s elapsed]
google_compute_instance.webapp_instance: Creation complete after 12s [id=projects/<projectid>/zones/asia-northeast3-c/instances/terraform-instance-2]

删除资源和下次

当您执行terraform destroy命令时,将删除您构建的所有资源。
下一次,我们想讨论在GCP上的Terraform模块。

广告
将在 10 秒后关闭
bannerAds