這篇文章將會說明如何快速在 Google Cloud Platform 上使用 Terraform 建立外部與內部的全球 IP 。
定義GCP Provider 和所需的 Terraform 版本。
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "5.7.0"
}
}
}
provider "google" {
project = "project-id"
region = "asia-east1"
}
在此檔案中定義全球 IP 地址資源。
resource "google_compute_global_address" "default" {
name = "global-external-ip"
address_type = "EXTERNAL"
ip_version = "IPV4"
labels = {
"dept" = "devops",
"env" = "dev",
"type" = "internal"
}
}
resource "google_compute_global_address" "default" {
name = "global-internal-ip"
address_type = "INTERNAL"
network = "default"
ip_version = "IPV4"
purpose = "PRIVATE_SERVICE_CONNECT"
address = "100.100.100.105"
labels = {
"dept" = "devops",
"env" = "dev",
"type" = "external"
}
}
參數介紹
PRIVATE_SERVICE_CONNECT
表示用於私有服務連接。terraform init
terraform plan
terraform apply
部署完成後,如果需要清理資源,可執行:
terraform destroy