系列專欄:從 AWS 視角征服 Azure:AZ-900 30 天通關實戰
難度指數:★★☆☆☆
核心考點:Infrastructure as Code (IaC), Declarative (宣告式) vs Imperative (命令式), ARM Templates (JSON 結構與五大區塊), Azure Bicep (專屬 DSL), AWS CloudFormation ↔ ARM Template 對照, AWS CDK ➔ Bicep 抽象演化, 冪等性 (Idempotency), 部署模式 (Incremental vs Complete) 與爆炸半徑, 範本機密防護 (Key Vault / SecureString), What-If 變更預覽 (對照 AWS Change Sets)
今天更新了撰文規則,使用的是 ANTIGRAVITY 2.0 也就是以agent mode 為主的生成文章,對標 kiro-ide 就是agent focus 。 並擷取了一段 aws saa domain1 的影片解說文字,增加詳細程度,
Hi, and welcome back. Let's begin with the first task statement from Domain one Design Secure Access to AWS Resources. We just mentioned that security is one of the biggest and earliest considerations you will make when designing an architecture. This includes defining how people, tools, and applications you build will access the necessary AWS services and your data. This could involve determining not only who or what can launch or terminate your resources but also managing how and when access is given along with operational permissions and almost anything else that would involve calls to the services. In the course introduction we mentioned the importance of understanding fundamentals specifically the Shared Responsibility Model, the AWS global infrastructure, and different AWS service resilience. This task statement covers securing access to your AWS resources and securing access to services in different types of cloud environments. Ensure you know the differences between public, private, hybrid, and multi-cloud environments and how to design secure access to all of them. One area that is often overlooked when studying for this certification exam is AWS accounts. AWS accounts may seem too basic to take the time to understand, but I consider it part of the required, need-to-know fundamentals for AWS. You must understand what accounts are and how they work to ensure you have secure access to your AWS resources. Back to our fundamentals. What level of service is AWS IAM? Zonal, regional or global? IAM is a global AWS service that secures any data in the AWS IAM database across all AWS Regions. Ensure you understand IAM. Review the best practices for account root user security, applying the principle of least privilege, and multifactor authentication. AWS accounts begin with a single account root user with full permissions. So why is this a risk to use the account root user? Well, these account permissions cannot be changed or modified and if this account root user is compromised then so is the whole AWS environment. One way to secure the account root user is to add multifactor authentication. But what else could we implement to secure our AWS account? How about adding another user with more limited permissions instead of using the account root user? When we add new users to our AWS account, those new users will be different people and need different levels of access. What level of permissions do new IAM users have in your AWS account? IAM identities start with no permissions but permissions can be granted. Dive deeper into IAM users, groups, and roles and what goes into deciding between which to use and how they might be combined. Understand how using the principle of least privilege limits your blast radius. Here are a few questions to consider. How do you create IAM users, groups, and roles? What are their strengths and limitations? And what scenarios would dictate possibly switching between the various user group and role-based permissions? Along with those identities, make sure you know how IAM and other AWS services give you the ability to secure the necessary credentials and best practices for handling those credentials. Look at various methods of assuming roles including assigning roles to AWS services. Understand how to use AWS Security Token Service within and across accounts, and how roles are used with federations. Your users might already have identities outside of AWS such as in your corporate directory. If those users need to work with AWS resources or work with applications that access those resources then those users will need security credentials. You can use an IAM role to specify permission for users whose identity is federated from your organization or third party identity provider. Do you know how to design and configure active directory to federation access to AWS IAM roles or users? Also dive into the best practices for controlling your application's access to AWS APIs. When should you hard code credentials into your application? The answer to this is never, but know the other ways to enable API access and dive deeper into IAM policies. For this certification, you do not need to know how to write advanced policies, but you should be able to read and interpret policy documents. Learn the major parts of a policy statement what's required, and what are the ways in which the policies provide granularity with permissions. For this exam, learn the major parts of a policy statement, what is required, and the ways in which the policies provide granularity with permissions. Make sure you also understand IAM decision logic when evaluating policies including how that will affect an identity with multiple policies attached. Also ensure you know the appropriate use of resource policies for AWS services. What is a policy? Well, it's an object in AWS that when associated with an identity or resource defines their permissions. The two types of policies are identity based policies which are attached to an IAM user group or role. These policies give you the ability to specify what that identity can do. Its permissions. With identity policies, we control what resources that identity can access. With resource policies, we control who can access the resource. So resource based policies are attached to a resource such as an Amazon S3 bucket, Amazon SQS queues, VPC endpoints, AWS Key Management Service encryption keys to specify who has access to the resource and what actions they can perform on it. And bucket policies are structured differently compared to the structure of identity policies. The difference is the principle part of the policy. The principle part of a resource policy defines which principles are affected by this resource policy. And lastly, understand the methods, services and tools available that help you create traceability for access to AWS resources. Traceability helps to monitor, alert and audit actions and changes to your environment in real time. It also helps to integrate log and metric collection with systems to automatically investigate and take action. Just as you need to be aware of the performance and behaviors of your application components. You also need to have insight into who and what has access to your account resources and data. You will be tested on designing security strategies for multiple AWS accounts, know how to gain that visibility, how to enforce security standards, and how to alert and automate based on that data. Ensure you understand AWS Control Tower, AWS Organizations and service control policies. Learning to design secure access to the resources is an important step in learning how to prioritize security at every step. Take your time when learning these topics and get practice with both the designing and implementation whenever possible. Let's get started with our first walkthrough question. I'll see you in the next video.

