Terraform 教學:用文字檔描述雲端資源,plan 預覽、apply 建立、state 記住建了什麼。
在 Google Cloud Console(GCP 的網頁管理介面)上點滑鼠建一個 bucket 只要幾個步驟,但三個月後要在另一個專案做一模一樣的環境,你多半已經忘了當初勾了哪些選項。Terraform 的做法是:把「最後要長什麼樣子」寫成文字檔,交給它去建、去改、去刪。
我在 2026 年 12 月 5 日凌晨,用 Cloud Shell 從零做了一次:
- 發現 Cloud Shell 從 2026 年 6 月起已經不內建 Terraform,照它自己印的指令裝好 1.17.3
- 寫 4 個檔案,用
terraform apply一次建出 Cloud Storage bucket、服務帳戶、Cloud Run 服務 3 個資源 - 把
terraform plan的 4 種符號(+、~、-/+、-)各實際跑出一次,包含「有人偷偷在 Console 改了設定」被 Terraform 抓到 terraform destroy第一次失敗,看懂原因再刪乾淨
這篇寫給想學 Terraform、想把 GCP 環境「寫成程式碼」、或正在準備 GCP DevOps 證照的人,指令都可以整段複製。這是「DevOps 證照+Ops 名詞」系列第 10 篇,只講基礎;state 放 Cloud Storage、模組(modules)、Infrastructure Manager 留給第 11 篇。

為什麼你需要知道
- 手動點的環境很難重做一次:Console 上點出來的設定(例如一個 Cloud Storage bucket,也就是雲端上放檔案的空間)沒有紀錄,換專案、換同事、出事要重建時,只能憑記憶再點一次,很容易漏掉一個勾選
- 改之前看得到會發生什麼:
terraform plan會先列出「要新增、修改、刪除哪些東西」,不會像手動操作那樣按下去才知道後果。這篇實測把區域從台灣改成東京,plan 直接標出 bucket 會被「砍掉重建」,裡面的檔案會一起不見 - 舊教學的第一步現在會卡住:Cloud Shell 以前預設就裝好 Terraform,Cloud Shell 版本資訊寫明 2026 年 6 月 20 日起不再內建(2026 年 12 月查官方文件)。照「Cloud Shell 打開就能用」的教學打
terraform,現在只會看到一段安裝說明 - 證照會考:Professional Cloud DevOps Engineer 2026 年 11 月 23 日起的新版考綱,1.2「管理基礎架構」點名 Terraform,還新增了「Terraform modules and blueprints」
先懂 4 個名詞
Infrastructure as Code(基礎架構即程式碼)是什麼
Infrastructure as Code(IaC,基礎架構即程式碼)是把伺服器、網路、儲存空間這些雲端資源,用文字檔寫下來,再交給工具照著建的做法。
- 白話:不用滑鼠點,改成寫一份「清單」,工具照清單把東西建好
- 生活比喻:裝潢房子時,口頭跟師傅說「這裡放櫃子、那裡裝燈」,下一間房子就得再講一次;改成畫一張設計圖,每間都照圖施工,哪裡改過也看得出來。IaC 就是那張設計圖
- 來龍去脈:雲端出現之後,開一台機器只要幾秒,公司的資源從幾台變成幾百個。靠人記「當初怎麼設定的」記不住,所以業界開始把設定寫成檔案、放進 Git(版本控制工具,記錄檔案每一次修改)管理
- 為什麼要學:寫成檔案之後,環境可以重複建、改動可以在合併前請同事審查(Code Review)、出事可以照檔案重建。DevOps、SRE 的工作幾乎天天碰得到
「基礎架構」(infrastructure)在這篇指的是程式跑起來需要的底層東西:放檔案的空間、跑程式的服務、程式用的身分,不是程式本身。
Terraform 是什麼
Terraform 是 HashiCorp 公司做的 IaC 工具:你用它的語言寫下「要有哪些雲端資源」,它負責比對現況、算出要做哪些動作,再呼叫雲端的 API 去建、改、刪。
- 白話:一個看得懂設計圖、會自己去跟 GCP 下單的工頭
- 生活比喻:你交給工頭一張「客廳要有 1 張沙發、2 盞燈」的圖。工頭先看客廳現在有什麼,回報「要買 1 張沙發、2 盞燈」(plan),你點頭他才去買(apply)。下次你把圖改成 3 盞燈,他只會補買 1 盞,不會把整個客廳重做
- 來龍去脈:Terraform 第一版 0.1 在 2014 年 7 月 28 日發布(GitHub 上的 v0.1.0 標記)。設定檔用 HashiCorp 自己設計的 HCL(HashiCorp Configuration Language,一種用大括號
{}分段、名稱 = 值寫設定的格式),副檔名.tf。2023 年 8 月 10 日 HashiCorp 宣布之後的版本從開源授權 MPL 2.0 改成 BSL 1.1(Business Source License,可以免費用,但不能拿來做跟 HashiCorp 競爭的商業服務);社群另外分出一個開源版本 OpenTofu,2024 年 1 月 10 日正式推出。2025 年 2 月 27 日 IBM 完成收購 HashiCorp - 為什麼要學:同一套寫法可以管 GCP、AWS、Azure、GitHub 和很多其他服務,學一次到處用。GCP 官方的 Terraform 範例、Infrastructure Manager(Google 代管的 Terraform 執行服務,第 11 篇講)也都用它
這篇實測的版本:Terraform 1.17.3(2026 年 12 月 2 日發布)、GCP 的 provider 8.12.0(2026 年 11 月 17 日發布),兩個都是當時的最新版(2026 年 12 月查 HashiCorp 和 Terraform Registry)。
provider 是什麼
provider 是 Terraform 跟某個平台溝通的外掛程式:Terraform 本身不認識 GCP,要裝上 Google 的 provider,才知道怎麼建 bucket、Cloud Run。
- 白話:翻譯官。你寫 HCL,provider 翻成 GCP 的 API 呼叫
- 生活比喻:手機要裝 LINE 才能傳 LINE 訊息,要裝銀行 App 才能轉帳。Terraform 是手機,provider 是 App
- 來龍去脈:HashiCorp 把「核心程式」和「各平台的知識」拆開,Google、AWS、各家廠商各自維護自己的 provider,放在 Terraform Registry(provider 和模組的下載站)上
- 為什麼要學:provider 有自己的版本號,大改版會換預設值。這篇會提到兩個:6.0 版起 Cloud Run 預設開「刪除保護」、7.0 版起刪資源不再順手關掉整個專案的 API。看舊教學時要先看它用的是哪一版
state 是什麼
state(狀態檔)是 Terraform 用來記住「我建了哪些東西、它們現在長怎樣」的檔案,預設叫 terraform.tfstate,放在你下指令的資料夾裡。
- 白話:Terraform 的記帳本
- 生活比喻:工頭的進貨單。沒有它,工頭走進客廳看到一張沙發,分不出是他買的、還是屋主本來就有的
- 來龍去脈:雲端上的資源名稱可以重複、有些屬性要建好才知道(例如 Cloud Run 的網址),所以 Terraform 需要一個地方把「設定檔裡的名字」對應到「雲端上真正的那個資源」
- 為什麼要學:state 裡是明文(沒加密的文字),HashiCorp 官方文件寫明它會存下設定裡的秘密值,所以不能放進 Git。一個人練習放本機就好;團隊一起用要放到大家都拿得到、又有權限控管的地方,例如 Cloud Storage,這部分在第 11 篇:Terraform 進階:state 放 Cloud Storage、modules、Infrastructure Manager
下面幾個名詞先看過去就好,動手做的時候會一個一個用到:
| 名詞 | 白話 | 這篇的例子 |
|---|---|---|
| resource(資源) | 一個要建的雲端東西 | google_storage_bucket、google_cloud_run_v2_service |
| variable(變數) | 可以從外面換值的欄位 | project_id、region、env |
| output(輸出) | 建好之後要印給你看的值 | Cloud Run 的網址、bucket 名稱 |
terraform.tfvars | 幫變數填值的檔案 | project_id = "dong-dong-gcp-3" |
lock 檔(.terraform.lock.hcl) | 記下這次用的 provider 版本 | hashicorp/google 8.12.0 |
| 漂移(drift) | 雲端現況跟設定檔對不上 | 有人在 Console 手動改了標籤 |
點 Console、下 gcloud、寫 Terraform 差在哪
gcloud 是 Google Cloud 的命令列工具,在終端機打指令操作 GCP。它是命令式(imperative):你寫「先做這步、再做那步」。Terraform 是宣告式(declarative):你只寫「最後要長這樣」,步驟它自己算。

