使用 Terraform 啟用存取權核准
Terraform 是一項開放原始碼的基礎架構即程式碼軟體工具,可讓您管理存取核准要求。您可以使用 Terraform 執行所有可透過存取權核准 API 執行的動作。
本頁說明如何使用 Terraform 啟用存取權核准。本教學課程使用 Google Cloud Terraform Provider。
目標
本教學課程說明如何建立 Terraform 設定檔,以執行下列操作:
- 設定存取權核准要求通知的電子郵件地址。
- 為所有支援的 Google Cloud 產品啟用「存取權核准」功能。如要查看 Access Approval 支援的完整 Google Cloud 產品清單,請參閱「支援的服務」。
事前準備
- 如要使用 Access Approval 和資料存取透明化控管機制,貴機構必須符合特定支援條件。詳情請參閱「使用存取核准的規定」。
- 為貴機構啟用資料存取透明化控管機制。詳情請參閱「啟用資料存取透明化控管機制」。
- 確認您具備存取權核准設定編輯者 (
roles/accessapproval.configEditor) Identity and Access Management (IAM) 角色。如要進一步瞭解存取核准的 IAM 角色,請參閱「存取核准角色」。
建立 Google Cloud 專案
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Access Approval API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Access Approval API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
安裝 Google Cloud CLI
安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:
gcloud init若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
系統提示時,請選擇先前選取或建立的專案。
如已安裝 Google Cloud CLI,請使用下列指令更新:
gcloud components update
建立 Terraform 設定檔
- 開啟 Cloud Shell,啟動獨立的 Cloud Shell 工作階段。
- 開啟工作區。
- 建立新資料夾。
- 在這個資料夾中新增名為
main.tf的 Terraform 設定檔。 複製下列資源,然後貼到
main.tf檔案中。main.tf
variable "parent_value" { type = string } variable "email_1" { type = string } variable "email_2" { type = string } resource "google_folder" "my_folder" { display_name = "my-folder" parent = var.parent_value # parent = "organizations/123456789" } resource "google_folder_access_approval_settings" "folder_access_approval" { folder_id = google_folder.my_folder.folder_id notification_emails = [var.email_1, var.email_2] enrolled_services { cloud_product = "all" } }輸入下列變數的值:
email_1和email_2:提供要設為這個專案存取要求審查者的使用者電子郵件地址。
parent_value:要建立my_folder資料夾的資料夾名稱。如要進一步瞭解資料夾,請參閱建立及管理資料夾。
執行 Terraform 設定檔
在 Cloud Shell 中執行下列指令。
在目錄中初始化 Terraform。
terraform init執行建立的 Terraform 設定檔。
terraform apply系統提示您確認是否要執行設定檔時,請輸入 yes。
如要進一步瞭解如何使用 Terraform 操作存取核准,請參閱這份 Terraform 文件:google_folder_access_approval_settings。
後續步驟
- 搭配使用 Terraform 和 Google Cloud
- 搭配存取權核准使用 Terraform
- 開始在 Google Cloud中使用 Terraform
- 透過 Cloud Shell 啟動 Terraform Google Cloud