deep-rock-development / auto-stop-firebase-ext

Firebase extension to stop services when a quota has been reached.
8 stars 1 forks source link

Auto Stop Services Firebase Extension

"Buy Me A Coffee"

What is this?

This extension is intended to support development, or early stage production environments from accruing significant costs by stopping services. This is a challenge as cloud platforms are usage based, and many methods provided by cloud providers are intended to support service uptime; and will not make any effort to mitigate unintended service overuse. Billing alerts are out of the box only intended to alert administrators that a budget threshold has been reached, but not take any action.

How does it work?

High Level View of Extension This Firebase extension monitors the associated budget and implements one of two strategies when a specified budget threshold is reached:

  1. Remove billing account from the project (Strategy 1)
  2. Disable predefined services (Strategy 2)

Strategy 1: Removing the Billing Account from Project

When a billing account is removed from a project:

This strategy prevents additional costs from accruing but has an immediate impact on service availability.

Strategy 2: Disable Predefined Services

When specific services are disabled (e.g., compute.googleapis.com, cloudfunctions.googleapis.com):

This strategy offers granular control over cost management by allowing selective service suspension while preserving critical operational functionality.

Installation Configuration

Extension Level View During the extension installation, you will be asked to configure it with the following parameters:

Manual Steps After Installation

After installing the extension, you must:

  1. Set Up a Budget

    • Firebase: Go to Settings > Usage & Billing. Set a budget and note its name.
    • GCP: Navigate to Billing > Budgets. Create a new budget, set it, and note the budget name.
  2. Connect the Budget to a Predefined Topic

    • Link the budget to the topic specified when installed the extension: {TOPIC_NAME}.
  3. Update the Service Account Permissions

    • Grant the following IAM roles to {EXTENSION_NAME}@{PROJECT_ID}.iam.gserviceaccount.com:
      • Strategy 1: Grant Project Billing Manager (roles/billing.projectManager) to manage project billing settings.
      • Strategy 2: Service Usage Admin (roles/serviceusage.serviceUsageAdmin) for enabling/disabling service APIs.

Note: Disabling the Cloud Functions service API (cloudfunctions.googleapis.com) will remove the extension's functions, effectively uninstalling the extension. This will always be disabled last.

Example Pub/Sub Message

When a budget alert fires, the message content will look like the below:

{
  "budgetDisplayName": "Your Budget Name",
  "alertThresholdExceeded": 0.5,
  "costAmount": 500.0,
  "costIntervalStart": "2021-01-01T00:00:00Z",
  "budgetAmount": 1000.0,
  "budgetAmountType": "SPECIFIED_AMOUNT",
  "currencyCode": "USD",
  "schemaVersion": "1.0",
  "notificationType": "ACTUAL",
  "projectId": "your-project-id",
  "billingAccountId": "012345-6789AB-CDEF01"
}

The key element of this message is the alertThresholdExceeded, all other information is effectively discarded.

Testing this Extension

Once you have followed the steps, you can submit a message through the pub/sub topic. Any results will be published as logs, which you can monitor.

This example message will not execute any strategy.

{
  "extensionTest": true
}