{Azure Policy}チュートリアル:コンプライアンスを強制するポリシーの作成と管理

 

https://learn.microsoft.com/ja-jp/azure/governance/policy/tutorials/create-and-manage
https://learn.microsoft.com/ja-jp/azure/governance/policy/assign-policy-azurecli

https://zenn.dev/tomot/articles/e6b07bf9e76cce

Azure Policy のスコープは、Azure Resource Manager でのスコープの動作に基づいています。
管理グループ、サブスクリプション、リソース グループ、およびリソースという 4 つのレベルのスコープが用意されています。

 

「Standard_B1lsかStandard_B1sのVM SKUを必須にする」ポリシーを作成し
リソースグループに割り当てる

-- 1. 前作業

az login --use-device-code
az account show

az version

az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults

az group create \
--name rg9999999 \
--location japaneast


az group list
az upgrade


-- 2. ポリシー定義の作成

az policy definition create \
--name policy01 \
--description 'policy01' \
--display-name 'policy01' \
--rules '{
    "if": {
        "allOf": [{
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
             },
             {
                 "field": "Microsoft.Compute/virtualMachines/sku.name",
                 "notIn": ["Standard_B1ls","Standard_B1s"]
             }
        ]
    },
    "then": {
        "effect": "deny"
    }
}'


az policy definition list --query "[?name=='policy01']"

 

-- 3. 割り当ての設定

az policy assignment create \
--name assign01 \
--display-name 'assign01' \
--scope '/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg9999999' \
--policy 'policy01'

 

az policy assignment list \
--resource-group rg9999999

 


-- 4. 動作確認

az vm create \
--resource-group rg9999999 \
--name vm01 \
--image UbuntuLTS \
--size Standard_B1ls \
--admin-username azureuser \
--generate-ssh-keys \
--storage-sku Standard_LRS

az vm create \
--resource-group rg9999999 \
--name vm02 \
--image Win2019Datacenter \
--size Standard_B1s \
--admin-username azureuser \
--admin-password 'passwordpassword'


az vm create \
--resource-group rg9999999 \
--name vm03 \
--image UbuntuLTS \
--size Standard_DS1_v2 \
--admin-username azureuser \
--generate-ssh-keys \
--storage-sku Standard_LRS

 


-- 5. クリーンアップ

az policy assignment delete \
--name 'assign01' \
--scope '/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg9999999'

az policy assignment list \
--resource-group rg9999999

 

az policy definition delete \
--name policy01

az policy definition list --query "[?name=='policy01']"

 

az group list
az group delete \
--name rg9999999 \
--yes

az group delete \
--name NetworkWatcherRG \
--yes