With App Engine, you only pay for the resources that you use beyond thefree quotas. After you exceed the free quotas, your costsscale with the amount of traffic that your application receives.
To limit the costs of your App Engine app, you can use any of thefollowing 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 yourapp receives, you can limit your costs by limiting thenumber of instancesApp Engine can create.
Setting the maximum to 1 instance usually keeps your instance hour usage withinthe free tier. However, setting the maximum too low may prevent your app fromhaving enough instances to adequately serve incoming requests.
To specify the maximum number of instances, use the max_instancessettingin your app's app.yaml
file.
Create budget alerts
Budget alerts send a notification when your costs rise above a threshold youspecify. When you receive a notification, you can limit costs by lowering thevalue of the max_instances
setting or disabling your app.
To get started with budget alerts, see Set budget alerts.
Select the scope of a budget alert
You can create budget alerts for the total cost of all Google Cloud servicesin one or more projects, or just for the cost of App Engine.
To create a budget alert just for costs fromApp Engine, in the Budget alert's Products field, select App Engine.The alert will trigger when the total cost from all App Engine resourcesexceed the amount you specify, including:
- Instance hours in the App Engine standard environment
- Instance hours and RAM in the App Engine flexible environment
- Bundled App Engine services
For the full list of billable App Engine resources, seeApp Engine SKUs.
Disable your app manually
Disabling your app temporarily stops it from serving and incurring billingcharges related to serving your app. All of your app's data and configurationsettings remain unchanged, and when you are ready to start running your appagain, you can enable it.
While the app is disabled, requests to your app will fail. You may continue toincur charges from other Google Cloud products. For example, if your projecthas exceeded the free quota forCloud Storage, you will continueto incur charges for storage.
For more information, seeDisable an app.
Disable your app programmatically
You can use Budget Alerts, Pub/Sub, and Cloud Run functions toautomatically 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 whileyour app is disabled.
To programmatically disable your app:
Create a budget alert that sends a notification to a Pub/Subtopic. For details, see Manage programmaticnotifications.
To create a budget alert just for costs from App Engine, in theBudget's Products field, select App Engine. For information about theresources that can trigger this alert, seeCreate budget alerts.
In Cloud Run functions create a function that is triggered by thePub/Sub topic. For details, see Create a CloudFunction.
When creating the function:
Use the following source code:
import base64import jsonimport osfrom googleapiclient import discoveryAPP_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()
Add the following dependencies to your function's
requirements.txt
file:google-api-python-client==2.131.0
Under Function to execute enter
limit_use_appengine
.Click Environment variables, networking, timeouts and more.
Select a service account that has the App Engine Admin role.The App Engine default serviceaccount has this roleby default.
Test the function.
When the budget alert is triggered, an email is sent to users in yourCloud Billing account, andyour function starts disabling your app. It may take few minutes to completethis process.
To verify that the function ran successfully, view the App Enginedashboard. 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 toApplication settings and click Enable application.
Go to Application settings