使用 Terraform 核發憑證
瞭解如何搭配使用 Terraform 與憑證授權單位服務,執行下列操作:
- 建立 CA 集區,並在新的 CA 集區中建立根 CA。
- 建立 CA 集區,並在其中建立由根 CA 簽署的從屬 CA
- 產生新的憑證簽署要求 (CSR)。
- 使用產生的 CSR,向包含從屬 CA 的 CA 集區要求憑證。
Terraform 是一項開放原始碼軟體,可讓您使用基礎架構即程式碼範例,建立及管理 CA Service 資源。本快速入門導覽課程會使用 Terraform 的 Google Cloud Terraform 供應商。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
確認您具備 CA 服務管理員 (roles/privateca.admin
) IAM 角色。如果您沒有這個 IAM 角色,請參閱「授予單一角色」,瞭解如何授予這個角色。
建立 Google Cloud 專案
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the CA Service API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the CA Service API.
- 啟動 Cloud Shell。
-
設定要套用 Terraform 設定的預設 Google Cloud 專案。
每項專案只需要執行一次這個指令,且可以在任何目錄中執行。
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
如果您在 Terraform 設定檔中設定明確值,環境變數就會遭到覆寫。
-
在 Cloud Shell 中建立目錄,並在該目錄中建立新檔案。檔案名稱的副檔名必須是
.tf
,例如main.tf
。在本教學課程中,這個檔案稱為main.tf
。mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
如果您正在學習教學課程,可以複製每個章節或步驟中的範例程式碼。
將範例程式碼複製到新建立的
main.tf
。視需要從 GitHub 複製程式碼。如果 Terraform 程式碼片段是端對端解決方案的一部分,建議您使用這個方法。
- 查看並修改範例參數,套用至您的環境。
- 儲存變更。
-
初始化 Terraform。每個目錄只需執行一次這項操作。
terraform init
如要使用最新版 Google 供應商,請加入
-upgrade
選項:terraform init -upgrade
-
檢查設定,確認 Terraform 即將建立或更新的資源符合您的預期:
terraform plan
視需要修正設定。
-
執行下列指令,並在提示中輸入
yes
,即可套用 Terraform 設定:terraform apply
等待 Terraform 顯示「Apply complete!」訊息。
- 開啟 Google Cloud 專案即可查看結果。在 Google Cloud 控制台中,前往 UI 中的資源,確認 Terraform 已建立或更新這些資源。
- 進一步瞭解如何使用 Cloud Shell 執行
gcloud
指令。 - 進一步瞭解如何搭配使用 Terraform 與 Google Cloud。
- 進一步瞭解如何搭配使用 Terraform 與 CA 服務。
- 參閱 Terraform 說明文件,瞭解 CA 服務支援。
- 開始使用 Google Cloud Provider。
安裝 Google Cloud CLI
如果尚未安裝 Google Cloud CLI,請先安裝。 系統提示時,請選擇先前選取或建立的專案。
如果您已安裝 Google Cloud CLI,請執行 gcloud components update
指令來更新:
gcloud components update
Terraform 設定範例
執行 Terraform 設定檔
如要在 Google Cloud 專案中套用 Terraform 設定,請完成下列各節的步驟。
準備 Cloud Shell
準備目錄
每個 Terraform 設定檔都必須有自己的目錄 (也稱為根模組)。
套用變更
清除
如要避免系統向您的 Google Cloud 帳戶收取本快速入門導覽課程所用資源的費用,請刪除 CA 集區和 Terraform 設定檔中定義的所有資源:
terraform destroy
系統顯示提示訊息時,請輸入 yes
。
如果您特地為此快速入門導覽課程建立專案,且已不再需要此專案,請刪除專案。