使用 Terraform 启用 Access Approval

Terraform 是一种开源的基础架构即代码软件工具,可让您管理访问审批请求。借助 Terraform,您可以执行使用 Access Approval API 可以执行的所有操作。

本页介绍了如何使用 Terraform 启用 Access Approval。 本教程使用 Google Cloud Terraform 提供程序

目标

本教程将介绍如何创建 Terraform 配置文件,该文件可执行以下操作:

  • 设置 Access Approval 审批请求通知的电子邮件地址。
  • 为所有受支持的 Google Cloud 产品启用 Access Approval。如需查看 Access Approval 支持的 Google Cloud 产品的完整列表,请参阅支持的服务

准备工作

  • 要使用 Access Approval 和 Access Transparency,您的组织必须满足特定的支持要求。如需了解详情,请参阅使用访问审批的要求
  • 为组织启用 Access Transparency。如需了解详情,请参阅启用 Access Transparency
  • 确保您拥有 Access Approval Config Editor (roles/accessapproval.configEditor) Identity and Access Management (IAM) 角色。如需详细了解访问审批的 IAM 角色,请参阅访问审批角色

创建 Google Cloud 项目

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Enable the Access Approval API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

安装 Google Cloud CLI

安装 Google Cloud CLI,然后 使用联合身份登录 gcloud CLI。 登录后,运行以下命令来初始化 Google Cloud CLI:

gcloud init

出现提示时,请选择您之前选择或创建的项目。

如果您已安装 Google Cloud CLI,请使用以下命令进行更新:

gcloud components update

创建 Terraform 配置文件

  1. 打开 Cloud Shell 以启动独立的 Cloud Shell 会话。
  2. 打开一个工作区。
  3. 创建新文件夹。
  4. 向此文件夹添加一个名为 main.tf 的 Terraform 配置文件。
  5. 复制以下资源,并将其粘贴到 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_1email_2:提供您要设为相应项目访问请求审核者的用户的电子邮件地址。
    • parent_value:您要在其中创建 my_folder 文件夹的文件夹的名称。如需详细了解文件夹,请参阅创建和管理文件夹

运行 Terraform 配置文件

在 Cloud Shell 中运行以下命令。

  1. 在目录中初始化 Terraform。

    terraform init
    
  2. 运行创建的 Terraform 配置文件。

    terraform apply
    
  3. 当系统提示您确认是否要运行配置文件时,请输入 yes

如需详细了解如何通过 Terraform 操作 Access Approval,请参阅以下 Terraform 文档:google_folder_access_approval_settings

后续步骤