最后审核时间:2026年5月
使用原生 Terraform 构建 AZ-204 考试中的 AWS 服务——每次构建一个代码块,并紧扣考试领域。相同的代码可在 OpenTofu 上运行。
通过本实验,您将使用纯 Terraform 预置经典的 Azure PaaS Web 应用程序 — 一个带有托管标识的 App Service 计划 + Linux Web 应用、一个用于存储状态的 Cosmos DB 账户、一个用于遥测的 Application Insights 工作区,以及一个应用程序可用于 Blob/文件工作的存储账户。共有五个模块;这是 AZ-204 的参考架构。
将代码片段放入单个 main.tf 文件中,运行 terraform init,然后逐步运行 terraform apply。
>= 1.5 或 OpenTofu >= 1.6。az login)。az webapp deploy)。本实验预置的是基础设施外壳 — 代码部署属于 AZ-204 的 实施 DevOps 领域,不在此 Terraform 范围之内。使用 B1 计划时,整个堆栈闲置每月约 13 美元。若要免费测试,请切换到 F1(在步骤 3 中移除托管标识)或及时销毁资源。
标准的 Azure 开场。App Service Web 应用名称必须是全局唯一的(作为 <name>.azurewebsites.net 发布到 DNS)— random_id 可以避免冲突。
terraform {
required_version = ">= 1.5"
required_providers {
azurerm = { source = "hashicorp/azurerm", version = "~> 4.0" }
random = { source = "hashicorp/random", version = "~> 3.6" }
}
}
provider "azurerm" {
features {}
}
resource "random_id" "suffix" {
byte_length = 3
}
locals {
tags = {
Project = "certlabpro-az-204"
ManagedBy = "terraform"
}
}
resource "azurerm_resource_group" "main" {
name = "certlabpro-az-204-rg"
location = "eastus"
tags = local.tags
}当问题提到 大规模单位数毫秒延迟、全球分布式 或 具有 SQL 般查询功能的 NoSQL 时,AZ-204 期望您选择 Cosmos DB。无服务器容量模式是成本优化的默认选项 — 按 RU 付费,闲置时免费,在考试中被测试为低流量/开发环境的正确选择。
我们创建一个账户,其下设一个数据库,以及一个带有分区键的容器。分区键的选择(此处为 /userId)是 AZ-204 Cosmos 中最常考的问题 — 错误的分区键会导致热分区;好的分区键则能均匀分布负载。考试会通过基数场景来测试这种模式。
resource "azurerm_cosmosdb_account" "main" {
name = "certlabpro-az-204-${random_id.suffix.hex}"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
offer_type = "Standard"
kind = "GlobalDocumentDB"
capabilities {
name = "EnableServerless"
}
consistency_policy {
consistency_level = "Session"
}
geo_location {
location = azurerm_resource_group.main.location
failover_priority = 0
}
tags = local.tags
}
resource "azurerm_cosmosdb_sql_database" "app" {
name = "appdb"
resource_group_name = azurerm_resource_group.main.name
account_name = azurerm_cosmosdb_account.main.name
}
resource "azurerm_cosmosdb_sql_container" "users" {
name = "users"
resource_group_name = azurerm_resource_group.main.name
account_name = azurerm_cosmosdb_account.main.name
database_name = azurerm_cosmosdb_sql_database.app.name
partition_key_paths = ["/userId"]
}AZ-204 的 实施 Azure 安全 和 监视、故障排除和优化 领域都依赖于 Application Insights — 这是一个用于分布式跟踪、依赖项跟踪、异常捕获和实时指标的 APM 服务。现代 Application Insights 实例需要一个 Log Analytics 工作区作为其数据后端(基于工作区 的模式;经典模式已被弃用)。
我们同时创建这两者,并通过应用程序设置在步骤 4 中将检测密钥 + 连接字符串作为 Application Insights 属性接入 App Service。
resource "azurerm_log_analytics_workspace" "main" {
name = "certlabpro-az-204-logs"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
sku = "PerGB2018"
retention_in_days = 30
tags = local.tags
}
resource "azurerm_application_insights" "main" {
name = "certlabpro-az-204-appi"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
workspace_id = azurerm_log_analytics_workspace.main.id
application_type = "web"
tags = local.tags
}App Service 是 AZ-204 首选的 PaaS Web 主机。App Service 计划是计算容器(可以将其视为抽象层下的 EC2);其中的 Web 应用共享该计划的计算资源。我们使用 Linux B1 计划和一个带有 Node.js 20 运行时 的单个 Web 应用 — 您可以根据您的技术栈切换到 python、dotnet、java 或 php。
系统分配的托管标识是 AZ-204 中应用程序访问其他 Azure 服务(Key Vault、Storage、Cosmos DB)时无需密码凭证的解决方案。一旦授予这些资源的 RBAC 角色,App Service 就会使用 Entra 颁发的令牌调用它们 — 无需在应用程序设置中存储连接字符串。
app_settings 块注入了环境变量:Application Insights 连接字符串、Cosmos 终结点和一个标志。Web 应用启动时是一个空壳 — 代码会单独部署。本实验的重点是正确配置基础设施。
resource "azurerm_service_plan" "main" {
name = "certlabpro-az-204-plan"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
os_type = "Linux"
sku_name = "B1"
tags = local.tags
}
resource "azurerm_linux_web_app" "main" {
name = "certlabpro-az-204-${random_id.suffix.hex}"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_service_plan.main.location
service_plan_id = azurerm_service_plan.main.id
https_only = true
site_config {
always_on = true
application_stack {
node_version = "20-lts"
}
}
identity {
type = "SystemAssigned"
}
app_settings = {
APPLICATIONINSIGHTS_CONNECTION_STRING = azurerm_application_insights.main.connection_string
COSMOS_ENDPOINT = azurerm_cosmosdb_account.main.endpoint
WEBSITES_ENABLE_APP_SERVICE_STORAGE = "false"
}
tags = local.tags
}
# Grant the web app's managed identity Cosmos DB data-plane access
# (Cosmos DB Built-in Data Contributor — the AZ-204 password-less pattern).
resource "azurerm_cosmosdb_sql_role_assignment" "webapp" {
resource_group_name = azurerm_resource_group.main.name
account_name = azurerm_cosmosdb_account.main.name
role_definition_id = "${azurerm_cosmosdb_account.main.id}/sqlRoleDefinitions/00000000-0000-0000-0000-000000000002"
principal_id = azurerm_linux_web_app.main.identity[0].principal_id
scope = azurerm_cosmosdb_account.main.id
}每个 Azure PaaS Web 应用最终都需要 Blob/文件存储 — 上传文件、用户头像、生成的 PDF、处理后的图片。我们使用标准的安全默认设置预置该账户,并授予应用程序的托管标识 Storage Blob Data Contributor 角色,这样应用程序就可以在没有连接字符串的情况下读写 Blob。
这完成了 AZ-204 开发 Azure 计算解决方案 的闭环:应用程序 + 状态 (Cosmos) + 遥测 (App Insights) + Blob 存储,所有这些都通过托管标识而不是密钥连接起来。每一个额外的 AZ-204 模式(Service Bus 队列、Event Grid 主题、Key Vault 密钥、Logic App 集成)都以相同的方式连接 — 在正确的范围内给予托管标识正确的角色。
resource "azurerm_storage_account" "app_blobs" {
name = "az204app${random_id.suffix.hex}"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
account_tier = "Standard"
account_replication_type = "LRS"
account_kind = "StorageV2"
https_traffic_only_enabled = true
min_tls_version = "TLS1_2"
allow_nested_items_to_be_public = false
tags = local.tags
}
resource "azurerm_role_assignment" "webapp_blobs" {
scope = azurerm_storage_account.app_blobs.id
role_definition_name = "Storage Blob Data Contributor"
principal_id = azurerm_linux_web_app.main.identity[0].principal_id
}terraform destroy 会销毁所有资源。App Service 计划 B1 是 24/7 全天候计费的项目(每月约 13 美元)。如果您错误地缩减了 Web 应用,该计划即使在 Web 应用移除后仍会保留在资源组中 — terraform destroy 会同时移除两者。Cosmos DB 无服务器和 Application Insights 在销毁后会立即停止计费。
AZ-204 涵盖了本实验无法包含的更多开发人员服务 — Azure Functions(事件驱动的无服务器)、Container Apps、AKS、Service Bus、Event Grid、Event Hubs、Logic Apps、Durable Functions、API Management、Front Door、CDN、Notification Hubs、SignalR,以及用于密钥存储的 Azure Key Vault(托管标识后的常见 AZ-204 后续内容)。
我们坚持使用 App Service + Cosmos + App Insights + Storage 基线,因为它是考试中最常考的 PaaS Web 应用形式 — 并且每个其他的 AZ-204 模式(Functions、Service Bus、Key Vault)都通过托管标识 + RBAC 连接到此基础。
对于上述内容,请参阅此认证页面中的 浏览、手册 和 Editorial 部分。