Search code examples
google-app-enginegae-modulegae-backends

Change backend instance class programmatically on Google App Engine


I am using backend instances for an Google App Engine project. (Frontend instances can't handle requests longer than 60 seconds — I need longer time.)

I chose B4 instance type because sometimes the load is high. However, on certain times (let's say 2am to 7am) the load is so low that having an B4 instance is overkill.

I want to make a cron job that changes the type of that instance to B2 on certain times and back to B4 on other times to save cost.

However, looking at the Modules API, I could not find a way to do so.

So how can I do so?

Edit after getting an answer by Ramiel

In the end I used the Admin API as follows:

# Construct the api client
cred = GoogleCredentials.get_application_default()
svc = discovery.build('appengine', 'v1', credentials=cred)
vapi = svc.apps().services().versions()

# get list of versions
o = vapi.list(appsId=app_identity.get_application_id(), servicesId=modules.get_current_module_name()).execute()

# PATCH all SERVING versions with the new instanceClass
for v in o['versions']:
    if v['servingStatus'] == 'SERVING':
        result = vapi.patch(
            appsId=app_identity.get_application_id(),
            servicesId=modules.get_current_module_name(),
            versionsId=v['id'],
            updateMask='instanceClass',
            body={
                'instanceClass': instanceClass
            }
        ).execute()

Solution

  • Checkout admin-api endpoints

    https://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps.services.versions/patch

    If this does not work for some reason, you can also deploy multiple versions of app with various instance/scaling settings and programmatically switch them with start_version from Modules API


    btw, if you switch to manual scaling you don't have 60s limit