Schedule Start/stop on GCP SQL Instance

自作多情 提交于 2020-12-15 07:26:21

问题


I want to schedule my SQL Instance of GCP. How to Trigger start/ stop SQL Instance automatically?

I already successfully scheduled the compute Engine VM but stuck in SQL Instance scheduling.


回答1:


In order to achieve this you can use a Cloud Function to make a call to the Cloud SQL Admin API to start and stop your Cloud SQL instance (you will need 2 Cloud functions)

def hello_world(request):

instance = 'test'  # TODO: Update placeholder value.
request = service.instances().get(project=project, instance=instance)
response = request.execute()
j = response["settings"]
settingsVersion = int(j["settingsVersion"])

dbinstancebody = {
   "settings": {
       "settingsVersion": settingsVersion,
       "tier": "db-n1-standard-1",
       "activationPolicy": "Always"
   }
}

request = service.instances().update(
   project=project,
   instance=instance,
   body=dbinstancebody)
response = request.execute()
pprint(response)

request_json = request.get_json()

if request.args and 'message' in request.args:
    return request.args.get('message')
elif request_json and 'message' in request_json:
    return request_json['message']
else:
    return f"Hello World!"

requirements.txt

google-api-python-client==1.7.8
google-auth-httplib2==0.0.3
google-auth==1.6.2
oauth2client==4.1.3

You can see my code on how to use a Cloud Function to start a Cloud SQL instance and stop a Cloud SQL instance

After creating your Cloud Function you can configure the Cloud Scheduler to trigger the HTTP address of each Cloud function or you can follow the recommended approach of this guide and trigger the functions with pub/sub



来源:https://stackoverflow.com/questions/61100899/schedule-start-stop-on-gcp-sql-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!