Azure Resource Graph Nedir ?
Azure Resource Graph Azure kaynaklarınızı komut satırı aracılığıyla hızlı ve kolay bir şekilde analiz edip keşfetmenize olanak sağlayan bir servistir.
Peki Neden Azure Azure Resource Graph ?
Azure dünyasında bulunan her kaynak grubu (VM, Storage, AppService, Azure SQL…) farklı Resource Provider aracılığıyla yönetilir. Azure Resource Manager sahip olduğumuz kaynaklara ait bazı bilgileri (Resource name, ID, Type, Resource Group, Subscriptions, and Location) kaynaklara ait bu Resource Provider’lara gönderir, bizlerde ihtiyaç duyduğumuz bu bilgileri çeşitli script’ler ile Resource Provider’lerdan okuyabiliriz. Ancak bu durum ihtiyaç duyduğumuz her bilgi için ekstra bir sorgu ve çoklu Azure aboneliklerinin olduğu ortamlarda karmaşık script’ler demektir.
Azure Resource Graph’da ise; değişiklik ve güncelleme bilgisine sahip her Azure kaynağı bu bilgiyi Resource Graph Resource Manager’a bildirir. Resource Graph ayrıca bu kaynaklar üzerinde düzenli taramalar yaparak değişiklik yada güncelleme bilgisi eksik olan kaynakları kontrol ederek database’ine yazar. Bizlerde gerekli olan bu bilgileri Azure Resource Graph’dan alabiliriz.
Azure Resource Graph, Azure Data Explorer, Application Insights ve Azure Log Analytics’de olduğu gibi Kusto Query dilini kullanıır, ve tüm Azure üyeliklerine (Subscriptions) ve Yönetim Gruplarına (Management Groups) ait ayrıntınları tek bir yerden alabilmemize olanak sağlar.
Aslında Azure Resource Graph portal üzerinde (Azure Portal -> All Resource) her arama yaptığınızda arka planda zaten Azure tarafından kullanılan bir servistir.
Azure Resource Graph Nasıl Kullanılır ?
Azure Resource Graph ile sorgu yapabilmek için ilgili kaynaklar üzerinde (RBAC) Read yetkisine sahip olmanız gereklidir aksi taktirde yaptığınız sorguya cevap alamazsınız. Resource Graph sorguları için Azure CLI (Cloud Shell) yada PowerShell kullanabilirsiniz.
Azure Resource Graph kullanabilmek için öncelikle AzureRmGraph modülunu aktif ediyoruz:
1 2 3 4 5 |
#PowerShell Install-Module -Name AzureRm.ResourceGraph -AllowPrerelease #AzureCLI az extension add --name resource-graph |
Şimdi Resource Graph ile birkaç basit örnek yapalım:
- Azure üzerinde kullanılan tüm kaynakların listelenmesi:
1 2 3 4 5 |
#PowerShell Search-AzureRmGraph -Query "summarize count() by type| project resource=type , total=count_ | order by total desc" | Format-Table #AzureCLI az graph query -q "summarize count() by type| project resource=type , total=count_ | order by total desc" --output table |
- Komut çıktısı:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Resource Total -------------------------------------------- ------- microsoft.network/publicipaddresses 6 microsoft.network/networksecuritygroups 6 microsoft.storage/storageaccounts 5 microsoft.network/networkinterfaces 5 microsoft.compute/virtualmachines 4 microsoft.compute/disks 4 microsoft.network/virtualnetworks 4 microsoft.network/networkwatchers 3 microsoft.insights/components 2 microsoft.cdn/profiles 2 microsoft.cdn/profiles/endpoints 2 microsoft.insights/alertrules 2 microsoft.sql/servers/databases 1 microsoft.containerregistry/registries 1 microsoft.recoveryservices/vaults 1 microsoft.web/certificates 1 microsoft.operationalinsights/workspaces 1 microsoft.compute/virtualmachines/extensions 1 microsoft.cognitiveservices/accounts 1 microsoft.insights/actiongroups 1 microsoft.sql/servers 1 |
- Azure üzerinde kullanılan kaynakların Azure lokasyonuna göre listelenmesi
1 2 3 4 5 |
#PowerShell Search-AzureRmGraph -Query "summarize count() by location | project total=count_, location | order by total desc " | Format-Table #AzureCLI az graph query -q "summarize count() by location | project total=count_, location | order by total desc " --output table |
- Komut çıktısı:
1 2 3 4 5 6 7 |
Location Total ----------- ------- westeurope 36 northeurope 12 westus 4 global 2 eastasia 1 |
- Azure VM’lerin SKU’larına göre listelenmesi:
1 2 3 4 5 |
#PowerShell Search-AzureRmGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | project SKU = tostring(properties.hardwareProfile.vmSize)| summarize count() by SKU" | Format-Table #AzureCLI az graph query -q "where type =~ 'Microsoft.Compute/virtualMachines' | project SKU = tostring(properties.hardwareProfile.vmSize)| summarize count() by SKU" --output table |
- Komut çıktısı:
1 2 3 4 5 6 |
SKU Count_ --------------- -------- Standard_D2 1 Standard_B2ms 1 Standard_D2s_v3 1 Standard_D4s_v3 1 |
- Azure üzerinde kullanılan Public IP’lerin listelenmesi:
1 2 3 4 5 |
#PowerShell Search-AzureRmGraph -Query "where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | project properties.ipAddress | limit 100" --output table #AzureCLI az graph query -q "where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | project properties.ipAddress | limit 100" --output table |
- Komut çıktısı:
1 2 3 4 5 |
Properties_ipAddress ---------------------- 13.93.74.171 104.41.221.171 13.69.50.83 |
Not: Resource Graph sorgulamarı varsayılan olarak sahip olduğunuz tüm Azure üyelikleri (subscription) için yapılır. Eğer sorgu dışında bırakmak istediğiniz bir Azure üyeliğiniz varsa sorguya subscription paramatresini eklemelisiniz.
az graph query -q “where type contains ‘publicIPAddresses’ and isnotempty(properties.ipAddress) | project properties.ipAddress | limit 100” –output table –-subscriptions “97037a45-XXXX-XXXXX”
Desteklenen tüm parametre ve örnek sorgulara aşağıdaki link’lerden erişebilirsiniz..
- https://docs.microsoft.com/en-gb/azure/governance/resource-graph/concepts/query-language
- https://docs.microsoft.com/en-us/azure/governance/resource-graph/samples/starter