创建 Terraform 配置
为组织选择 Service Catalog 解决方案时,您可以创建 Terraform 配置,您的用户可以使用 Terraform 部署该配置。创建配置后,您可以将其分配给目录,从而与用户共享。
在本指南中,您将使用 Cloud Build 在最新版本的 Terraform Docker 映像中运行 Terraform 命令,并使用 Cloud Storage 存储和管理 Terraform 资源,例如模块和状态文件。
如需了解有关使用 Terraform 的资源和指南,请参阅将 Terraform 与 Google Cloud搭配使用。
准备工作
您必须拥有以下 Identity and Access Management (IAM) 角色:
- 与已启用 Service Catalog 的项目关联的 Google Cloud 组织的 Catalog Admin 或 Catalog ManagerGoogle Cloud 。如果您没有此角色,请与 Organization Administrator 联系以申请访问权限。
- 您要创建解决方案的 Google Cloud 项目的 Storage Admin 角色。如果您的 Terraform 配置位于其他项目中,您还必须拥有包含 Terraform 配置的项目的 Storage Admin 角色。
- 要在其中创建解决方案的项目的 Cloud Build Editor。 Google Cloud
如果您打算使用自己的服务账号,则还必须对包含您打算使用的服务账号的项目具有 Service Account User 角色。 Google Cloud
如果您不具备这些角色,请联系您的组织管理员以请求访问权限。
如果您打算使用自己的服务账号,则该服务账号必须具有以下 IAM 角色:
- 您要创建解决方案的 Google Cloud 项目的 Storage Admin 角色。如果您的 Terraform 配置位于其他项目中,您的服务账号还必须具有包含 Terraform 配置的项目的 Storage Object Viewer 角色。
- 您要创建解决方案的Google Cloud 项目的日志写入器。
如果您不具备这些角色,请联系您的组织管理员以请求访问权限。
在您要在其中创建配置的项目中设置 Cloud Build:
-
Enable the Cloud Build API.
启用 API 后,系统会自动创建您在下一步中使用的 Cloud Build 服务账号。 - 前往 Cloud Build 页面。
- 在左侧面板中,选择设置。
- 使用服务账号电子邮件地址部分下提供的服务账号。
- 如果您不使用自己的服务账号,请向该服务账号授予以下 IAM 角色。如需了解如何授予角色的步骤,请参阅配置 Cloud Build 服务账号的访问权限。
- Project Editor (
roles/editor
) - Storage Admin (
roles/storage.admin
)
- Project Editor (
-
将配置文件上传到 Cloud Storage
您可以使用 Cloud Storage 为 Service Catalog 管理 Terraform 配置文件。
创建 Cloud Storage 存储桶
如需设置 Cloud Storage,请在您为组织启用了 Service Catalog 的项目中创建存储桶:
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
-
In the Get started section, do the following:
- Enter a globally unique name that meets the bucket naming requirements.
- To add a
bucket label,
expand the Labels section ( ),
click add_box
Add label, and specify a
key
and avalue
for your label.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
In the Replication settings section, click Configure to configure settings for the replication job.
The Configure cross-bucket replication pane appears.
- To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
- To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
- Click Done.
-
In the Choose how to store your data section, do the following:
- Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
-
In the Choose how to protect object data section, do the
following:
- Select any of the options under Data protection that you
want to set for your bucket.
- To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
- To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
- To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
- To enable Object Retention Lock, click the Enable object retention checkbox.
- To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
- To choose how your object data will be encrypted, expand the Data encryption section (Data encryption method. ), and select a
- Select any of the options under Data protection that you
want to set for your bucket.
-
In the Get started section, do the following:
- Click Create.
启用对象版本控制
您必须对存储分区使用对象版本控制,以防止 Terraform 配置被删除或覆盖。如需启用对象版本控制,请参阅有关使用对象版本控制的 Cloud Storage 文档。
授予对存储桶的访问权限
当组织中的用户部署配置时,他们的 Cloud Build 服务账号必须拥有存储分区的读取权限。如果您的存储桶存储在用户部署配置的 Google Cloud 项目中,则服务账号已经具有此访问权限。
如果您的用户要在其他Google Cloud 项目中部署配置,请使用以下任一方法授予对存储桶的访问权限:
将 Storage Object Viewer (
roles/storage.objectViewer
) 角色授予用户的 Cloud Build 服务账号,或者用户在其中部署 Terraform 配置的资源(例如项目、文件夹或组织)。 Google Cloud使用访问控制列表 (ACL) 管理对存储分区的访问权限。
如需详细了解如何管理对存储分区的访问权限,请参阅 Cloud Storage 的访问权限控制概览。
创建和上传 Terraform 模块
设置 Cloud Storage 存储分区后,您必须创建并上传 Terraform 模块,该模块是您的所有配置文件的容器。Service Catalog 使用该模块自动生成 JSON 架构文件,以定义配置的变量。
调用当前目录之外的模块时,请使用远程路径,而不是相对路径。例如,使用 source = "GoogleCloudPlatform/cloud-run/google"
取代 source = "../../"
。
如需了解常见 Google Cloud 用例的模块,请参阅适用于 Google Cloud的 Terraform 蓝图和模块。
以下代码示例展示了 Terraform 配置文件 main.tf
:
variable "machine_type" {
type = string
default = "n1-standard-1"
}
variable "zone" {
type = string
default = "us-central1-a"
}
variable "deployment_identifier" {
description = "The unique name for your instance"
type = string
}
resource "google_compute_instance" "default" {
name = "vm-${var.deployment_identifier}"
machine_type = var.machine_type
zone = var.zone
boot_disk {
device_name = "boot"
auto_delete = true
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
}
创建模块后,将文件压缩为 zip 文件。确保该模块存储在 zip 文件的根目录下。
为确保在压缩 Terraform 文件时正确存储模块,请运行以下命令:
none
zip solution.zip file1.tf file2.tf file3.tf
然后,将 zip 文件上传到您的存储分区。如需了解上传 zip 文件的步骤,请参阅有关上传对象的 Cloud Storage 文档。
在 Service Catalog 中创建配置
使用 Terraform 模块设置 Cloud Storage 存储分区后,您可以创建一个包含该存储桶的 Service Catalog 解决方案。
如需创建 Terraform 配置作为 Service Catalog 解决方案,请执行以下操作:
前往 Google Cloud 控制台中的 Service Catalog“管理”解决方案页面。
前往“解决方案”页面如需选择 Google Cloud 项目,请点击选择。
点击创建解决方案。在下拉列表中,选择创建 Terraform 配置。
为您的 Terraform 配置输入名称、说明和标语。标语是用户在浏览 Service Catalog 时看到的解决方案简短说明。
在指向 Terraform 配置的链接字段中,提供包含 Terraform 模块的 zip 文件的 Cloud Storage 存储分区的链接,例如
gs://my-terraform-bucket/my-zip-file.zip
。(可选)上传解决方案的图标。 建议的图标尺寸为 80 × 80 像素。
(可选)输入创建者的支持链接和联系信息。
(可选)添加关联到解决方案文档的链接。
选择您要用于部署解决方案的 Terraform 版本。
(可选)您可以点击选择或输入自己的服务账号旁边的复选框,提供自己的服务账号。如果您的服务账号位于您创建解决方案的 Google Cloud 项目中,请选择从当前项目中选择服务账号,然后从下拉菜单中选择您的服务账号。如果您的服务账号位于与您的解决方案不同的 Google Cloud 项目中,请选择输入任何项目中的服务账号电子邮件地址,然后输入服务账号的电子邮件地址。
点击创建。
解决方案已创建完毕,并显示在 Service Catalog“管理”解决方案页面上。
以下屏幕截图演示了如何创建 Terraform 配置:

后续步骤
- 将 Terraform 配置分配给目录,以便组织中的用户能够访问和部署解决方案。
- 使用最新 Terraform 配置详细信息更新解决方案。