MODULE 06 Tools · Core ~3 giờ Administrator

Cấu hình Azure Resources bằng Công Cụ

Nắm vững 4 công cụ quản lý Azure cốt lõi: Azure Portal (GUI), Cloud Shell (browser terminal), Azure PowerShell (scripting Windows), và Azure CLI (cross-platform). Chọn đúng công cụ cho từng tình huống.

Lý Thuyết Cốt Lõi

1. Azure Portal — Giao Diện Đồ Hoạ

Azure Portal (portal.azure.com) là giao diện web đồ hoạ chính thức của Azure. Phù hợp cho công việc khám phá, cấu hình lần đầu, và giám sát trực quan. Không phù hợp cho tự động hoá hàng loạt.

Tính năng nổi bật
  • Dashboard: tùy chỉnh widget, pin resource quan trọng
  • Resource Graph: tìm kiếm resource theo tag, type, location
  • Cost Analysis: biểu đồ chi phí tương tác
  • Activity Log: xem mọi thao tác trong 90 ngày
  • Cloud Shell: terminal tích hợp ngay trong portal
Khi nào dùng Portal?
  • • Lần đầu cấu hình service mới, chưa quen syntax CLI
  • • Cần xem biểu đồ metrics, topology mạng trực quan
  • • Quản lý quyền IAM (drag-and-drop friendly)
  • • Diagnose vấn đề — xem error message chi tiết
  • • Demo cho stakeholder không kỹ thuật
Portal tip: Dùng G + / để mở thanh tìm kiếm toàn cục. Gõ tên resource/service và Enter — nhanh hơn điều hướng menu. Pin resource thường dùng vào Dashboard để tiết kiệm thời gian.

2. Azure Cloud Shell — Terminal Trên Trình Duyệt

Cloud Shell là môi trường terminal được quản lý bởi Azure, chạy ngay trên trình duyệt tại shell.azure.com hoặc từ icon trong Portal. Không cần cài đặt gì. Đã được xác thực sẵn với subscription của bạn.

Bash mode
  • • Azure CLI (az) sẵn sàng
  • • Bash scripting đầy đủ
  • • kubectl, helm, terraform, git
  • • Python, Node.js runtime
PowerShell mode
  • • Az PowerShell module sẵn sàng
  • • PowerShell 7 (cross-platform)
  • • Azure CLI cũng dùng được
  • • .NET SDK, dotnet CLI
Lưu trữ
  • • Yêu cầu Azure Storage Account (5GB miễn phí đầu)
  • • File lưu trong clouddrive/ — persistent qua sessions
  • • Session timeout sau 20 phút không dùng
  • • Tự động authenticated với subscription hiện tại

3. Azure PowerShell — Scripting cho Windows Admin

Azure PowerShell là module Az cho PowerShell. Phù hợp với Windows administrator quen PowerShell scripting. Chạy được trên Windows, macOS, Linux (PowerShell 7+).

Cài đặt và xác thực
# Cài Az module (chạy với quyền Admin)
Install-Module -Name Az -Scope CurrentUser -Force

# Đăng nhập (mở browser)
Connect-AzAccount

# Chọn subscription
Set-AzContext -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

# Xem subscription hiện tại
Get-AzContext
Cú pháp Verb-Noun
  • Get-Az*: truy vấn/đọc resource
  • New-Az*: tạo resource mới
  • Set-Az*: cập nhật resource
  • Remove-Az*: xóa resource
# Ví dụ: tạo RG
New-AzResourceGroup `
  -Name "rg-az104-m06" `
  -Location "southeastasia"
Điểm mạnh của Azure PowerShell
• Pipeline: Get-AzVM | Where-Object {$_.Location -eq 'southeastasia'}
• Object-oriented: kết quả trả về .NET object, dễ xử lý với OOP
• Tích hợp tốt với Windows task scheduler, Group Policy, Active Directory

4. Azure CLI — Cross-Platform Command Line

Azure CLI (az) là công cụ dòng lệnh cross-platform viết bằng Python. Cú pháp ngắn gọn, thân thiện với DevOps/Linux admin. Chạy trên Windows (cmd/PowerShell), macOS, Linux, Cloud Shell.

