最后审核时间:2026年5月
使用原生 Terraform 构建 CDL 考试中的 AWS 服务——每次构建一个代码块,并紧扣考试领域。相同的代码可在 OpenTofu 上运行。
到本实验结束时,您将使用纯 Terraform 预置最小的真实 GCP 足迹——启用两个项目服务,一个具有统一桶级访问权限 + 生命周期管理功能的 Cloud Storage 桶,以及一个在支出超过阈值时发送电子邮件的 Cloud Billing 预算。这相当于 GCP 中创建一个 AWS 账户并放入一个 S3 桶的四个代码块。
将这些代码片段放入一个 main.tf 文件中,运行 terraform init,然后逐步运行 terraform apply。
>= 1.5 或 OpenTofu >= 1.6。provider 代码块中。gcloud auth application-default login。gcloud beta billing accounts list 查找。terraform apply 之前,请将以下代码片段中的 your-project-id 和 your-billing-account-id 替换为实际值。在此范围内,所有服务均免费:
us-* 区域每月有 5 GB 免费额度;实验桶不存储任何数据。该实验每月闲置成本约为 0 美元。其目的是证明计费预警机制有效——整个 CDL 考试都围绕着 安全操作 GCP 的技能集,其中“了解您的支出”是第一条规则。
GCP 要求在配置资源之前显式启用项目服务 (API)。我们启用了 storage.googleapis.com(用于第 2 步)和 billingbudgets.googleapis.com(用于第 4 步)。与 AWS 中 API 界面始终开启不同,GCP 要求您按项目选择启用。
将 your-project-id 替换为您的实际项目 ID。
terraform {
required_version = ">= 1.5"
required_providers {
google = { source = "hashicorp/google", version = "~> 6.0" }
}
}
provider "google" {
project = "your-project-id" # REPLACE with your GCP project ID
region = "us-central1"
}
locals {
labels = {
project = "certlabpro-cdl"
managed_by = "terraform"
}
}
resource "google_project_service" "storage" {
service = "storage.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "budgets" {
service = "billingbudgets.googleapis.com"
disable_on_destroy = false
}Cloud Storage 是 GCP 中与 S3 等效的对象存储服务,提供区域级、多区域级和双区域级复制选项。CDL 考试会反复测试“选择哪种存储类别”的问题:标准(热存储)、近线(>30 天)、冷线(>90 天)、归档(>365 天)。
我们启用了统一桶级访问权限(CDL 推荐的安全默认设置——禁用细粒度的对象级 ACL,转而仅使用 IAM)以及一个 30 天 → 近线的生命周期转换规则。桶名称在所有 GCP 中必须是全局唯一的——我们使用随机十六进制后缀。
resource "random_id" "suffix" {
byte_length = 4
}
resource "google_storage_bucket" "main" {
name = "certlabpro-cdl-${random_id.suffix.hex}"
location = "US"
uniform_bucket_level_access = true
force_destroy = true # lab-only — never in production
lifecycle_rule {
condition {
age = 30
}
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
}
labels = local.labels
depends_on = [google_project_service.storage]
}GCP 的 Cloud Logging 默认对大多数服务启用——但 CDL 考试会测试 必需 与 默认 与 数据访问 审计日志的区别。必需审计日志(管理员活动)始终开启、免费且无法禁用。默认日志(某些服务的数据写入)默认开启但可以禁用。数据访问日志(数据读取)默认关闭——必须显式启用并正常计费。
我们为 Cloud Storage 启用数据访问日志,以便第 2 步中桶内每个对象的每次读取都会记录到 Cloud Logging 中。本实验是 iam-audit-config 原语的演示;生产部署通过 [[gcp-pcse]] 模式在组织级别使用此模式。
resource "google_project_iam_audit_config" "storage_data_access" {
service = "storage.googleapis.com"
audit_log_config {
log_type = "DATA_READ"
}
audit_log_config {
log_type = "DATA_WRITE"
}
}CDL 考试非常喜欢这种模式——Cloud Billing 预算 + 警报是 CDL 考试中关于成本控制的常见题型。我们设置每月 10 美元的预算,并配置在预测支出的 50% / 90% / 100% 时发出警报。警报会通过电子邮件发送给计费账户管理员(电子邮件无需额外配置;对于 Slack / PagerDuty,您需要添加 Pub/Sub 主题 + Cloud Function 扇出)。
将 your-billing-account-id 替换为您的实际 ID。预算通过 projects 筛选器仅限于当前项目——CDL 考试会测试这种按项目预算与组织范围预算的区别。
data "google_project" "current" {}
resource "google_billing_budget" "monthly_10" {
billing_account = "your-billing-account-id" # REPLACE — find via `gcloud beta billing accounts list`
display_name = "certlabpro-cdl-$10-monthly"
budget_filter {
projects = ["projects/${data.google_project.current.number}"]
}
amount {
specified_amount {
currency_code = "USD"
units = "10"
}
}
threshold_rules {
threshold_percent = 0.5
spend_basis = "FORECASTED_SPEND"
}
threshold_rules {
threshold_percent = 0.9
spend_basis = "FORECASTED_SPEND"
}
threshold_rules {
threshold_percent = 1.0
spend_basis = "CURRENT_SPEND"
}
depends_on = [google_project_service.budgets]
}terraform destroy 会干净地拆除所有资源。桶将被销毁(仅限实验的 force_destroy = true 允许 Terraform 在桶非空时也将其删除——切勿在生产环境中使用此设置)。预算将解除关联;电子邮件警报停止。IAM 审计配置将恢复为默认。项目服务保持启用状态(我们设置 disable_on_destroy = false ——它们免费且可以保持启用,禁用它们可能会破坏同一项目上不相关的负载)。
CDL 涵盖了本实验无法容纳的许多 GCP 服务——Compute Engine 虚拟机、GKE 集群、Cloud Run、Cloud Functions、App Engine、BigQuery、Cloud SQL、Spanner、Bigtable、Firestore、Cloud Pub/Sub、Dataflow、Dataproc、Vertex AI、Cloud Build、Cloud Deploy、Anthos / 多云、Cloud CDN / Cloud Armor、VPC + Cloud NAT、Cloud Interconnect、Cloud IAM / Identity、Cloud KMS、Security Command Center 以及完整的 GCP 市场。
我们专注于 存储 + 日志 + 计费 等基本原语,因为它们是每个 CDL 考试场景都假设已存在的最低通用足迹。其他所有 GCP 服务都会写入 Cloud Storage / 从 Cloud Storage 读取 / 将日志写入 Cloud Logging / 显示在 Cloud Billing 控制面板上。掌握基础;之后再叠加专业服务。
有关按服务划分的概念性覆盖范围,请参阅此认证页面的 浏览、手册 和 Editorial 部分。