Managing App Engine costs

With App Engine, you only pay for the resources that you use beyond the free quotas. After you exceed the free quotas, your costs will scale with the amount of traffic your application receives.

To limit the costs of your App Engine app, you can use any of the following mechanisms:

  • Specify the maximum number of instances
  • Create budget alerts
  • Disable your app manually
  • Disable your app programmatically

Specify the maximum number of instances

Since App Engine costs usually scale based on the amount of traffic your app receives, you can limit your costs by limiting the number of instances App Engine can create.

Setting the maximum to 1 instance usually keeps your instance hour usage within the free tier. However, setting the maximum too low may prevent your app from having enough instances to adequately serve incoming requests.

To specify the maximum number of instances, use the max_instances setting in your app's app.yaml file.

Create budget alerts

Budget alerts send a notification when your costs rise above a threshold you specify. When you receive a notification, you can limit costs by lowering the value of the max_instances setting or disabling your app.

To get started with budget alerts, see Set Budget Alerts.

Selecting the scope of a budget alert

You can create budget alerts for the total cost of all Google Cloud services in one or more projects, or just for the cost of App Engine.

To create a budget alert just for costs from App Engine, in the Budget alert's Products field, select App Engine. The alert will trigger when the total cost from all App Engine resources exceed the amount you specify, including:

For the full list of billable App Engine resources, see App Engine SKUs.

Disable your app manually

Disabling your app temporarily stops it from serving and incurring billing charges related to serving your app. All of your app's data and configuration settings remain unchanged, and when you are ready to start running your app again, you can enable it.

While the app is disabled, requests to your app will fail. You may continue to incur charges from other Google Cloud products. For example, if your project has exceeded the free quota for Cloud Storage, you will continue to incur charges for storage.

For more information, see Disabling an app.

Disable your app programmatically

You can use Budget Alerts, Pub/Sub, and Cloud Functions to automatically disable your app when your costs exceed a threshold you specify.

As with manually disabling an app:

  • All of your app's data and configuration settings remain unchanged.
  • When you are ready to start running your app again, you can enable it.
  • Requests to your app will fail while the app is disabled.
  • You may continue to incur charges from other Google Cloud products while your app is disabled.

To programmatically disable your app:

  1. Create a budget alert that sends a notification to a Pub/Sub topic. For details, see Manage programmatic notifications.

    To create a budget alert just for costs from App Engine, in the Budget's Products field, select App Engine. For information about the resources that can trigger this alert, see Create budget alerts.

  2. In Cloud Functions create a function that is triggered by the Pub/Sub topic. For details, see Create a Cloud Function.

    When creating the function:

    1. Use the following source code:

      import base64
      import json
      import os
      from googleapiclient import discovery
      APP_NAME = os.getenv('GCP_PROJECT')
      
      
      def limit_use_appengine(data, context):
          pubsub_data = base64.b64decode(data['data']).decode('utf-8')
          pubsub_json = json.loads(pubsub_data)
          cost_amount = pubsub_json['costAmount']
          budget_amount = pubsub_json['budgetAmount']
          if cost_amount <= budget_amount:
              print(f'No action necessary. (Current cost: {cost_amount})')
              return
      
          appengine = discovery.build(
              'appengine',
              'v1',
              cache_discovery=False
          )
          apps = appengine.apps()
      
          # Get the target app's serving status
          target_app = apps.get(appsId=APP_NAME).execute()
          current_status = target_app['servingStatus']
      
          # Disable target app, if necessary
          if current_status == 'SERVING':
              print(f'Attempting to disable app {APP_NAME}...')
              body = {'servingStatus': 'USER_DISABLED'}
              apps.patch(appsId=APP_NAME, updateMask='serving_status', body=body).execute()
    2. Add the following dependencies to your function's requirements.txt file:

      google-api-python-client==1.10.0
      
    3. Under Function to execute enter limit_use_appengine.

    4. Click Environment variables, networking, timeouts and more.

    5. Select a service account that has the App Engine Admin role. The App Engine default service account has this role by default.

  3. Test the function.

When the budget alert is triggered, an email is sent to users in your Cloud Billing account, and your function starts disabling your app. It may take few minutes to complete this process.

To verify that the function ran successfully, view the App Engine dashboard. A message appears near the top to indicate that your app is disabled.

Go to the App Engine dashboard

When you want your app to continue serving requests, go to the Application settings page and click Enable application.

Go to the application settings page

Changing or removing a spending limit

Older App Engine apps could create spending limits to specify an approximate maximum amount you can be charged for using App Engine resources.