forked from 2i2c-org/infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 2i2c-org#4351 from sgibson91/budgetalerts
Add budget alerts based on forecasts
- Loading branch information
Showing
20 changed files
with
220 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(howto:enable-budget-alerts)= | ||
# Enable Budget Alerts | ||
|
||
This document describes how to enable budget alerts for a cluster. | ||
|
||
```{note} | ||
This feature is currently only available on GCP! | ||
``` | ||
|
||
## GCP | ||
|
||
```{attention} | ||
We can only enable budget alerting on GCP projects where we have enough permissions to enable APIs and view the billing account. | ||
``` | ||
|
||
First, ensure the following APIs are enable on the GCP project you'd like to enable budget alerting for: | ||
|
||
- [Cloud Resource Manager API](https://console.cloud.google.com/apis/library/cloudresourcemanager.googleapis.com) | ||
- [Cloud Billing Budget API](https://console.cloud.google.com/apis/library/billingbudgets.googleapis.com) | ||
|
||
Then edit the following variables in the relevant `.tfvars` file for the cluster. | ||
|
||
- **Set `budget_alert_enabled = false`**, or delete the variable altogether (it is set to `true` in the `variables.tf` file). | ||
This will ensure that the relevant resources will be created by terraform. | ||
- **Set `billing_account_id`.** | ||
This is the ID for the billing account linked to the project. | ||
- You can find the ID by visiting the [Billing console](https://console.cloud.google.com/billing/linkedaccount?project=two-eye-two-see), ensuring the correct project is selected in the dropdown at the top. | ||
In the dialogue box, click "Go to Linked Billing Account", and then click "Manage Billing Account" along the top. | ||
This will open a pane that gives you the Billing Account ID. | ||
- For accounts that we don't manage, the process is the same but _we may not have permission to view the Billing Account ID_. | ||
In this case, we cannot enable budget alerting for this project. | ||
- **Set `budget_alert_amount`.** | ||
Current practice is to set this to the average expenditure of the last 3 months, plus 20%. | ||
You can find values to calculate that in the [Billing Reports console](https://console.cloud.google.com/billing/0157F7-E3EA8C-25AC3C/reports?organizationId=184174754493&project=two-eye-two-see). | ||
_Make sure you select only the project you are interested in from the Projects field in the Filters pane on the right side of the screen._ | ||
- If you are setting this up for a new cluster, you obviously don't have this information yet! | ||
Instead, set the value to something like `500` and we can adjust as the community begins to use it. | ||
|
||
With these variables set, you are ready to open a PR and perform a terraform apply! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(topic:billing:budget-alerts)= | ||
# Cloud Billing Budget Alerts | ||
|
||
"I forgot to turn off my cloud resources, your honor" as a reason for declaring | ||
bankruptcy is second only to "The US healthcare system sucks, your honor" in the | ||
US court system. "How much is my cloud going to cost?" is a big anxiety for a lot | ||
of our users, and hence us. We set up billing alerts to help deal with this anxiety. | ||
|
||
See [](howto:enable-budget-alerts) for instructions on enabling this feature. | ||
|
||
## When are the alerts triggered? | ||
|
||
Budget alerts are sent under two conditions: | ||
|
||
1. When *forecasted monthly spend* at end of the month goes over our spending limit. | ||
This is an *early warning* system, that helps us evaluate where spend is going | ||
and make sure this is expected. | ||
2. When *current actual spend* goves over 100% of our spending limit. | ||
|
||
## What to do when we receive an alert? | ||
|
||
The current goal is to just make sure we don't end up spending *wildly* more money | ||
than budgeted. So if the forecasted spend busts through on day 5 of the month, | ||
we might need to do something different than if it does on day 30. If it is expected | ||
to overshoot by 500% vs by 10$, our actions might be different. One valid action is | ||
we just adjust the forecast. As an organization, we need more experience with costs | ||
to figure out what the right thing to do is. So our current primary goal would | ||
be to work with our stakeholders and gather that experience. | ||
|
||
## Where are these alerts sent? | ||
|
||
Budget alerts are "Cliff Alerts" - they don't indicate a current outage (unlike | ||
uptime checks), but indicate that we are perhaps heading in a direction that will | ||
cause problems soon if we do not course correct. Hence, we do not send them to | ||
PagerDuty but to our `[email protected]` email address. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ chargeable-resources | |
accounts | ||
reports | ||
tools | ||
``` | ||
budget-alerts | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Alerts sent to [email protected] for things that *will go bad* in the future | ||
# if left unattended. Should *not* be used for immediate outages | ||
|
||
resource "google_monitoring_notification_channel" "support_email" { | ||
count = var.budget_alert_enabled ? 1 : 0 | ||
project = var.project_id | ||
display_name = "[email protected] email" | ||
type = "email" | ||
labels = { | ||
email_address = "[email protected]" | ||
} | ||
} | ||
|
||
data "google_project" "project" { | ||
project_id = var.project_id | ||
} | ||
|
||
# Need to explicitly enable https://console.cloud.google.com/apis/library/billingbudgets.googleapis.com?project=two-eye-two-see | ||
resource "google_billing_budget" "budget" { | ||
count = var.budget_alert_enabled ? 1 : 0 | ||
|
||
billing_account = var.billing_account_id | ||
display_name = "Billing alert" | ||
|
||
budget_filter { | ||
# Use project number here, as project_name seems to be converted internally to number | ||
# If we don't do this, `terraform apply` is not clean | ||
# This is a bug in the google provider / budgets API https://github.com/hashicorp/terraform-provider-google/issues/8444 | ||
projects = ["projects/${data.google_project.project.number}"] | ||
credit_types_treatment = "INCLUDE_ALL_CREDITS" | ||
} | ||
|
||
amount { | ||
specified_amount { | ||
currency_code = var.budget_alert_currency | ||
units = var.budget_alert_amount | ||
} | ||
} | ||
|
||
all_updates_rule { | ||
monitoring_notification_channels = [ | ||
google_monitoring_notification_channel.support_email[0].id, | ||
] | ||
disable_default_iam_recipients = true | ||
} | ||
# NOTE: These threshold_rules *MUST BE ORDERED BY threshold_percent* in ascending order! | ||
# If not, we'll run into https://github.com/hashicorp/terraform-provider-google/issues/8444 | ||
# and terraform apply won't be clean. | ||
threshold_rules { | ||
# Alert when *actual* spend reached 80% of budget | ||
threshold_percent = 1.0 | ||
spend_basis = "CURRENT_SPEND" | ||
} | ||
threshold_rules { | ||
# Alert when *forecasted* spend is about to blow over our budget | ||
# Adding the extra 1% to help terraform not redo this each time. | ||
threshold_percent = 1.01 | ||
spend_basis = "FORECASTED_SPEND" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,11 @@ project_id = "{{ project_id }}" | |
zone = "{{ cluster_region }}" | ||
region = "{{ cluster_region }}" | ||
|
||
# Config required to enable automatic budget alerts to be sent to [email protected] | ||
budget_alert_enabled = false | ||
budget_alert_amount = "" | ||
billing_account_id = "" | ||
|
||
# TODO: Before applying this, identify a k8s version to specify. Pick the latest | ||
# k8s version from GKE's regular release channel. Look at the output | ||
# called `regular_channel_latest_k8s_versions` as seen when using | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters