Change backend instance class programmatically on Google App Engine
<p>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.)</p>

<p>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. </p>

<p>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.</p>

<p>However, looking at the <a href="https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.modules.modules" rel="nofollow">Modules API</a>, I could not find a way to do so.</p>

<p>So how can I do so?</p>

<p><strong>Edit after getting an answer by Ramiel</strong></p>

<p>In the end I used the Admin API as follows:</p>

<pre><code># 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()
</code></pre>