| 點 Console | gcloud 指令 | Terraform | |
|---|---|---|---|
| 適合 | 第一次摸索、看畫面 | 臨時查狀態、一次性操作、排查問題 | 要長期維護、要重複建的環境 |
| 再跑一次 | 照截圖再點 | 資源已經存在要自己判斷 | 實測:No changes,什麼都不做 |
| 有人手動改了 | 不會知道 | 不會知道 | 實測:設定檔有寫的屬性,plan 列出差異、apply 改回去 |
| 刪乾淨 | 一個一個找 | 自己寫刪除指令 | terraform destroy,照 state 刪 |
學 Terraform 不代表不用 gcloud。這篇確認資源有沒有建出來、故意製造漂移,用的都還是 gcloud。
動手做:用 Terraform 建 bucket、服務帳戶、Cloud Run
這段做完會有:一個 Cloud Storage bucket、一個服務帳戶、一個公開的 Cloud Run 服務,全部由 4 個 .tf 相關檔案描述;最後再用 Terraform 全部刪掉。
- 在哪裡做:Cloud Shell(Google 提供、開瀏覽器就能打指令的 Linux 終端機),專案 dong-dong-gcp-3,區域
asia-east1(台灣彰化) - 花多久:我實測從安裝到刪完約 11 分鐘(裝 Terraform 不到 1 分鐘、apply 約 35 秒、destroy 約 12 秒);第一次邊讀邊做,預留 1 小時比較從容
- 事前準備:一個已連結帳單帳戶的 GCP 專案,你的帳號在專案上最簡單是擁有者(Owner)。不是 Owner 的話,要能開 API(
roles/serviceusage.serviceUsageAdmin)、建 bucket(roles/storage.admin)、建服務帳戶(roles/iam.serviceAccountAdmin)、部署 Cloud Run(roles/run.admin),而且要能讓 Cloud Run 用那個服務帳戶的身分執行(roles/iam.serviceAccountUser)。下面的dong-dong-gcp-3要換成你的專案 ID - 會不會花錢:幾乎不會。Cloud Run 沒人連的時候會縮到 0 台(這篇沒設最小執行個體,預設是 0)、空的 bucket 不收儲存費、服務帳戶免費;Terraform CLI 本身免費下載使用。但 Cloud Run 網址是公開的,任何人都能連、連了就有請求量,做完照「用完記得刪」刪乾淨
- 專案要選對:Cloud Shell 提示字元括號裡顯示的是目前的專案;不是你要用的那個,先跑
gcloud config set project 你的專案ID - 中途斷線:Cloud Shell 重新連線後,先打
cd ~/do-10回到練習資料夾。如果閒置超過 40 分鐘、機器被收掉,步驟 2 的安裝要再做一次;~/do-10在家目錄裡,檔案和 state 都還在
怎麼開 Cloud Shell:登入 Google Cloud Console,點右上角的 >_ 圖示(紅框)。第一次用的完整步驟在第 1 篇。

下面每段指令都是整段複製、貼進 Cloud Shell、按 Enter,等提示字元(結尾是 $ 的那行)再出現就是跑完了。截圖裡的提示字元有縮短,你的畫面會是「你的帳號@cloudshell:~/do-10 (專案ID)$」。這篇建的東西一律 do-10- 開頭,刪的時候不會刪錯。
步驟 1:確認 Cloud Shell 有沒有 Terraform
先建一個練習用的資料夾,再問 Terraform 的版本。預期會看到一段安裝說明,不是你做錯:
mkdir -p ~/do-10 && cd ~/do-10
terraform version
Follow the instructions at https://developer.hashicorp.com/terraform/install to install terraform, or run the commands below:
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
bash
To persist the installation between sessions, either add the above commands to $HOME/.customize_environment, or install the terraform binary in your home directory.

