0

I need to connect google cloud SQL server from google cloud function in python and write data every 30 minutes.

I have a python function which create pandas data frame and with another function I connected Google cloud SQL server and write this pandas data frame to corresponding table in Microsoft SQL server management . But I need to run this functions every 20 minutes periodically. So, my idea is create cloud function and then create google cloud scheduler work to run these python functions every 20 minutes. But I can not found clear guide for this. Google cloud documentation is very confused. Any suggestion are appreciated.

nihad1918
  • 33
  • 6
  • It sounds like you know what you need to do. If you are stuck on a specific task, please edit the question to explain that, or share the code that isn't working the way you expect. – Doug Stevenson Oct 26 '20 at 15:19
  • See https://dev.to/googlecloud/moving-your-cron-job-to-the-cloud-with-google-cloud-functions-1ecp for an example – Dustin Ingram Oct 26 '20 at 20:30
  • @Dustin, your example would not work, they do not tell anything about setting permissions which is necessary to invoke the cloud function. – Cloudkollektiv Oct 26 '20 at 20:44

1 Answers1

1

We have a similar setup in our project. To trigger a Cloud Function, you will need to create an HTTP cloud function. This http cloud function then needs to be triggered with a cloud scheduler that sends a request to the created endpoint. This is an example of the commands we use for that:

# Create cloud function
gcloud functions deploy my_function \
  --entry-point=my_entrypoint \
  --runtime=python37 \
  --trigger-http \
  --region=europe-west1

# Set invoke permissions
gcloud functions add-iam-policy-binding my_function 
  --region=europe-west1 \
  --member=${PROJECT_ID}@appspot.gserviceaccount.com \
  --role="roles/cloudfunctions.invoker" \
  --project=${PROJECT_ID}

# Deploy scheduler
gcloud scheduler jobs create http my_job \
    --schedule="every 20 minutes" \
    --uri="https://europe-west1-${PROJECT_ID}.cloudfunctions.net/my_function/" \
    --http-method=POST \
    --oidc-service-account-email="${PROJECT_ID}@appspot.gserviceaccount.com" \
    --oidc-token-audience="https://europe-west1-${PROJECT_ID}.cloudfunctions.net/my_function" \
    --project=${PROJECT_ID}

There is a detailed description here on how to accomplish this setup through the console.

Cloudkollektiv
  • 11,852
  • 3
  • 44
  • 71