0

Presently, I am deleting app engine active instances as outlined here: deleting instances

Code:

import requests
from apiclient.discovery import build
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file('credentials.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/appengine.admin',"https://www.googleapis.com/auth/cloud-platform"])
appengine = build(serviceName="appengine",version="v1",credentials=scoped_credentials)

VERSION_ID = "version_id"
PROJECT_ID = "project_id"
SERVICE_ID = "appengine_service_name"
APP_URL = "http://some_url.com"

active_instances_dict = appengine.apps().services().versions().instances().list(servicesId=SERVICE_ID,appsId=PROJECT_ID,versionsId=VERSION_ID).execute()
list_of_instances = active_instances_dict["instances"]

for instance in list_of_instances:
    appengine.apps().services().versions().instances().delete(servicesId=SERVICE_ID,appsId=PROJECT_ID,
                  versionsId=VERSION_ID,instancesId=instance["id"]).execute()

After this, new instances are initialized only when someone accesses the web app.

Is there a way I can programmatically initialize the instances to ensure there is already an active instance for someone accessing the web app?

Note: I'm using Standard Python 3.7 runtime with auto scaling

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
  • If you are using `automatic_scaling` why don't you just specify [min_instances](https://cloud.google.com/appengine/docs/standard/python3/config/appref#automatic_scaling_min_instances)? You can set `min_instances` to `1` ensuring your app has an active instance. Note that this feature will require you to also enable [warmup requests](https://cloud.google.com/appengine/docs/standard/python3/configuring-warmup-requests). – Deniss T. Nov 19 '20 at 08:14

1 Answers1

0

There is no need to do it programmatically - if you are using automatic_scaling you can just specify min_instances in your app.yaml file.

Setting min_instances to 1 will ensure your app has an active instance ready to serve traffic. Note that this will require you to enable warmup requests as well.


Keep in mind, however, that you will be charged for the number of instances specified whether they are receiving traffic or not.

Deniss T.
  • 2,526
  • 9
  • 21
  • I am presently using `min_idle_instances` in the *app.yaml* configs. However, haven't enabled warmup requests. Probably that's the cause for the instances dropping down to 0 every often – data_cruncher Nov 19 '20 at 15:13
  • I dug into using warmup requests for my use case. However, the issue with the warmup requests is that it requires to have an instance running to warmup another one if an increase in traffic is expected. Also, this request is not guaranteed. Alternatively, using cron job as a tool to wake up an instance and keep it alive worked for me. Discussion on this [link1](https://stackoverflow.com/questions/12574741/cron-job-on-google-app-engine-to-speed-up-pages), [link2](https://stackoverflow.com/questions/37780275/google-app-engine-automatic-scaling-with-always-on-instance) – data_cruncher Jan 09 '21 at 23:20