Cài đặt và xác thực
# Windows: download installer từ Microsoft
# macOS:
brew install azure-cli
# Ubuntu/Debian:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

# Đăng nhập (mở browser)
az login

# Đặt subscription mặc định
az account set --subscription "sub-name-or-id"

# Xem subscription hiện tại
az account show
Cấu trúc lệnh az
# az [group] [subgroup] [command] [args]
# Ví dụ:
az group create --name rg-m06 --location southeastasia
az vm create --resource-group rg-m06 --name vm01 ...
az storage account list -o table
az network vnet show --name vnet01 -g rg-m06

# Output format: json (default), table, tsv, yaml, jsonc
az group list -o table
az vm list --query "[].{Name:name, RG:resourceGroup}" -o table
Tiêu chí Azure PowerShell Azure CLI
Nền tảng Windows tốt nhất, cũng dùng được macOS/Linux Cross-platform như nhau (Win/Mac/Linux)
Cú pháp New-AzResourceGroup (Verb-Noun) az group create (group subgroup command)
Output .NET object — pipe sang cmdlet khác JSON/table/tsv — dễ dùng với jq, grep, awk
Scripting .ps1 file, tốt cho Windows environment Bash .sh file, tốt cho CI/CD, Docker, Linux
Thi AZ-104 Cần nhớ cmdlet name và parameter syntax Cần nhớ group/subgroup/command pattern

Bài Tập Thực Hành (Lab)

Lab 06-A: Azure Portal Navigation Lab 06-B: Cloud Shell Setup Lab 06-C: Azure CLI Scripting Lab 06-D: PowerShell Az Module
1

Kích hoạt Cloud Shell và tạo Resource Group bằng CLI

Mở portal.azure.com → click icon Cloud Shell (góc trên phải, icon >_) → chọn Bash → tạo Storage Account khi được yêu cầu lần đầu.

Azure CLI— Chạy trong Cloud Shell (Bash mode)
# Xác minh đã đăng nhập và xem subscription
az account show --output table

# Xem danh sách tất cả subscriptions có quyền truy cập
az account list --output table

# Tạo Resource Group cho module 06
az group create \
  --name rg-az104-m06 \
  --location southeastasia \
  --tags Course=AZ-104 Module=06 Environment=Lab

# Xem danh sách Resource Groups
az group list --output table

# Xem chi tiết RG vừa tạo
az group show --name rg-az104-m06

# Lấy Location của RG bằng JMESPath query
az group show \
  --name rg-az104-m06 \
  --query "{Name:name, Location:location, State:properties.provisioningState}" \
  --output json
Verify Portal: Azure Portal → Resource Groups → rg-az104-m06 phải xuất hiện với Location "Southeast Asia". Tags phải hiển thị Course=AZ-104, Module=06.
2

Tạo Storage Account bằng Azure CLI với JMESPath query

Azure CLI— Chạy trong Cloud Shell hoặc local terminal
# Tạo tên storage unique (tối đa 24 ký tự, chữ thường + số)
STORAGE_NAME="stm06$(date +%s | tail -c 8)"
echo "Storage name: $STORAGE_NAME"

# Tạo Storage Account
az storage account create \
  --name $STORAGE_NAME \
  --resource-group rg-az104-m06 \
  --location southeastasia \
  --sku Standard_LRS \
  --kind StorageV2 \
  --access-tier Hot \
  --https-only true \
  --min-tls-version TLS1_2 \
  --tags Course=AZ-104 Module=06

# Xem storage accounts trong RG
az storage account list \
  --resource-group rg-az104-m06 \
  --output table

# Query chỉ lấy name và primaryLocation
az storage account list \
  --resource-group rg-az104-m06 \
  --query "[].{Name:name, Location:primaryLocation, SKU:sku.name}" \
  --output table

# Lấy connection string
az storage account show-connection-string \
  --name $STORAGE_NAME \
  --resource-group rg-az104-m06 \
  --output tsv
