Tạo Budget Alert trên AWS & Azure
🎯 Mục tiêu: Tạo budget $50/tháng trên AWS với alert 80% và 100%; tạo budget tương đương trên Azure, nhận email khi vượt ngưỡng.
🧰 Công cụ / nền tảng: AWS CLI v2, Azure CLI 2.x, VS Code, Git.
📦 Chuẩn bị: AWS CLI configured (aws configure); Azure CLI logged in (az login); có địa chỉ email nhận alert.
▶️ Phần A — AWS Budget (CLI):
# Tạo file budget definition
cat > budget.json <<'EOF'
{
"BudgetName": "monthly-devops-budget",
"BudgetLimit": { "Amount": "50", "Unit": "USD" },
"TimeUnit": "MONTHLY",
"BudgetType": "COST"
}
EOF
# Tạo file notifications (80% và 100%)
cat > notifications.json <<'EOF'
[
{
"Notification": {
"NotificationType": "ACTUAL",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 80,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
},
{
"Notification": {
"NotificationType": "ACTUAL",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 100,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
}
]
EOF
# Lấy Account ID
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
echo "Account ID: $ACCOUNT_ID"
# Tạo budget
aws budgets create-budget \
--account-id "$ACCOUNT_ID" \
--budget file://budget.json \
--notifications-with-subscribers file://notifications.json
# Xác nhận
aws budgets describe-budgets --account-id "$ACCOUNT_ID" \
--query 'Budgets[?BudgetName==`monthly-devops-budget`].[BudgetName,BudgetLimit.Amount]' \
--output table
▶️ Phần B — Azure Budget (CLI):
# Lấy subscription ID
SUB_ID=$(az account show --query id -o tsv)
echo "Subscription: $SUB_ID"
# Tạo budget $50/tháng với alert 80% và 100%
az consumption budget create \
--budget-name "devops-monthly-budget" \
--amount 50 \
--time-grain Monthly \
--start-date "2026-06-01" \
--end-date "2027-05-31" \
--resource-group "rg-devops" \
--category Cost
# Thêm alert notification (Azure Portal hoặc REST API)
# Qua Portal: Cost Management → Budgets → chọn budget → Add alert
# Kiểm tra budget đã tạo
az consumption budget list --query '[].{Name:name, Amount:amount, CurrentSpend:currentSpend.amount}' -o table
🖥️ Đối chiếu GUI:
AWS Console: Billing → Budgets → Create budget. Azure Portal: Cost Management + Billing → Budgets → Add.
✅ Kết quả mong đợi: aws budgets describe-budgets trả về budget monthly-devops-budget với Amount=50 USD; Azure CLI liệt kê budget devops-monthly-budget. Email nhận thông báo test khi trigger threshold.
🧹 Cleanup:
# AWS
aws budgets delete-budget --account-id "$ACCOUNT_ID" --budget-name "monthly-devops-budget"
# Azure
az consumption budget delete --budget-name "devops-monthly-budget"