Change backend instance class programmatically on Google App Engine

562 views Asked by At

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()
3

There are 3 answers

1
glmvrml On BEST ANSWER

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

0
Serge Hendrickx On

This might not be what you're looking for, but it's a possible way of achieving what you want.

Set up a system on container engine or something like that which would automatically pull the latest code from your repo, automatically adjust the instance type and automatically do a redeploy. You could make it deploy different instance types on different times. A new redeploy would be needed for every change in instance class, but these could be fully automated in theory so this would be possible.

Thought? Is this a possible solution for you?

0
Allen On

Task Queue can run for 10 minutes, check out the document