Verify Portal: Portal → Storage accounts → tìm storage vừa tạo. Click vào → Configuration → xác nhận HTTPS only: Enabled, Minimum TLS version: TLS 1.2.
3

Thực hiện tương tự bằng Azure PowerShell

Đổi Cloud Shell sang PowerShell mode (dropdown góc trái Cloud Shell) rồi chạy lệnh sau. Hoặc cài Az module trên Windows local.

Azure PowerShell— Cloud Shell PowerShell mode hoặc local PowerShell 7+
# Xem context hiện tại
Get-AzContext

# Xem danh sách subscriptions
Get-AzSubscription | Format-Table Name, Id, State

# Tạo thêm storage account bằng PowerShell (so sánh cú pháp)
$storageName = "stm06ps$(Get-Date -Format 'MMddHHmm')"
Write-Host "Creating storage: $storageName"

$storageParams = @{
    Name              = $storageName
    ResourceGroupName = "rg-az104-m06"
    Location          = "southeastasia"
    SkuName           = "Standard_LRS"
    Kind              = "StorageV2"
    AccessTier        = "Hot"
    EnableHttpsTrafficOnly = $true
    MinimumTlsVersion = "TLS1_2"
    Tag               = @{Course="AZ-104"; Module="06"; Tool="PowerShell"}
}

$storage = New-AzStorageAccount @storageParams
Write-Host "Created: $($storage.StorageAccountName) in $($storage.PrimaryLocation)"

# Xem tất cả storage trong RG
Get-AzStorageAccount -ResourceGroupName "rg-az104-m06" |
    Select-Object StorageAccountName, PrimaryLocation, Kind, @{N="SKU";E={$_.Sku.Name}} |
    Format-Table -AutoSize
Verify Portal: Portal → Storage accounts → phải thấy 2 accounts: một tạo bằng CLI (stm06...), một tạo bằng PowerShell (stm06ps...). Cả hai đều trong rg-az104-m06, region Southeast Asia.
4

Viết script CLI tự động hóa — Deploy nhiều storage cùng lúc

Bash— Cloud Shell Bash mode
#!/usr/bin/env bash
# Script tạo storage cho 3 môi trường: dev, staging, prod

RG="rg-az104-m06"
LOCATION="southeastasia"
SUFFIX=$(date +%s | tail -c 5)
ENVIRONMENTS=("dev" "staging" "prod")

echo "=== Deploying storage accounts to $RG ==="

for ENV in "${ENVIRONMENTS[@]}"; do
    STORAGE_NAME="stm06${ENV}${SUFFIX}"
    echo "Creating storage for $ENV: $STORAGE_NAME"

    az storage account create \
        --name "$STORAGE_NAME" \
        --resource-group "$RG" \
        --location "$LOCATION" \
        --sku Standard_LRS \
        --kind StorageV2 \
        --https-only true \
        --min-tls-version TLS1_2 \
        --tags Environment="$ENV" Course=AZ-104 Module=06 \
        --output none

    if [ $? -eq 0 ]; then
        echo "  [OK] $STORAGE_NAME created"
    else
        echo "  [FAIL] Failed to create $STORAGE_NAME"
    fi
done

echo ""
echo "=== Final state ==="
az storage account list \
    --resource-group "$RG" \
    --query "[].{Name:name, Env:tags.Environment, SKU:sku.name}" \
    --output table
Verify Portal: Portal → rg-az104-m06 → Overview → Resources tab → phải thấy 5 storage accounts (2 từ bước trước + 3 từ script). Filter by tag "Environment" để phân biệt dev/staging/prod.
5

Cleanup — Dọn dẹp tài nguyên

Azure CLI— Chạy được trên PowerShell, CMD, Bash hoặc Azure Cloud Shell
# Xóa Resource Group và toàn bộ resource bên trong
# --yes: không hỏi xác nhận | --no-wait: không chờ hoàn thành
az group delete --name rg-az104-m06 --yes --no-wait

echo "Deletion of rg-az104-m06 initiated (running in background)"

# Verify sau vài phút
az group show --name rg-az104-m06 2>/dev/null \
  && echo "Still deleting..." \
  || echo "Resource group deleted successfully"

