Skip to content

Google Cloud

BigQuery

I will be using BigQuery for storing the following data:

  • primary temperature measurements and error messages
  • chat IDs for Telegram bots.

Python API

  • To run the client library, you must first set up authentication.

    • Create the service account:gcloud iam service-accounts create bgtest
    • Grant role admin to the service account.

      gcloud projects add-iam-policy-binding tarasovka --member="serviceAccount:bgtest@project_name.iam.gserviceaccount.com" --role=roles/admin
      

    • Generate the key file:

      cd $HOME/devel/ && gcloud iam service-accounts keys create bgtest.json --iam-account=bgtest@project_name.iam.gserviceaccount.com
      

    • Set GOOGLE_APPLICATION_CREDENTIALS env var:

      set -x GOOGLE_APPLICATION_CREDENTIALS "______.json"
      

  • Install library in Python

    pip3 install google-cloud-bigquery
    

Google Functions

I will use Google Function for the following tasks:

  • process messages from IoT devices
  • handle http requests from Telegram
  • regular notification via Cron.

How to deploy a Cloud Function

Cloud Functions’ arguments

When deploying a new Cloud Function you nedd to specify –trigger

Example of Google Function with trigger-topic:

gcloud functions deploy function_name                        \
  --gen2                                                     \
  --region=region_name                                       \
  --trigger-topic=topic_name                                 \
  --runtime=python310                                        \
  --source=path_to_directory                                 \
  --entry-point=function_name                                \
  --allow-unauthenticated

Example of Google Function with trigger-http:

gcloud functions deploy function_name                        \
  --gen2                                                     \
  --region=region_name                                       \
  --trigger-http                                             \
  --runtime=python310                                        \
  --source=path_to_directory                                 \
  --entry-point=function_name                                \
  --allow-unauthenticated

If you wish to delete an exicting Google Function do this:

gcloud functions delete function_name --gen2 --region region_name

Job Sheduler

For receiving data from sensors every day at the exact time you need to use Cron to shedule jobs.

Setup cron job that sends a request at 6:00, 15:00, 18:00 (London time) every day:

gcloud sheduler jobs create http fucntion_name \
  --location=path_to_function                  \
  --shedule="0 6,15,18 1-31 1-12 0-7"          \
  --uri=""                                     \
  --http-method=GET                            \
  --time-zone=zone_name