前置准备
安装 Terraform
请参考 Terraform 官方文件 安装 Terraform。MacOS 可以使用 Homebrew 安装:
brew install terraform
安装 Google Cloud SDK
请参考 Google Cloud SDK 官方文件 安装 Google Cloud SDK。MacOS 可以使用 Homebrew 安装:
brew install --cask google-cloud-sdk
安装完成后,请使用以下指令登录您的 Google Cloud 账号:
gcloud auth application-default login
在 GCP 上面建立 Service Account
确保已经安装并设置了 Google Cloud SDK,并且已经登录到您的 Google Cloud 账号。您可以执行以下指令來检查是否正确安装了 gcloud 工具:
$ gcloud version
Google Cloud SDK 432.0.0
bq 2.0.93
core 2023.05.19
gcloud-crc32c 1.0.0
gke-gcloud-auth-plugin 0.5.3
gsutil 5.24
接下来,使用以下指令建立 service account:
gcloud iam service-accounts create [SERVICE_ACCOUNT_NAME] --display-name "[DISPLAY_NAME]"
将 [SERVICE_ACCOUNT_NAME]
替换为您想要建立的 service account 名称,并将 [DISPLAY_NAME]
替换为适当的显示名称。
建立 service account 后,您可以为该账号新增所需的角色/权限。例如,如果要将 service account 分配为project的所有者,请执行以下指令:
gcloud projects add-iam-policy-binding [PROJECT_ID] \--member "serviceAccount:[SERVICE_ACCOUNT_EMAIL]" \--role roles/owner
将 [PROJECT_ID]
替换为您的PROJECT ID,将 [SERVICE_ACCOUNT_EMAIL]
替换为刚刚建立的 service account 的電子邮件地址。
完成以上步骤后,您已成功使用 gcloud 指令建立了一个 service account,并为其分配了适当的角色/权限。接下来,您可以使用以下指令建立一个 service account 认证文件:
gcloud iam service-accounts keys create [FILE_NAME].json \--iam-account [SERVICE_ACCOUNT_EMAIL]
将 [FILE_NAME]
替换为您想要建立的自定义名称,将 [SERVICE_ACCOUNT_EMAIL]
替换为刚刚建立的 service account 的电子邮件地址。请将此文件妥善保存,后续的任何操作都基于此文件。
建立 Terraform 初始化认证配置
terraform init
在 main.tf
配置 project
、credentials
、region
、zone
等字段:
terraform {required_providers {google = {source = "hashicorp/google"version = "4.66.0"}}required_version = ">= 0.14"
}provider "google" {project = "your-project-id"credentials = file("your-service-account-key.json")region = "asia-east1"zone = "asia-east1-a"
}
其中 credentials 参数为刚刚创建的 service account 配置文件。还可以用 terraform.tfvars
來设定:
project = "your-project-id"
credentials = "your-service-account-key.json"
region = "asia-east1"
zone = "asia-east1-a"
除了配置 credentials
之外,您也可以使用 GOOGLE_APPLICATION_CREDENTIALS
环境变量来设定 service account 认证文件的路径:
export GOOGLE_APPLICATION_CREDENTIALS="your-service-account-key.json"
建立 GCP 资源
main.tf
terraform {required_providers {google = {source = "hashicorp/google"version = "4.66.0"}}required_version = ">= 0.14"
}variable "project_id" {description = "project id"
}variable "region" {description = "region"
}variable "zone" {description = "zone"
}provider "google" {project = var.project_idregion = var.regionzone = var.zone
}
建立 VPC 资源
# VPC
resource "google_compute_network" "tf-vpc" {name = "${var.project_id}-tf-vpc"auto_create_subnetworks = "false"
}
完成以上步骤后,您可以使用以下指令来预览 Terraform 将要建立的资源:
terraform plan
如果预览結果沒有问题,您可以使用以下指令来建立资源:
terraform apply
存储 Terraform 状态到 GCS
执行完上述步骤后,在terraform资源配置目录下底下多了一个 terraform.tfstate
文件,这个文件是用来记录 Terraform 管理的资源状态,以及资源间的关联性。如果您想要将这个文件存储到 GCS,可以用 Terraform 建立 GCS 相关资源:
# GCS
resource "random_id" "bucket_prefix" {byte_length = 8
}resource "google_storage_bucket" "tf-state-bucket-demo" {name = "tf-state-bucket-demo-${random_id.bucket_prefix.hex}"force_destroy = falselocation = "US"storage_class = "STANDARD"versioning {enabled = true}
}
输出 GCS bucket 名称
output "tf_state_bucket" {value = google_storage_bucket.tf-state-bucket.name
}
可以看到底下结果:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Outputs:tf_state_bucket = tf-state-bucket-demo-5704c463dc9b78df
打开main.tf档案,加入以下配置到 terraform主配置:
backend "gcs" {bucket = "tf-state-bucket-demo-5704c463dc9b78df"prefix = "terraform/state"}
再执行一次 terraform init
,就会看到以下结果:
Initializing the backend...
Acquiring state lock. This may take a few moments...
Do you want to copy existing state to the new backend?Pre-existing state was found while migrating the previous "local" backend to thenewly configured "gcs" backend. No existing state was found in the newlyconfigured "gcs" backend. Do you want to copy this state to the new "gcs"backend? Enter "yes" to copy and "no" to start with an empty state.Enter a value:
输入 yes
,就会看到以下结果;
Successfully configured the backend "gcs"! Terraform will automatically
use this backend unless the backend configuration changes.Initializing provider plugins...
- Reusing previous version of hashicorp/google from the dependency lock file
- Reusing previous version of hashicorp/random from the dependency lock file
- Using previously-installed hashicorp/google v4.66.0
- Using previously-installed hashicorp/random v3.5.1Terraform has been successfully initialized!
这样就可以透通过GCS进行Terraform 状态文件的存储及版本控制了。最后可以将目录下状态文件刪除:
rm terraform.tfstate
rm terraform.tfstate.backup
删除 GCP资源
如果想要刪除建立的资源,可以在资源目录下使用以下指令:
terraform destroy