|
| 1 | +locals { |
| 2 | + k = 1000 |
| 3 | + m = 1000 * local.k |
| 4 | + mb = 1024 * 1024 |
| 5 | + gb = 1024 * 1024 * 1024 |
| 6 | + |
| 7 | + r2_storage_free_tier = 10 * local.gb # 10 GB |
| 8 | + r2_class_a_operations_free_tier = 1 * local.m # 1 million requests |
| 9 | + r2_class_b_operations_free_tier = 10 * local.m # 10 million requests |
| 10 | + |
| 11 | + thresholds = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95] |
| 12 | + |
| 13 | + notification_configs = merge( |
| 14 | + { |
| 15 | + for threshold in local.thresholds : |
| 16 | + "r2_storage_${threshold}" => { |
| 17 | + name = "R2 Storage - ${threshold}% of Free Tier" |
| 18 | + product = "r2_storage" |
| 19 | + limit = local.r2_storage_free_tier * (threshold / 100) |
| 20 | + description = "Alert when R2 storage usage reaches ${threshold}% of free tier (${threshold / 10} GB)" |
| 21 | + } |
| 22 | + }, |
| 23 | + { |
| 24 | + for threshold in local.thresholds : |
| 25 | + "r2_class_a_${threshold}" => { |
| 26 | + name = "R2 Class A Operations - ${threshold}% of Free Tier" |
| 27 | + product = "r2_class_a_operations" |
| 28 | + limit = local.r2_class_a_operations_free_tier * (threshold / 100) |
| 29 | + description = "Alert when R2 Class A operations reach ${threshold}% of free tier (${threshold * 10000} requests)" |
| 30 | + } |
| 31 | + }, |
| 32 | + { |
| 33 | + for threshold in local.thresholds : |
| 34 | + "r2_class_b_${threshold}" => { |
| 35 | + name = "R2 Class B Operations - ${threshold}% of Free Tier" |
| 36 | + product = "r2_class_b_operations" |
| 37 | + limit = local.r2_class_b_operations_free_tier * (threshold / 100) |
| 38 | + description = "Alert when R2 Class B operations reach ${threshold}% of free tier (${threshold * 100000} requests)" |
| 39 | + } |
| 40 | + } |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +resource "cloudflare_notification_policy" "r2_notifications" { |
| 45 | + for_each = local.notification_configs |
| 46 | + |
| 47 | + account_id = cloudflare_account.account.id |
| 48 | + name = each.value.name |
| 49 | + enabled = true |
| 50 | + alert_type = "billing_usage_alert" |
| 51 | + |
| 52 | + filters = { |
| 53 | + limit = [each.value.limit] |
| 54 | + product = [each.value.product] |
| 55 | + } |
| 56 | + |
| 57 | + mechanisms = { |
| 58 | + email = [{ |
| 59 | + id = var.notification_email |
| 60 | + }] |
| 61 | + } |
| 62 | +} |
0 commit comments