希望能夠透過terraform來加速佈建雲端服務的效率
1. 安裝terraform
(terraform只是一個執行檔案)
到https://www.terraform.io/downloads.html 下載壓縮檔案
解壓縮到機器上的/usr/local/bin就可以直接使用
2.建立第一個 tf配置檔案
a.從GCP建立Account Service , 並下載金鑰檔案(json格式),rename成account.json
b.配置Google Cloud provider設定
(project 這邊是project id)
provider "google" {
credentials = file("account.json")
project = "ab-xx-xxxx"
region = "asia-east1"
}
3. 完善tf設定檔案,並在台灣節點建立一台vm , 配置如下
file - 1.tf
provider "google" {
credentials = file("account.json")
project = "ab-xx-xxxx
region = "asia-east1"
}
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "n1-standard-1"
zone = "asia-east1-c"
boot_disk {
initialize_params {
image = "centos-cloud/centos-7"
}
}
network_interface {
# A default network is created for all GCP projects
network = "default"
subnetwork = "default"
}
}
4.啟動/刪除資源從tf檔案 ---- 好處是資源新增刪除都不會漏掉
(金鑰和tf檔案都要放在同一層)
啟動
terraform init
刪除
terraform destroy
參考網站:
https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance
https://ithelp.ithome.com.tw/articles/10206648