沒有版本號,只有一段安裝說明。Cloud Shell 裡的 terraform 現在是一個只會印這段字的小腳本(實測放在 /google/bin/terraform)。這就是 2026 年 6 月 20 日那則版本資訊說的「不再內建」。
最後一句的意思是:照這樣裝,Cloud Shell 重開就不見了。官方文件寫明 Cloud Shell 閒置 40 分鐘就會被收掉,家目錄以外的修改都會消失,下次開的是一台新機器(配額與限制也寫 40 分鐘);只有家目錄(5 GB)會跨工作階段保存,但 120 天沒開 Cloud Shell,家目錄會被自動刪除、救不回來(刪除前會寄信通知,2026 年 12 月查官方文件)。
步驟 2:照 Cloud Shell 印的指令安裝 Terraform
這 3 行是從步驟 1 那段安裝說明照抄的(apt install 我多加了 -y,不用再按一次 Y)。apt 是 Ubuntu(Cloud Shell 用的 Linux 版本)安裝軟體的工具。第 1 行下載 HashiCorp 的簽章金鑰,第 2 行把 HashiCorp 的軟體來源加進系統,第 3 行更新清單並安裝。sudo 是用管理員權限執行,Cloud Shell 不用輸入密碼。這 3 行看不懂沒關係,整段貼上就好,重點是最後出現 Setting up terraform (1.17.3-1):
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install -y terraform
deb [arch=amd64 signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com noble main
Get:1 https://apt.releases.hashicorp.com noble InRelease [12.8 kB]
Get:2 https://apt.releases.hashicorp.com noble/main amd64 Packages [295 kB]
(中間省略)
Fetched 15.0 MB in 17s (890 kB/s)
(中間省略)
The following NEW packages will be installed:
terraform
0 upgraded, 1 newly installed, 0 to remove and 47 not upgraded.
Need to get 37.7 MB of archives.
After this operation, 130 MB of additional disk space will be used.
Get:1 https://apt.releases.hashicorp.com noble/main amd64 terraform amd64 1.17.3-1 [37.7 MB]
Fetched 37.7 MB in 1s (50.1 MB/s)
(中間省略)
Setting up terraform (1.17.3-1) ...

再看一次版本。hash -r 是請終端機忘掉之前記住的舊 terraform 位置,改找新裝的那個:
hash -r; terraform version
Terraform v1.17.3
on linux_amd64

想要重開 Cloud Shell 也還在:照說明最後一句,把這 3 行寫進家目錄的 ~/.customize_environment(Cloud Shell 每次開機會用管理員權限跑一次,官方說明),或把 Terraform 的執行檔放進家目錄。我的 Cloud Shell 家目錄 ~/bin 裡就留著之前放的 1.17.2,只要 120 天內有開過 Cloud Shell 就一直在;這次為了讓畫面跟新帳號一樣,示範時先把它排除。
步驟 3:確認要用的 API 已經開啟
API(服務對外的操作介面)要先「啟用」,Terraform 才能呼叫。這篇用到 Cloud Run、IAM(身分與權限管理)、Cloud Storage 三個:
gcloud services enable run.googleapis.com iam.googleapis.com storage.googleapis.com
Operation "operations/acat.p2-600195576280-aad4db33-62ee-4650-af4e-459f9f15143a" finished successfully.
出現 finished successfully 就是開好了。
為什麼不用 Terraform 開 API? 可以,用
google_project_service這個資源。但舊版 provider(6.x 以前)刪掉這個資源時,預設會把整個專案的那支 API 關掉,連別人用到的服務都會壞。7.0 版升級指南已經把這個預設拿掉(2026 年 12 月查官方文件)。初學練習建議先用 gcloud 開,資源跟 API 分開管。
步驟 4:寫 4 個檔案
Terraform 會讀資料夾裡所有 .tf 檔,合在一起看,檔名怎麼取都可以。業界習慣分成:main.tf 放資源、variables.tf 放變數、outputs.tf 放輸出、terraform.tfvars 填變數的值。
下面用 cat > 檔名 <<'EOF' ... EOF 這種寫法建檔:中間那段文字會原封不動寫進檔案。不想用這招,也可以點 Cloud Shell 上方的「開啟編輯器」,在網頁版編輯器裡開檔、把內容貼進去。
main.tf:最重要的檔案,分三段。
cat > main.tf <<'EOF'
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 8.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
}
# 1. Cloud Storage bucket:放檔案的空間
resource "google_storage_bucket" "files" {
name = "do-10-${var.project_id}-files"
location = var.region
uniform_bucket_level_access = true
public_access_prevention = "enforced"
labels = {
env = var.env
managed-by = "terraform"
}
}
# 2. 服務帳戶:給 Cloud Run 用的身分
resource "google_service_account" "run" {
account_id = "do-10-run"
display_name = "do-10 Cloud Run runtime"
}
# 3. Cloud Run 服務:跑 Google 的範例容器
resource "google_cloud_run_v2_service" "hello" {
name = "do-10-hello"
location = var.region
invoker_iam_disabled = true
labels = {
env = var.env
managed-by = "terraform"
}
template {
service_account = google_service_account.run.email
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
}
EOF
逐段看:
terraform { required_providers {...} }:宣告要用哪個 provider。source = "hashicorp/google"是 Registry 上的名字;version = "~> 8.0"的意思是「8.0 以上、9.0 以下都可以」(官方說明),大改版的 9.0 出來不會自動換過去provider "google" {...}:設定這個 provider 預設用哪個專案、哪個區域。值從變數來(var.變數名)。這裡沒有寫任何金鑰:Terraform 是借你目前登入的身分去跟 GCP 下單,所以你的帳號要有建立這些資源的權限。在 Cloud Shell 裡你已經登入了,不用另外設定。這個「自動找目前登入身分」的機制,官方叫應用程式預設憑證(Application Default Credentials,ADC);在自己的電腦上要先裝好 gcloud(Google Cloud CLI),跑gcloud auth application-default login登入一次,之後 Terraform 就用你的身分做事resource "類型" "名字" {...}:一個要建的資源。"google_storage_bucket"是類型(provider 規定的)、"files"是你在 Terraform 裡給它取的小名,兩個合起來google_storage_bucket.files就是它在 Terraform 裡的地址- bucket:Cloud Storage 裡放檔案的容器,名稱全世界不能重複,所以用
"do-10-${var.project_id}-files"把專案 ID 拼進去(${...}是把變數塞進字串裡)。uniform_bucket_level_access是統一用 IAM 管權限、public_access_prevention = "enforced"是禁止公開,兩個都是 Google 建議的安全設定。labels是標籤(貼在資源上的「鍵=值」小紙條,方便分類和看帳單) - 服務帳戶(service account):給程式用的帳號,不是給人登入的。這裡建一個給 Cloud Run 用,讓服務不要用到預設那個權限很大的帳戶
- Cloud Run 服務:Google 代管、跑容器的服務(容器是把程式和它需要的環境打包在一起的執行單位)。
image用 Google 官方的範例映像檔,打開網址會看到一頁「Congratulations」。invoker_iam_disabled = true等於 gcloud 的--no-invoker-iam-check:不檢查呼叫者身分,誰都能打開網址。公司或學校的帳號可能有組織政策禁止公開服務,apply 這一行會失敗;個人帳號比較不會遇到 service_account = google_service_account.run.email:這行是重點。Cloud Run 要用「google_service_account.run這個服務帳戶的 email」,Terraform 看到這個引用,就知道要先建好服務帳戶、才能建 Cloud Run,順序不用你排
variables.tf:宣告 3 個變數。project_id 沒有預設值,一定要有人給;region、env 有預設值,不給就用預設。
cat > variables.tf <<'EOF'
variable "project_id" {
description = "要建立資源的 GCP 專案 ID"
type = string
}
variable "region" {
description = "資源放在哪個區域"
type = string
default = "asia-east1"
}
variable "env" {
description = "環境名稱,會變成標籤"
type = string
default = "dev"
}
EOF
outputs.tf:建好之後要印出來的 3 個值。Cloud Run 的網址要建好才知道,寫在 output 裡就不用再去 Console 找。
cat > outputs.tf <<'EOF'
output "bucket_name" {
value = google_storage_bucket.files.name
}
output "service_account_email" {
value = google_service_account.run.email
}
output "service_url" {
value = google_cloud_run_v2_service.hello.uri
}
EOF
terraform.tfvars:幫變數填值。Terraform 會自動讀這個檔名。你的專案 ID 不一樣,改這一行就好,其他檔案都不用動。
cat > terraform.tfvars <<'EOF'
project_id = "dong-dong-gcp-3"
EOF
ls
main.tf outputs.tf terraform.tfvars variables.tf
步驟 5:terraform init,下載 provider
terraform init 是每個新資料夾第一個要跑的指令:照 required_providers 下載 provider、建立 lock 檔。 換電腦、換資料夾、改了 provider 版本,都要重跑一次。
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/google versions matching "~> 8.0"...
- Installing hashicorp/google v8.12.0...
- Installed hashicorp/google v8.12.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
(後面省略)

"~> 8.0"挑到當時最新的 8.12.0- 第一行
Initializing the backend的 backend 是「state 檔放哪裡」。沒設定就是預設的本機資料夾,第 11 篇會改成 Cloud Storage - init 會產生
.terraform.lock.hcl,輸出建議把這個檔案放進 Git:同事 clone 下來跑 init,就會裝到一模一樣的 8.12.0,不會「我這邊可以、你那邊壞掉」
步驟 6:看看多了什麼,再檢查格式和語法
ls -a; du -sh .terraform; terraform fmt; terraform validate
. .. main.tf outputs.tf .terraform .terraform.lock.hcl terraform.tfvars variables.tf
135M .terraform
Success! The configuration is valid.

- 4 個指令一次跑,輸出怎麼對:第 1 行是
ls -a的結果(連名稱以.開頭的隱藏檔都列出來),第 2 行是du -sh(看資料夾佔多少空間),terraform fmt沒有印東西是正常的,最後一行是terraform validate .terraform資料夾 135 MB:裡面就是剛下載的 provider 程式。算一次:Cloud Shell 家目錄 5 GB ≈ 5,120 MB,一個資料夾 135 MB 約佔 2.6%;開 10 個練習資料夾各自 init,就吃掉約 1.3 GB。這個資料夾不用放進 Git,要用時 init 會重新下載terraform fmt:把縮排、等號對齊整理成官方格式。檔案本來就整齊時什麼都不印(這次就是);有改的話會印出改了哪個檔terraform validate:檢查語法和欄位有沒有寫錯,不會連到 GCP
步驟 7:terraform plan,先預覽
terraform plan 是「預演」:讀設定檔、讀 state、問 GCP 現況,三邊比對後列出要做的事,但完全不動手。
terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_cloud_run_v2_service.hello will be created
+ resource "google_cloud_run_v2_service" "hello" {
+ conditions = (known after apply)
(中間省略)
+ deletion_protection = true
+ effective_annotations = (known after apply)
+ effective_labels = {
+ "env" = "dev"
+ "goog-terraform-provisioned" = "true"
+ "managed-by" = "terraform"
}
(中間省略)
+ invoker_iam_disabled = true
+ labels = {
+ "env" = "dev"
+ "managed-by" = "terraform"
}
(中間省略)
+ location = "asia-east1"
+ name = "do-10-hello"
(中間省略)
+ uri = (known after apply)
(中間省略)
# google_service_account.run will be created
+ resource "google_service_account" "run" {
+ account_id = "do-10-run"
(中間省略)
+ email = "do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com"
(中間省略)
# google_storage_bucket.files will be created
+ resource "google_storage_bucket" "files" {
(中間省略)
+ force_destroy = false
(中間省略)
+ location = "ASIA-EAST1"
+ name = "do-10-dong-dong-gcp-3-files"
(中間省略)
+ public_access_prevention = "enforced"
(中間省略)
Plan: 3 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ bucket_name = "do-10-dong-dong-gcp-3-files"
+ service_account_email = "do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com"
+ service_url = (known after apply)
(分隔線省略)
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

怎麼讀:
+ create:最上面的符號說明。這次 3 個資源都是+,全部新增Plan: 3 to add, 0 to change, 0 to destroy.:最重要的一行,apply 前一定要看。數字跟你預期的不一樣,就停下來(known after apply):建好才知道的值,例如 Cloud Run 的網址urigoog-terraform-provisioned = "true":我沒寫這個標籤,是 provider 自動加的。官方說明:6.0 版起預設幫新資源加上它,方便在 Console、gcloud 認出「這是 Terraform 建的」。你寫的labels只有 2 個,effective_labels(實際會貼在資源上的)有 3 個deletion_protection = true:我也沒寫這行。先記住它,「用完記得刪」那段會被它擋下來
輸出最後還有一行提醒:沒用 -out 存下這份計畫,之後 apply 時 Terraform 會重新算一次,不保證跟現在一模一樣。一個人練習沒關係;CI/CD 自動化時,常見做法是 terraform plan -out=tfplan 存檔、審過再 terraform apply tfplan。
步驟 8:terraform apply,真的建出來
terraform apply 會先再算一次 plan、印出來,等你打 yes 才動手。打別的字(包括 y)都會取消。
apply 的工作是「讓雲端變得跟設定檔一樣」:設定檔寫了、雲端沒有的就建,設定檔還寫著的東西就算被刪掉,下次 apply 也會補回來。這點在步驟 17 會實際撞到一次。
terraform apply
(前面跟 plan 一樣,省略)
Plan: 3 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ bucket_name = "do-10-dong-dong-gcp-3-files"
+ service_account_email = "do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com"
+ service_url = (known after apply)
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:

打 yes、按 Enter:
Enter a value: yes
google_service_account.run: Creating...
google_storage_bucket.files: Creating...
google_storage_bucket.files: Creation complete after 2s [id=do-10-dong-dong-gcp-3-files]
google_service_account.run: Still creating... [00m10s elapsed]
google_service_account.run: Still creating... [00m20s elapsed]
google_service_account.run: Creation complete after 23s [id=projects/dong-dong-gcp-3/serviceAccounts/do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com]
google_cloud_run_v2_service.hello: Creating...
google_cloud_run_v2_service.hello: Still creating... [00m10s elapsed]
google_cloud_run_v2_service.hello: Creation complete after 11s [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello]
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Outputs:
bucket_name = "do-10-dong-dong-gcp-3-files"
service_account_email = "do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com"
service_url = "https://do-10-hello-b5j2wtbgca-de.a.run.app"

看建立順序:
- bucket 和服務帳戶同時開始:兩個互不相干,Terraform 會平行建
- Cloud Run 等服務帳戶建好才開始:因為
main.tf寫了service_account = google_service_account.run.email,Terraform 自己排出先後。這叫隱含相依(implicit dependency) - 算一次總時間:服務帳戶 23 秒+Cloud Run 11 秒 ≈ 34 秒;bucket 的 2 秒跟服務帳戶重疊,不用加。資源越多、相依越長,平行的好處越明顯
步驟 9:用 output 拿網址,打開看看
terraform output; curl -s "$(terraform output -raw service_url)" | grep -o '<title>.*</title>'
bucket_name = "do-10-dong-dong-gcp-3-files"
service_account_email = "do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com"
service_url = "https://do-10-hello-b5j2wtbgca-de.a.run.app"
<title>Congratulations | Cloud Run</title>

terraform output -raw service_url 只印出網址本身(不加引號),方便塞進其他指令。curl 是在終端機開網頁的工具,抓到標題「Congratulations | Cloud Run」,代表服務真的跑起來、而且不用登入就能看。
到 Console 的 Cloud Run 頁面看,do-10-hello 就在那裡,跟手動建的服務長得一模一樣。網址欄顯示的是 https://do-10-hello-600195576280.asia-east1.run.app(服務名稱+專案編號+區域),跟 Terraform 印的 ...-b5j2wtbgca-de.a.run.app 不一樣:Cloud Run 的服務有新、舊兩種網址,Console 顯示新式的,provider 的 uri 欄位給的是舊式的。步驟 9 的 curl 測的是舊式那個:

步驟 10:用 gcloud 確認,順便看標籤
gcloud run services describe do-10-hello --region=asia-east1 --format='value(metadata.labels)'; gcloud storage buckets describe gs://do-10-dong-dong-gcp-3-files --format='value(labels)'; gcloud iam service-accounts list --filter='email~^do-10-' --format='value(email)'
cloud.googleapis.com/location=asia-east1;env=dev;goog-terraform-provisioned=true;managed-by=terraform
env=dev;goog-terraform-provisioned=true;managed-by=terraform
do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com

3 個資源都在。goog-terraform-provisioned=true 真的貼上去了,之後在帳單報表、資源清單裡,用這個標籤就能篩出「哪些是 Terraform 管的」。
步驟 11:看 state 記了什麼
terraform state list; head -8 terraform.tfstate; grep -o '"creator": "[^"]*"' terraform.tfstate | sed -E 's/[a-z0-9.]+@gmail[.]com/你的帳號/'
google_cloud_run_v2_service.hello
google_service_account.run
google_storage_bucket.files
{
"version": 4,
"terraform_version": "1.17.3",
"serial": 4,
"lineage": "a1fec9c5-72ee-23d0-f15a-44987346aedd",
"outputs": {
"bucket_name": {
"value": "do-10-dong-dong-gcp-3-files",
"creator": "你的帳號"

terraform state list:state 裡記了 3 個資源,名字就是main.tf裡的「類型.小名」terraform.tfstate是 JSON(一種用大括號和「”名稱”: 值」記資料的文字格式)。serial是改版次數,每次 apply 改到 state 就加 1;lineage是這份 state 的身分證號碼,防止拿錯檔案- state 存了你的帳號:Cloud Run 的
creator欄位原原本本寫著建立者的 email(最後那個sed只是把畫面上的 email 換成「你的帳號」,檔案裡是真的 email)。這就是不能把 state 放進 Git 的原因之一;如果資源裡有資料庫密碼,也會一起寫進來 - 只有 3 個資源,這個檔案就有 337 行(步驟 12 用
wc -l數行數量的)
步驟 12:再跑一次 plan,什麼都不會做
wc -l terraform.tfstate; terraform plan
337 terraform.tfstate
google_service_account.run: Refreshing state... [id=projects/dong-dong-gcp-3/serviceAccounts/do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com]
google_storage_bucket.files: Refreshing state... [id=do-10-dong-dong-gcp-3-files]
google_cloud_run_v2_service.hello: Refreshing state... [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Refreshing state 是 Terraform 去 GCP 問每個資源現在長怎樣。設定檔、state、雲端三邊一致,就是 No changes。這個「同樣的設定跑幾次結果都一樣」的特性叫冪等(idempotent),是 IaC 很重要的性質之一:你可以放心每天跑 plan,沒事它就不動。前提是沒人在外面改過設定,而且 provider 版本被 lock 檔鎖住,不會自己換新版、換預設值。
步驟 13:換一個變數值,看 ~ 原地修改
不改檔案,用 -var 臨時把 env 換成 test:
terraform plan -var="env=test"
(Refreshing state 3 行省略)
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# google_cloud_run_v2_service.hello will be updated in-place
~ resource "google_cloud_run_v2_service" "hello" {
(中間省略)
~ labels = {
~ "env" = "dev" -> "test"
# (1 unchanged element hidden)
}
name = "do-10-hello"
(中間省略)
# google_storage_bucket.files will be updated in-place
~ resource "google_storage_bucket" "files" {
(中間省略)
~ labels = {
~ "env" = "dev" -> "test"
# (1 unchanged element hidden)
}
(中間省略)
Plan: 0 to add, 2 to change, 0 to destroy.

~ update in-place:原地修改,資源不用刪掉重建"dev" -> "test":箭頭左邊是現在、右邊是改完- 只有用到
var.env的 2 個資源會變,服務帳戶沒用到,所以不在清單裡 - 這次只 plan、不 apply。同一份設定檔,換不同的變數值就能建出 dev、test 兩套環境,這是變數最常見的用法
步驟 14:有人在 Console 偷改了,Terraform 抓得到嗎
模擬同事沒經過 Terraform、直接用 gcloud 把 bucket 的 env 標籤改成 hotfix,然後馬上 plan:
gcloud storage buckets update gs://do-10-dong-dong-gcp-3-files --update-labels=env=hotfix; terraform plan
Updating gs://do-10-dong-dong-gcp-3-files/...
(Refreshing state 3 行省略)
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# google_storage_bucket.files will be updated in-place
~ resource "google_storage_bucket" "files" {
~ effective_labels = {
~ "env" = "hotfix" -> "dev"
# (2 unchanged elements hidden)
}
id = "do-10-dong-dong-gcp-3-files"
~ labels = {
~ "env" = "hotfix" -> "dev"
# (1 unchanged element hidden)
}
(中間省略)
Plan: 0 to add, 1 to change, 0 to destroy.

抓到了。雲端現況(hotfix)跟設定檔(dev)對不上,這就是漂移(drift)。對設定檔裡有寫的屬性,Terraform 以設定檔為準,所以計畫是把它改回 dev。
要注意兩個例外:設定檔沒寫的欄位被手動改了,plan 通常不會列出來,也不會改回去;另外設定檔可以用 lifecycle { ignore_changes = [...] }(官方說明)刻意叫 Terraform 不管某些欄位。
套用,改回來:
terraform apply -auto-approve; gcloud storage buckets describe gs://do-10-dong-dong-gcp-3-files --format='value(labels)'
(plan 內容省略)
Plan: 0 to add, 1 to change, 0 to destroy.
google_storage_bucket.files: Modifying... [id=do-10-dong-dong-gcp-3-files]
google_storage_bucket.files: Modifications complete after 0s [id=do-10-dong-dong-gcp-3-files]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
(Outputs 省略)
env=dev;goog-terraform-provisioned=true;managed-by=terraform

-auto-approve 是跳過打 yes 的確認,CI/CD 自動化常用;自己手動操作時不建議,因為你就看不到計畫了。
這個例子也說明一件事:用 Terraform 管的資源,就不要再到 Console 手動改。手動改的東西,下次有人跑 apply 就會被蓋回去。真的要改,改設定檔。
步驟 15:改區域看 -/+,這次只看不做
如果把 region 從台灣換成東京(asia-northeast1)會怎樣:
terraform plan -var="region=asia-northeast1"
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# google_cloud_run_v2_service.hello must be replaced
-/+ resource "google_cloud_run_v2_service" "hello" {
(中間省略)
~ location = "asia-east1" -> "asia-northeast1" # forces replacement
(中間省略)
# google_storage_bucket.files must be replaced
-/+ resource "google_storage_bucket" "files" {
(中間省略)
~ location = "ASIA-EAST1" -> "ASIA-NORTHEAST1" # forces replacement
(中間省略)
Plan: 2 to add, 0 to change, 2 to destroy.

-/+ destroy and then create replacement:先刪掉、再建新的。有些屬性建好就不能改(bucket 的位置就是),要改只能整個重建# forces replacement:標出是哪一行逼它重建- 這是最危險的一種:bucket 被刪掉,裡面的檔案會一起不見。這次 bucket 是空的而且我只 plan,沒有 apply
- 服務帳戶不分區域,所以不受影響:2 個重建、1 個不動

看 plan 的習慣:先看最後一行的數字,再找有沒有 -/+ 和 -。有的話,確認那個資源裡有沒有不能丟的資料。
用完記得刪
這篇建了:Cloud Storage bucket、服務帳戶、Cloud Run 服務,還有 Cloud Shell 裡的 ~/do-10 資料夾和裝好的 Terraform。不刪會怎麼計費:
- Cloud Run:沒人打網址時縮到 0 台,不收運算費
- Cloud Storage bucket:空的不收錢;放了檔案,台灣
asia-east1的 Standard 儲存是每 GiB 每月 0.020 美元(2026 年 12 月查 Cloud Billing Catalog API,定價頁)。算一次:放 10 GiB 的檔案,10 × 0.020 = 每月 0.2 美元;放 1 TiB(1,024 GiB)就是每月約 20.5 美元。檔案越放越多,帳單就跟著長 - 服務帳戶:不收費,但留著就是一個沒人管的身分,該刪
步驟 16:terraform destroy,第一次失敗
terraform destroy 會把 state 裡記的資源全部刪掉,一樣要打 yes。它只刪 state 裡記的這 3 個,專案裡其他不是這份設定建的東西不會動。
terraform destroy
(每個資源要刪掉的屬性都列出來,省略)
Plan: 0 to add, 0 to change, 3 to destroy.
Changes to Outputs:
- bucket_name = "do-10-dong-dong-gcp-3-files" -> null
- service_account_email = "do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com" -> null
- service_url = "https://do-10-hello-b5j2wtbgca-de.a.run.app" -> null
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
google_storage_bucket.files: Destroying... [id=do-10-dong-dong-gcp-3-files]
google_cloud_run_v2_service.hello: Destroying... [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello]
google_storage_bucket.files: Destruction complete after 1s
╷
│ Error: cannot destroy service without setting deletion_protection=false and running `terraform apply`
│
│
╵

bucket 刪掉了,Cloud Run 刪不掉。原因就是步驟 7 看到的 deletion_protection = true:google provider 從 6.0 版起,Cloud Run 服務預設開啟「刪除保護」,防止有人一個 destroy 就把正式服務刪掉。服務帳戶因為 Cloud Run 還在用(相依),也沒輪到刪。
兩個重點:
- 這個保護只擋 Terraform。官方升級指南寫明它不會擋 Terraform 以外的刪除,在 Console 或用 gcloud 照樣刪得掉
- 錯誤訊息說得很清楚:要先把
deletion_protection改成false,而且要 apply 一次讓 state 記住,再 destroy
步驟 17:關掉刪除保護,apply 一次
這一步要在 main.tf 的 Cloud Run 那段加一行 deletion_protection = false,改完長這樣,新加的那行在 resource "google_cloud_run_v2_service" "hello" { 的大括號裡面:
resource "google_cloud_run_v2_service" "hello" {
name = "do-10-hello"
location = var.region
invoker_iam_disabled = true
deletion_protection = false
labels = {
看不懂下面的 sed 沒關係,用編輯器打開 main.tf,在 invoker_iam_disabled = true 下面手動多打這一行,結果一樣。我實測是用指令做的:sed(用指令改文字檔的工具)插入那一行,grep(找文字的工具)印出那兩行確認位置,最後 apply:
terraform state list; sed -i '/invoker_iam_disabled = true/a\ deletion_protection = false' main.tf; grep -n -A1 invoker_iam_disabled main.tf; terraform apply -auto-approve
google_cloud_run_v2_service.hello
google_service_account.run
38: invoker_iam_disabled = true
39- deletion_protection = false
(Refreshing state 省略)
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
~ update in-place
Terraform will perform the following actions:
# google_cloud_run_v2_service.hello will be updated in-place
~ resource "google_cloud_run_v2_service" "hello" {
~ deletion_protection = true -> false
id = "projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello"
name = "do-10-hello"
# (36 unchanged attributes hidden)
# (3 unchanged blocks hidden)
}
# google_storage_bucket.files will be created
(中間省略)
Plan: 1 to add, 1 to change, 0 to destroy.
(中間省略)
google_storage_bucket.files: Creating...
google_cloud_run_v2_service.hello: Modifying... [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello]
google_cloud_run_v2_service.hello: Modifications complete after 1s [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello]
google_storage_bucket.files: Creation complete after 2s [id=do-10-dong-dong-gcp-3-files]
Apply complete! Resources: 1 added, 1 changed, 0 destroyed.

這裡出了一個意外:terraform state list 只剩 2 個資源(bucket 在步驟 16 刪掉了),但 main.tf 裡 bucket 還寫著。apply 的工作是「讓雲端跟設定檔一樣」,所以它把剛刪掉的 bucket 又建了回來(1 to add)。我原本只想改刪除保護,因為用了 -auto-approve 沒看計畫就套用了。這正是步驟 14 說「手動操作不建議用 -auto-approve」的原因。你照做時,建議把最後的 terraform apply -auto-approve 換成 terraform apply,看到計畫裡的 1 to add,就知道 bucket 會被建回來,心裡有數再打 yes(步驟 18 的 destroy 會把它一起刪掉)。
步驟 18:再 destroy 一次,全部刪乾淨
terraform destroy -auto-approve
(省略)
Plan: 0 to add, 0 to change, 3 to destroy.
(中間省略)
google_storage_bucket.files: Destroying... [id=do-10-dong-dong-gcp-3-files]
google_cloud_run_v2_service.hello: Destroying... [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello]
google_storage_bucket.files: Destruction complete after 1s
google_cloud_run_v2_service.hello: Still destroying... [id=projects/dong-dong-gcp-3/locations/asia-east1/services/do-10-hello, 00m10s elapsed]
google_cloud_run_v2_service.hello: Destruction complete after 10s
google_service_account.run: Destroying... [id=projects/dong-dong-gcp-3/serviceAccounts/do-10-run@dong-dong-gcp-3.iam.gserviceaccount.com]
google_service_account.run: Destruction complete after 1s
Destroy complete! Resources: 3 destroyed.

刪的順序跟建的時候相反:Cloud Run 刪完,才輪到它用的服務帳戶。
確認 state 是空的、GCP 上也找不到 do-10- 開頭的資源:
terraform state list | wc -l; gcloud run services list --region=asia-east1 --filter='metadata.name~^do-10-' --format='value(metadata.name)'; gcloud iam service-accounts list --filter='email~^do-10-' --format='value(email)'; gcloud storage buckets list --filter='name~^do-10-' --format='value(name)'
0

步驟 19:刪掉練習資料夾
cd ~ && rm -rf ~/do-10 && ls -d ~/do-10 2>&1 | sed "s#$HOME#~#"
ls: cannot access '~/do-10': No such file or directory

.terraform 的 135 MB 也一起刪了。用 apt 裝的 Terraform 不用特別移除:它裝在家目錄以外,Cloud Shell 下次重開就不見了。我另外用 Cloud Shell 跑了系列的清理檢查腳本,專案裡沒有任何 do-10- 開頭的資源。
實測時的意外
1. Cloud Shell 不內建 Terraform 了:打
terraform version只印出安裝說明。Cloud Shell 版本資訊寫明 2026 年 6 月 20 日起移除。Google 官方繁中的 Terraform 快速入門,事前準備已經改成「請照 HashiCorp 的說明安裝」,但頁面裡的範例輸出還是 providerv5.35.0,跟這次裝到的v8.12.0差了 3 個大版本(步驟 1、5)。2. 用 apt 裝的重開就不見:只有家目錄會保存。要長期用,寫進
~/.customize_environment或把執行檔放家目錄(步驟 2)。3. provider 自己加了一個標籤:我只寫了
env、managed-by,資源上多了goog-terraform-provisioned=true,6.0 版起的預設行為(步驟 7、10)。4. Cloud Run 預設有刪除保護:plan 裡就有
deletion_protection = true,第一次 destroy 刪掉 bucket 後卡在 Cloud Run(步驟 16)。5. 關保護的那次 apply,把刪掉的 bucket 又建回來:設定檔還寫著 bucket,apply 就照設定檔補回去;加了
-auto-approve沒看到計畫(步驟 17)。6. state 裡有我的 email:Cloud Run 的
creator欄位明文存著建立者帳號(步驟 11)。7. 服務帳戶建了 23 秒:比 bucket(2 秒)、Cloud Run(11 秒)都久,Cloud Run 一直等它建好才開始(步驟 8)。
8. plan 不會特別標「這是有人在外面改的」:手動改標籤後的 plan,長得跟步驟 13 改變數時一樣,都是
~ update in-place,差別只在箭頭左邊變成hotfix。要認出漂移,得自己看箭頭左邊的值是不是你設定的(步驟 14)。
Terraform 和 OpenTofu、Infrastructure Manager、Config Connector 差在哪
新版考綱 1.2 一次列了好幾個 IaC 工具,先用一張表分清楚,細節在第 11 篇。
| 工具 | 一句話 | 誰在維護 | 寫法 |
|---|---|---|---|
| Terraform | 這篇用的,最多人用的 IaC 工具 | HashiCorp(2025 年起屬於 IBM),BSL 授權 | HCL(.tf) |
| OpenTofu | 2023 年授權改變後,社群分出來的開源版本 | 開源社群 | 跟 Terraform 幾乎一樣的 HCL |
| Infrastructure Manager | Google 代管的服務,幫你跑 Terraform、保管 state | Google Cloud | 一樣寫 Terraform |
| Config Connector | 用 Kubernetes 的 YAML 管 GCP 資源 | Google Cloud | Kubernetes YAML |
| Helm | 打包、安裝 Kubernetes 應用程式的工具 | CNCF 社群 | 範本化的 YAML |
OpenTofu 最新是 1.13.11(2026 年 11 月 30 日在 GitHub 發布,2026 年 12 月查),這篇沒有實測 OpenTofu。
Infrastructure Manager 能用的 Terraform 版本有限:沒指定時預設用免費的 1.5.7,較新的 1.16.5 要另外收管理費,都不是這篇用的 1.17.3(2026 年 12 月查官方支援版本表)。在 Cloud Shell 用新版語法寫的設定,搬過去前要先確認版本。
證照會怎麼考
Professional Cloud DevOps Engineer 考試 2 小時、50~60 題選擇題、報名費 200 美元,考試語言英文和日文(2026 年 11 月查官方考試頁)。2026 年 11 月 23 日起適用新版考綱。
| 考綱小節(新版) | 原文重點 | 這篇對應的地方 |
|---|---|---|
| 1.2 管理基礎架構 | IaC 工具與代管服務(Application Design Center、Infrastructure Manager、Config Connector、GitOps、Terraform、Helm) | 整篇;工具比較表 |
| 1.2 | 用 Google 建議的做法和藍圖改基礎架構(Terraform modules and blueprints,新版點名) | 這篇的 4 個檔案就是最小的「根模組」,modules 在第 11 篇 |
| 2.1 建置管線 | 應用程式和基礎架構的 CI/CD | plan -out 存檔、審過再 apply 的做法 |
| 1.4 管理多個執行環境 | 管理短暫環境(ephemeral environments) | 同一份設定換 env 變數;用完 terraform destroy |
| 1.5 管理雲端開發環境 | 幫環境裝好需要的工具(bootstrapping),例子點名 Cloud Shell | Cloud Shell 不內建 Terraform 了,用 ~/.customize_environment 每次開機自動裝 |
跟舊版比:1.2 新增了 Application Design Center,拿掉了 Cloud Foundation Toolkit,「藍圖」那條多寫了 Terraform modules。
看到這些字,就想到這個(情境題的判斷關鍵字):
| 題目關鍵字 | 想到 |
|---|---|
| 「環境要能重複建立」「避免手動設定」「設定要版本控制」 | IaC,Terraform 設定檔放 Git |
| 「套用前先確認會改什麼」「變更要審查」 | terraform plan(CI 裡 plan -out 給人審) |
| 「有人在 Console 手動改了設定」「設定漂移」 | 跑 terraform plan 找差異,用 apply 改回設定檔的樣子(只限設定檔有寫的屬性;lifecycle 的 ignore_changes 列出的欄位不管) |
| 「多人一起改」「state 檔衝突、遺失」 | state 放遠端(Cloud Storage)並加鎖,第 11 篇 |
| 「state 裡有敏感資料」 | state 不放 Git、限制存取權限、遠端加密 |
| 「不想自己架 Terraform 執行環境」「Google 代管」 | Infrastructure Manager |
| 「用 Kubernetes 的方式管 GCP 資源」 | Config Connector |
| 「防止 Terraform 誤刪正式服務」 | deletion_protection(只擋 Terraform)、搭配 IAM 權限控管 |
考試範圍完整的準備地圖在第 1 篇:GCP DevOps 證照怎麼考?2026 新版考綱、比重與準備地圖。
面試會怎麼問
- 「Terraform 的 plan 和 apply 差在哪?」:plan 只比對、列出要做的事,不動手;apply 才真的呼叫 API。補一句「apply 前一定先看 plan 最後一行的數字,有
-/+要特別確認資料會不會丟」 - 「state 是什麼?為什麼不能放 Git?」:Terraform 記住它管哪些資源的檔案;裡面是明文,會有帳號、密碼這類敏感值。團隊要放遠端(例如 Cloud Storage)並開鎖定,避免兩個人同時 apply
- 「有人在 Console 手動改了 Terraform 管的資源會怎樣?」:設定檔裡有寫的屬性,下次 plan 會出現差異(漂移),apply 會改回設定檔的樣子(這篇實測:手動把標籤改成 hotfix,plan 顯示
1 to change,apply 後回到 dev)。設定檔沒寫的欄位就抓不到,所以規定要改就改設定檔 - 「什麼情況 Terraform 會把資源刪掉重建?」:改了不能原地修改的屬性,例如 bucket 的位置,plan 會標
-/+和# forces replacement - 「Terraform 跟 gcloud 腳本差在哪?」:gcloud 腳本寫步驟(命令式),重跑要自己處理「已經存在」;Terraform 寫最後狀態(宣告式),重跑沒差異就
No changes
常見問答
Cloud Shell 為什麼打 terraform 沒反應,只有安裝說明?
Google Cloud 的 Cloud Shell 版本資訊寫明,2026 年 6 月 20 日起 Cloud Shell 不再內建 Terraform CLI。現在打 terraform 只會印出安裝說明。照說明用 apt 安裝(這篇實測裝到 1.17.3)就能用,但 Cloud Shell 重開後會消失;要保留,就把安裝指令寫進 ~/.customize_environment,或把執行檔放在家目錄。
Terraform 要錢嗎?
Terraform CLI 可以免費下載使用。2023 年 8 月起新版本改用 BSL 1.1 授權,限制的是拿它做跟 HashiCorp 競爭的商業服務,一般個人和公司內部使用不受影響。要付錢的是你用 Terraform 建出來的雲端資源,例如這篇的 Cloud Storage 放了檔案才照容量收費、Cloud Run 有流量才收運算費。
terraform.tfstate 可以放進 Git 嗎?
不建議。HashiCorp 官方文件寫明 state 是明文檔案,會存下設定裡的秘密值;這篇實測 Cloud Run 的 creator 欄位就存著建立者的 email。一個人練習放本機就好;團隊使用要放到有權限控管、能加鎖的遠端位置,例如 Cloud Storage。.terraform.lock.hcl(provider 版本鎖定檔)則建議放進 Git。
為什麼 terraform destroy 刪不掉 Cloud Run?
google provider 從 6.0 版起,google_cloud_run_v2_service 的 deletion_protection 預設是 true。實測第一次 destroy 回 cannot destroy service without setting deletion_protection=false and running terraform apply。做法是在設定檔加上 deletion_protection = false,先 apply 一次讓 state 記住,再 destroy。這個保護只擋 Terraform,在 Console 或用 gcloud 照樣刪得掉。
在 Console 手動改了 Terraform 建的資源會怎樣?
如果改的是設定檔裡有寫的屬性,下次 terraform plan 會列出差異,apply 會改回設定檔寫的樣子。實測用 gcloud 把 bucket 的 env 標籤改成 hotfix,plan 立刻列出 "hotfix" -> "dev"、1 to change,apply 後標籤回到 dev。設定檔沒寫的欄位被手動改了,plan 通常看不出來,所以 Terraform 管的資源,要改就改設定檔,不要手動改。
Terraform 和 OpenTofu 差在哪?
OpenTofu 是 2023 年 Terraform 改成 BSL 授權後,社群從最後一個開源版本分出來的工具,2024 年 1 月 10 日推出 1.6.0 正式版,寫法幾乎跟 Terraform 一樣。差別主要在授權和維護者:Terraform 屬於 HashiCorp(2025 年起是 IBM 的一部分),OpenTofu 是開源社群維護。這篇只實測 Terraform。
plan 裡的 +、~、-/+、- 是什麼意思?
+ 是新增、~ 是原地修改、-/+ 是先刪除再重建、- 是刪除。這篇各實測一次:第一次 plan 是 3 to add;把 env 變數改成 test 是 2 to change;把區域改成 asia-northeast1 是 2 to add, 2 to destroy;destroy 是 3 to destroy。看到 -/+ 和 - 要特別確認資料會不會跟著被刪。
重點整理
- Terraform 教學的核心是 4 個指令:
init下載 provider(實測 google 8.12.0,135 MB)、plan只預覽、apply打yes才建、destroy照 state 全部刪 - Cloud Shell 從 2026 年 6 月 20 日起不內建 Terraform:照它印的 apt 指令裝(實測 1.17.3),重開會消失,要保留就寫進
~/.customize_environment或放家目錄(家目錄 120 天沒用會被刪) - state 是 Terraform 的記帳本:3 個資源就有 337 行 JSON,還明文存著建立者 email,不能放 Git;團隊用的遠端 state 在第 11 篇
- plan 最後一行先看:
+新增、~原地修改、-/+砍掉重建(bucket 的檔案會一起不見)、-刪除;有人手動改了設定檔裡有寫的屬性,plan 會抓出漂移 - 刪除有兩個坑:Cloud Run 預設
deletion_protection = true,要改false並 apply 一次;那次 apply 也會把已刪掉、但設定檔還寫著的資源重建回來
延伸閱讀
本系列
- 第 1 篇:GCP DevOps 證照怎麼考?2026 新版考綱、比重與準備地圖
- 第 3 篇:CI/CD 是什麼?持續整合、持續交付、持續部署差在哪
- 第 9 篇:Ops 名詞總表:DevOps、SRE、Platform Engineering、FinOps 差在哪
- 第 11 篇:Terraform 進階:state 放 Cloud Storage、modules、Infrastructure Manager
- 第 13 篇:Cloud Build 串 GitHub/GitLab:push 就自動建置部署
這篇用到的服務
- Cloud Run 是什麼?Cloud Run 教學:部署容器、版本與流量分割
- Cloud Run 收費怎麼算?Cloud Run pricing、冷啟動、最小執行個體一次看懂
- GCP 的 Cloud IAM:角色與權限控管介紹
- 使用 GCP Service Account 開發程式的小技巧和注意事項
站上相關文章
一個人用 Terraform 建好環境之後,下一步是讓團隊一起用:state 放到 Cloud Storage、把重複的設定包成模組,第 11 篇接著做。可以看看東東的 GCP 課程,或加 LINE 聊聊你現在的狀況,一起找出最適合你的學習順序。