在 Day 4 中,我們介紹了 Azure 的五大管理工具(Portal、CLI、PowerShell、Cloud Shell、Mobile App),並理解了它們背後全部匯流至 Azure Resource Manager (ARM) 控制平面的核心原理。
但當你從基礎操作邁向架構設計時,必須思考一個根本性的問題:「當系統規模擴大到數十個微服務、數百個雲端資源時,我們該如何保證每次部署都安全、精確、可被審計且絕對一致?」
想像一個真實的雲端維運場景:
Titan 科技即將為跨國電商核心業務,在全球三個不同的 Azure 區域(East US、East Asia、North Europe)分別建立「開發 (Dev)」、「測試 (Test)」與「生產 (Prod)」環境。每個環境都包含 1 組虛擬網路 (VNet)、3 個子網路 (Subnet)、多組安全性規則 (NSG)、數台負載平衡虛擬機器 (VM) 以及加密儲存體帳戶。
如果這套架構由工程師在 Azure Portal 上手動點擊(Manual ClickOps)建立:
這正是雲端架構設計中最核心的基石——Infrastructure as Code (IaC,基礎架構即程式碼)!
在 AWS 生態系中,架構師會使用 AWS CloudFormation 或以 TypeScript/Python 編寫的 AWS CDK;而在 Azure 世界裡,微軟的原生 IaC 王牌是基於 JSON 的 ARM Templates,以及專為雲端打造的新一代領域專用語法 Azure Bicep!
今天 Day 5,我們將以媲美原廠架構培訓的深度,帶你徹底打通雙雲 IaC 的底層機制、語法解構、安全性最佳實踐,以及 AZ-900 考卷上所有關於自動化治理的必考題!
在早期,AWS 與 Azure 都是以宣告式範本起家:
但純手寫宣告式 JSON 存在巨大的工程痛點(被工程界稱為 JSON Hell):
{"Fn::Sub": "..."} vs Azure 的 "[concat('app-', parameters('env'), '-', uniqueString(resourceGroup().id))]")。為了解決這個問題,雙雲龍頭各自演化出了截然不同、卻殊途同歸的高階抽象武器:
┌───────────────────────────────────┬───────────────────────────────────┐
│ 🟠 AWS IaC 抽象演化架構 │ 🔵 Azure IaC 抽象演化架構 │
├───────────────────────────────────┼───────────────────────────────────┤
│ 【高階語言抽象層】 │ 【專屬 DSL 抽象層】 │
│ AWS CDK (TypeScript/Python/C#) │ Azure Bicep (.bicep 領域語法) │
│ • 物件導向 L1/L2/L3 Constructs │ • 簡潔宣告式語法、模組化重用 │
│ • 具備迴圈、條件式與邏輯判斷 │ • VS Code 智慧感知與型別檢查 │
├───────────────────────────────────┼───────────────────────────────────┤
│ │ │ │ │
│ ▼ cdk synth 合成 │ ▼ bicep build 轉譯│
│ │ (Synthesize) │ │ (Transpile) │
│ ▼ │ ▼ │
├───────────────────────────────────┼───────────────────────────────────┤
│ 【底層宣告式範本 Payload】 │ 【底層宣告式範本 Payload】 │
│ CloudFormation JSON / YAML │ ARM Template JSON │
├───────────────────────────────────┼───────────────────────────────────┤
│ │ │ │ │
│ ▼ HTTPS API 請求 │ ▼ HTTPS API 請求 │
├───────────────────────────────────┼───────────────────────────────────┤
│ 【雲端部署與管理引擎】 │ 【雲端部署與管理引擎】 │
│ AWS CloudFormation (Stacks) │ Azure Resource Manager (ARM) │
│ • 依賴分析與 Change Sets 變更集 │ • 依賴分析與冪等性 (Idempotency) │
│ • 建立與管理 AWS 實體資源 │ • 建立與管理 Azure 實體資源 │
└───────────────────────────────────┴───────────────────────────────────┘
CfnBucket)。s3.Bucket 自動啟用安全阻擋與方便的 .grantRead() 授權方法)。ApplicationLoadBalancedFargateService 一行指令自動建置 VPC、ALB、ECS Fargate 與 DNS)。| 比較維度 | AWS 生態系 | Azure 原生體系 | 第三方跨雲標準 (Terraform) | 核心概念與架構決策剖析 |
|---|---|---|---|---|
| 宣告式底層範本 | AWS CloudFormation (JSON / YAML) | ARM Template (JSON) | HashiCorp HCL (.tf) |
• 底層皆為宣告式(Declarative),告訴雲端「我要什麼最終狀態 (What)」,而非「如何逐步執行 (How)」。 |
| 高階程式化語法 | AWS CDK (TypeScript/Python 等) | Azure Bicep (.bicep) |
CDK for Terraform (CDKTF) | • AWS CDK 走物件導向多層封裝路線;Azure Bicep 走簡潔專屬 DSL 路線。 |
| 編譯/轉譯指令 | cdk synth(合成出 CloudFormation 範本) |
bicep build(轉譯出 ARM Template JSON) |
terraform plan(編譯並計算差異計畫) |
• 高階程式碼不會直接丟給雲端 API,皆在本地或 CI/CD 轉譯/合成成標準 JSON Payload。 |
| 部署管理指令 | cdk deploy / aws cloudformation deploy |
az deployment group create |
terraform apply |
• 透過 CLI 或 Pipeline 將範本提交給雲端控制平面。 |
| 狀態追蹤 (State) | CloudFormation Stacks(雲端託管,免維護 state 檔) | Azure Resource Manager(雲端即時狀態,免維護 state 檔) | Terraform State File(需自建 S3/Blob 儲存並處理 State Lock) | • AWS 與 Azure 原生 IaC 均為 No State File 架構,雲端平台的即時狀態就是單一真實來源 (Single Source of Truth)。 |
| 部署預覽 (Preview) | CloudFormation Change Sets | ARM Template What-If 操作 | terraform plan |
• 在實際套用前,先比對雲端現況並列出即將建立、修改或刪除的資源清單。 |
| 部署模式與影響 | Stack Update (支援安全替換) | Incremental (增量,預設)vs Complete (完整) | Apply with diff evaluation | • Incremental:只更新範本內定義的資源。• Complete:Resource Group 內若有未寫在範本內的資源,將會被全數自動刪除(極高危險)! |
| 核心特性保證 | 冪等性 (Idempotency) | 冪等性 (Idempotency) | 冪等性 (Idempotency) | • 重複執行同一個範本 100 次,只要內容沒變,產生的基礎架構狀態永遠一致,不會重複收費或建立多餘副本。 |
為了讓你具體感受兩者的差異,以下是部署一個標準 Azure 儲存體帳戶 (Storage Account) 的代碼對比:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storagePrefix": {
"type": "string",
"defaultValue": "titanstore"
}
},
"variables": {
"uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-01-01",
"name": "[variables('uniqueStorageName')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true
}
}
],
"outputs": {
"storageEndpoint": {
"type": "string",
"value": "[reference(variables('uniqueStorageName')).primaryEndpoints.blob]"
}
}
}
// 定義輸入參數,支援預設值與型別檢查
param storagePrefix string = 'titanstore'
param location string = resourceGroup().location
// 使用直覺的字串插值 (String Interpolation) 產生全域唯一名稱
var storageAccountName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
// 宣告儲存體帳戶資源
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
// 輸出建立後的 Blob 端點 URL
output blobEndpoint string = storageAccount.properties.primaryEndpoints.blob
💡 代碼精華解析(ARM/Bicep 的五大核心結構):
$schema/ API Version:指定範本遵循的 JSON Schema 規則與 Azure 資源提供者版本。parameters(參數區塊):範本的外部輸入口(如環境名稱dev/prod、虛擬機大小),實踐「一份範本、多環境部署」的關鍵。variables(內部變數區塊):在範本內部運算的輔助數值(如使用uniqueString自動計算全域唯一的 S3/Blob 名稱)。resources(資源區塊):欲在 Azure 建立的實體資源陣列(VM、VNet、Database)。outputs(輸出區塊):部署完成後回傳的動態資訊(如新 VM 的 Public IP 或 Storage Connection String),供後續自動化 Pipeline 使用。
在大型企業架構中,只會寫範本是不夠的,必須懂得如何控制爆炸半徑 (Blast Radius) 與保護敏感資料:
@secure() (Bicep) 或 "type": "securestring" (ARM),Azure 不會將其寫入部署日誌。更進階的做法是直接在範本中動態引用 Azure Key Vault(對應 AWS Secrets Manager / Parameter Store)的金鑰參照。az deployment group what-if 進行變更預測。系統會以顏色標示即將建立 (Create)、修改 (Modify)、忽略 (Ignore) 或刪除 (Delete) 的資源,避免災難性誤操作。Titan 科技即將推出核心支付閘道微服務。為了確保產品質量與交易安全,團隊規劃了三個標準環境:開發環境 (Dev)、整合測試環境 (Test) 與 線上正式環境 (Prod)。
每個環境都包含複雜的拓撲結構:1 組隔離的 Virtual Network、3 個子網段(Web / App / DB)、多組 Network Security Groups 防火牆規則、跨可用性區域 (AZ) 的 Virtual Machines 以及加密的 Storage Accounts。
在架構審查會議上,團隊成員提出了不同的部署提案:
維運主管:「上次產品上線,因為測試環境是用 Portal 手動點擊建立的,工程師漏開了一個安全性群組通訊埠,導致測試通過的程式碼一推到生產環境立刻全面崩潰!這次我們有三個環境,我要求一定要有防呆機制!」
資深工程師:「不如我寫一份 50 頁的 Word 操作手冊(Checklist),附上每一頁的截圖,讓三位工程師對著螢幕一步一步點?」
Linux 腳本專家:「我可以用 Bash 寫一個 500 行的命令式腳本,呼叫az vm create逐台建立,只是如果中間網路斷線,重跑會報錯,需要人工進 Portal 刪除半成品!」
CTO 轉頭看向你:「架構師,我們在 AWS 習慣用 AWS CDK 與 CloudFormation 確保環境一致性。在 Azure 這種跨環境部署到底該如何做到 100% 規格一致、消除人為失誤、具備冪等性,並且將所有基礎設施納入 GitHub 進行版本控制與審查?」
身為 Titan 科技的 Chief Cloud Architect,你該如何制定標準基礎架構部署策略?
az vm create 與 az network vnet create,並要求工程師在腳本執行失敗時手動清理所有已建立的半成品資源。dev.parameters.json 與 prod.parameters.json),可在開發環境使用平價的 VM 規格(如 Standard_B2s),在生產環境自動切換至高規格實例(如 Standard_D4s_v5),範本主體完全無需修改。以下精選 3 題 關於 ARM Templates、Bicep 與 IaC 的高頻核心真題進行深度拆解:
【Question】 (改寫自 AZ-900 官方題庫高頻真題)
Titan 科技計劃利用 Azure 原生自動化工具來部署微服務基礎架構。關於 Azure Resource Manager (ARM) Templates,下列哪兩項敘述是正確的?(選兩項)
B、D
resources 陣列中,你可以任意組合宣告各種服務(VNet、Subnet、NSG、VM、Database、Storage 等),並透過 dependsOn 指定資源建立的相依順序。【Question】 (改寫自 AZ-900 官方題庫真題 Question 22 / Question 17)
Titan 科技每週需要為不同的開發專案快速部署 20 台具有相同規格、相同安全性設定與網路拓撲的虛擬機器。
身為架構師,你應該建議團隊使用下列哪一項解決方案,以達到最高效率、消除人為錯誤並確保每週部署的重複一致性?
C
【Question】 (改寫自 AZ-900 官方題庫精選)
針對以下關於 Infrastructure as Code (IaC) 與 Azure 範本技術 的陳述,請判斷敘述為 Yes (正確) 還是 No (錯誤):
| 題目陳述 (Statements) | Yes / No |
|---|---|
| 1. ARM Templates 與 Bicep 採用宣告式語法 (Declarative syntax),使用者只需定義期望的最終資源狀態,而不需要編寫具體的逐步建立指令。 | [ ? ] |
| 2. 由於 ARM Templates 具備冪等性 (Idempotency),重複執行同一份未修改的範本會導致 Azure 建立出第二套重複名稱的虛擬機器並產生雙倍費用。 | [ ? ] |
| 3. Azure Bicep 檔案在部署到 Azure Resource Manager 之前,會被編譯/轉譯為標準的 ARM Template JSON 格式。 | [ ? ] |
bicep build 或透過 CLI 部署時,底層機制就是將 .bicep 轉譯為 ARM JSON 傳送至 ARM API。| 項目 | 內容 |
|---|---|
| 對應課程章節 | 第 3 章 Azure 管理與治理 ▸ Infrastructure as Code(p118) |
| 官方考綱領域 | Describe Azure Management & Governance(占比 30–35%) |
| 課程涵蓋範圍 | 介紹 Infrastructure as Code (IaC) 的核心概念、使用 ARM Templates 進行宣告式部署的好處,以及範本檔案採用 JSON 格式的基礎認知。 |
| 本文補充範圍 | 深入補充微軟最新一代 DSL 語言 Azure Bicep 的轉譯與運作機制、AWS CDK (Constructs L1/L2/L3) ➔ CloudFormation vs Azure Bicep ➔ ARM Template 雙雲抽象演化架構圖、程式碼結構五大區塊拆解、宣告式 (Declarative) vs 命令式 (Imperative) 語法差異、冪等性 (Idempotency)、ARM 部署模式(增量 Incremental vs 完整 Complete 與爆炸半徑控制)、機密安全防護 (Key Vault / SecureString) 以及 What-If 變更預覽。 |
cdk synth) ➔ CloudFormation,對應 Azure Bicep (bicep build) ➔ ARM Template (JSON);兩者皆將高階語法轉譯/合成至底層宣告式範本供雲端引擎執行。securestring 或 Key Vault 保護機密,並謹慎評估 Incremental 與 Complete 部署模式的爆炸半徑。有了自動化部署的利器,雲端環境已經順利運轉。但身為首席雲端架構師,我們該如何隨時掌握雲端平台的健康狀態?又該如何主動獲得微軟官方針對成本、安全性、可靠性與效能的最佳化改善建議?
明天 Day 6,我們將深入剖析 Azure 兩大維運監控與架構優化神器——【Azure Advisor & Service Health】!