{"id":3380,"date":"2022-10-14T11:36:58","date_gmt":"2022-10-14T11:36:58","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=3380"},"modified":"2024-02-06T07:12:33","modified_gmt":"2024-02-06T07:12:33","slug":"cloud-function-triggers","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/cloud-function-triggers","title":{"rendered":"How to Schedule Cloud Function Triggers using Cloud Scheduler?"},"content":{"rendered":"<h3><strong>Cloud Functions\u00a0<\/strong><\/h3>\n<p>This an event-driven fully managed compute platform provided by <a href=\"https:\/\/www.checkmateq.com\/google-cloud\">Google Cloud Platform<\/a>, here <strong>event-driven<\/strong> means it is only triggered when an event being watched is fired, and you are only billed while your function is running.\u00a0 so, if your function is idle, the cost is zero. it is easy to setup, just write your logic in any of the supporting languages and trigger it via HTTP requests or File uploads to cloud storage or Events and Pub\/Sub and fire base, it offers auto scaling, built-in security, and you can also get all the system usage metrics and logs for free as it&#8217;s a fully managed service.<\/p>\n<h3><strong>Cloud Scheduler\u00a0<\/strong><\/h3>\n<p><a href=\"https:\/\/www.checkmateq.com\/cloud\">Cloud<\/a> Scheduler is a fully managed\u00a0 cron job scheduler provided by Google Cloud Platform, it allows you to schedule virtually any job like batch processing, big data jobs, cloud infrastructure operations and many more, you can automate almost everything like\u00a0 Maximum retry attempts, Max retry duration, Minimum and Maximum backoff duration (time to wait before retrying a job after it fails)in case of failure to reduce intervention, it comes with many supported targets like App Engine, Cloud Pub\/Sub, and HTTP endpoints which allows jobs to trigger Compute Engine, Google <a href=\"https:\/\/www.checkmateq.com\/kubernetes\">Kubernetes<\/a> Engine, and other on-premises resources.<\/p>\n<p>In this blog let&#8217;s deploy a <a href=\"https:\/\/www.checkmateq.com\/python-development\">python<\/a> function on Cloud Functions to copy a blob from cloud storage bucket and copy it another other with time stamp and trigger the function for every hour using Cloud Scheduler, python code I am using is given below.<\/p>\n<h5>rename.py<\/h5>\n<pre> \r\nfrom google.cloud import storage\r\nimport datetime\r\n\r\ndef rename_blob():\r\n    time = datetime.datetime.now().strftime(\"%H%M%S\")\r\n    bucket_name = \"aksshaay\"\r\n    blob_name = \"aksshaay.txt\"\r\n    destination_bucket_name = \"aksshaay\"\r\n    destination_blob_name = \"aksshaay\"+time\r\n  \r\n\r\n    storage_client = storage.Client()\r\n\r\n    source_bucket = storage_client.bucket(bucket_name)\r\n    source_blob = source_bucket.blob(blob_name)\r\n    destination_bucket = storage_client.bucket(destination_bucket_name)\r\n\r\n    blob_copy = source_bucket.copy_blob(\r\n        source_blob, destination_bucket, destination_blob_name\r\n    )\r\n\r\n    return(\r\n        \"Blob {} in bucket {} copied to blob {} in bucket {}.\".format(\r\n            source_blob.name,\r\n            source_bucket.name,\r\n            blob_copy.name,\r\n            destination_bucket.name,\r\n        )\r\n    )\r\n\r\n<\/pre>\n<p>And the requirement.txt will have the following<\/p>\n<pre>google-cloud-storage==2.5.0<\/pre>\n<p>&nbsp;<\/p>\n<p>On your GCP console search for Cloud Functions API and enable it, then go to Cloud Function to create a new Function, select product generation and Region (Comparision of generations is given below)<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3373 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1-300x152.jpeg\" alt=\"\" width=\"900\" height=\"456\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-GCP-Solutions-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>GCP offers 2 product versions (1st and 2nd generation) for Cloud Function service basic comparison between both versions.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3370 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Cloud-Infrastructure-Solutions-300x182.jpeg\" alt=\"\" width=\"789\" height=\"479\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Cloud-Infrastructure-Solutions-300x182.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Cloud-Infrastructure-Solutions-1024x622.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Cloud-Infrastructure-Solutions-768x467.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Cloud-Infrastructure-Solutions-1200x729.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Cloud-Infrastructure-Solutions.jpeg 1275w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Select authentication for HTTPS trigger, you can also select any event trigger.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3374 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services-300x152.jpg\" alt=\"\" width=\"945\" height=\"479\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services-300x152.jpg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services-1024x519.jpg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services-768x389.jpg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services-1536x778.jpg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services-1200x608.jpg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Services.jpg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Then configure Runtime, build and Connections as required,<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3375 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Devops-Engineer-300x153.jpeg\" alt=\"\" width=\"989\" height=\"504\" \/><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3376 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1-300x153.jpeg\" alt=\"\" width=\"909\" height=\"464\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1-1024x524.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1-768x393.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1-1536x786.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1-1200x614.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-cloud-infrastructure-service-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Then select the Runtime you want to use also select how you want to provide source code and give entry point (exported function) i.e., function that should be called when triggered, to configure your code please follow the format given by cloud function when you select a runtime, then deploy it.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3377 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support-300x152.jpg\" alt=\"\" width=\"921\" height=\"467\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support-300x152.jpg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support-1024x520.jpg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support-768x390.jpg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support-1536x780.jpg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support-1200x609.jpg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Devops-Support.jpg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Once it&#8217;s deployed successfully you can use HTTPS link to invoke the function, as mentioned you will get all the metrics without need of installing any agent or paying extra, you will only pay for what you use.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3378 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer-300x153.jpeg\" alt=\"\" width=\"939\" height=\"479\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer-1024x522.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Hire-Checkmate-cloud-engineer.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Configure Cloud Scheduler to Trigger Cloud Function on a Specific Time Interval<\/h3>\n<p>Go to cloud scheduler on your cloud console to Create a Cron Job, give a name and select region and time zone, configure the job frequency, if are new to cron jobs refer this <a href=\"https:\/\/www.checkmateq.com\/blog\/how-to-schedule-a-cronjob-for-seconds\/\">blog <\/a> to learn about cron jobs and setting a cron expression.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3381 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions-300x152.jpeg\" alt=\"\" width=\"953\" height=\"483\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Infrastructure-Solutions.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>Then continue to configure execution, set the target type to<strong> http<\/strong>, give the URL copied from Cloud Functions and set the http method,<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3382 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service-300x153.jpeg\" alt=\"\" width=\"932\" height=\"475\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-offshore-it-service.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>you can also retry configuration (when a job is not completed successfully it is retried according to the retry configuration you set), in retry configuration you can set Maximum retries, max duration, maximum backoff and minimum backoff.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-3383 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services-300x153.jpeg\" alt=\"\" width=\"916\" height=\"467\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-Cloud-Devops-Services.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Please contact us for <a href=\"https:\/\/www.checkmateq.com\/virtual-cto-services\">Virtual CTO Services<\/a> for any technology strategy, engineering team management, cloud infrastructure management services <a href=\"https:\/\/www.checkmateq.com\">website<\/a>, LinkedIn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cloud Functions\u00a0 This an event-driven fully managed compute platform provided by Google Cloud Platform, here event-driven means it is only triggered when an event being watched is fired, and you are only billed while your function is running.\u00a0 so, if your function is idle, the cost is zero. it is easy to setup, just write &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/cloud-function-triggers\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Schedule Cloud Function Triggers using Cloud Scheduler?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":3388,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,79,2,71,70,69,68,59,11,78,63,23,16,24,80,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3380"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=3380"}],"version-history":[{"count":9,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3380\/revisions"}],"predecessor-version":[{"id":4484,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3380\/revisions\/4484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/3388"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=3380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=3380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=3380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}