standard
turbot/azure_thrifty

Detect & correct Compute disks attached to stopped virtual machine

Overview

Compute disk can be attached to stopped virtual machine which can cost money. Detaching compute disks from stopped virtual machines can significantly reduce storage costs by eliminating charges for unused disk storage.

This query trigger detects compute disks attached to stopped virtual machine and then either sends a notification or attempts to perform a predefined corrective action.

Getting Started

By default, this trigger is disabled, however it can be configured by setting the below variables

  • compute_disks_attached_to_stopped_virtual_machine_trigger_enabled should be set to true as the default is false.
  • compute_disks_attached_to_stopped_virtual_machine_trigger_schedule should be set to your desired running schedule
  • compute_disks_attached_to_stopped_virtual_machines_enabled_actions should be set to your desired action (i.e. "notify" for notifications or "snapshot_and_delete_disk" to snapshot and delete the disk).

Then starting the server:

flowpipe server

or if you've set the variables in a .fpvars file:

flowpipe server --var-file=/path/to/your.fpvars

Query

with attached_disk_with_vm as (
select
concat(
vm.id,
' [',
vm.resource_group,
'/',
vm.subscription_id,
']'
) as title,
vm.name,
vm.power_state as virtual_machine_state,
vm.os_disk_name,
jsonb_agg(data_disk ->> 'name') as data_disk_names
from
azure_compute_virtual_machine as vm
left join jsonb_array_elements(vm.data_disks) as data_disk on true
group by
vm.id,
vm.resource_group,
vm.subscription_id,
vm.name,
vm.os_disk_name,
vm.power_state
)
select
m.title,
d.id as resource,
d.name as disk_name,
m.name as vm_name,
d.resource_group,
d.subscription_id,
d.name || to_char(current_date, 'YYYYMMDD') as snapshot_name,
d.sp_connection_name as conn
from
azure_compute_disk as d
left join attached_disk_with_vm as m on (
d.name = m.os_disk_name
or m.data_disk_names ?| array [ d.name ]
)
left join azure_subscription as sub on sub.subscription_id = d.subscription_id
where
d.disk_state != 'Unattached'
or m.virtual_machine_state != 'running';

Schedule

15m

Tags

category = Cost
class = unused
plugin = azure
service = Azure/Compute