Kết Quả Đầu Ra

Thành thạo Azure Portal

Điều hướng nhanh bằng search, tùy chỉnh Dashboard, đọc Activity Log, xem Cost Analysis trực quan

Sử dụng Cloud Shell

Kích hoạt Cloud Shell, chuyển đổi Bash/PowerShell mode, lưu file trong clouddrive persistent storage

Cú pháp Azure CLI thành thạo

Nắm cấu trúc az [group] [subgroup] [command], dùng --query JMESPath, --output table/json/tsv

Azure PowerShell cú pháp Verb-Noun

Cài Az module, Connect-AzAccount, New/Get/Set/Remove-Az* cmdlets, pipeline object processing

Viết script tự động hoá

Bash loop script deploy nhiều resource, xử lý lỗi với exit code, log kết quả rõ ràng

Chọn đúng công cụ theo tình huống

Biết khi nào dùng Portal (khám phá), CLI (CI/CD, Linux), PowerShell (Windows automation), Cloud Shell (quick fix)

Ứng Dụng Thực Tế

Tình huống 1: Startup công nghệ — CI/CD pipeline deploy hạ tầng Azure

Team 10 developer dùng GitHub Actions. Mỗi lần merge vào main, pipeline tự động provision hạ tầng Azure (Storage, App Service) cho môi trường staging rồi production.

Giải pháp

Azure CLI trong GitHub Actions workflow: uses: azure/login@v2 với Service Principal credentials từ GitHub Secrets. Script az group create, az storage account create chạy idempotent.

Triển khai

AZURE_CREDENTIALS secret lưu SP credentials. Pipeline: az login → az group create (--no-wait nếu đã tồn tại) → az storage create → az webapp deploy. Output json → parse lấy endpoint URL → comment vào PR.

Lợi ích

Infrastructure as Code ngay trong pipeline. Staging environment sẵn trong 3 phút sau merge. Không cần admin can thiệp thủ công. Xóa staging sau test để tiết kiệm chi phí.

Tình huống 2: Tập đoàn VN — Onboard 50 VM mới cho dự án ERP

Dự án ERP cần 50 VM Windows Server với cấu hình chuẩn (size, disk, tag, join domain). Làm thủ công qua Portal tốn 2-3 ngày và dễ sai.

Giải pháp

PowerShell script đọc CSV (vmname, role, size, subnet) → loop tạo 50 VM bằng New-AzVM với splatting parameter. Mỗi VM auto-tag Department, Project, Owner từ CSV.

Triển khai

Cloud Shell PowerShell mode — upload CSV vào clouddrive. Script chạy parallel với Start-Job để tạo 10 VM cùng lúc. Log output ra file. Sau khi tạo xong, script 2 gắn data disk và join AD domain.

Lợi ích

50 VM chuẩn hóa trong 45 phút (so với 3 ngày thủ công). Zero lỗi cấu hình do human error. Script tái dùng cho dự án tiếp theo. Audit trail đầy đủ trong Activity Log.

Tình huống 3: Công ty dịch vụ CNTT — Admin fix incident lúc 2 giờ sáng

Hệ thống cảnh báo lúc 2h sáng: Storage Account bị misconfiguration allow public access. Admin cần fix ngay nhưng chỉ có điện thoại và laptop không có VPN.

Giải pháp

Mở shell.azure.com trên trình duyệt điện thoại. Dùng Cloud Shell (không cần cài gì, đã auth). Chạy: az storage account update --allow-blob-public-access false. Fix xong trong 2 phút.

Triển khai

Cloud Shell lưu lịch sử command trong clouddrive — admin có thể xem lại lịch sử. Sau fix, chạy az storage account show --query allowBlobPublicAccess để verify. Ghi incident report trong Portal Activity Log export.

Lợi ích

MTTR (Mean Time To Repair) giảm từ 30 phút xuống 5 phút. Không cần VPN, không cần máy tính riêng. Cloud Shell là "backup terminal" luôn sẵn sàng từ mọi thiết bị. Incident được giải quyết trước khi khách hàng nhận ra.

Zalo