Terraform for GCP How to create Cloud Scheduler

Paul Ravvich
Terraform for the Google Cloud Platform
2 min readMay 5, 2024

--

Terraform for GCP How to create Cloud Scheduler

Hi, this is Paul, and welcome to the #33 part of my Terraform guide. Today we will discuss, how to create a Cloud Scheduler using the Terraform script.

Add Role for Service Account

First, to create a Cloud Run you need to add a Role Cloud Run Admin to you're Service Account. How to add permissions we already discussed in this article:

Description of the google_cloud_scheduler_job

The example below creates a google_cloud_scheduler_job named demo_job:

resource "google_cloud_scheduler_job" "demo_job" {
paused = true
name = "demo-job"
description = "Demo job"
schedule = "* * * * *"
time_zone = "America/New_York"
attempt_deadline = "320s"

retry_config {
retry_count = 1
}

http_target {
http_method = "POST"
uri = "https://example.com"
body = base64encode("{\"foo\":\"bar\"}")
headers = {
"Content-Type" = "application/json"
}
}
}

Key Parameters

  1. paused: Indicates whether the job is active. If true, the job is created in a paused state and will not execute until it is activated.
  2. name: The name of the job in Cloud Scheduler. It must be unique within your Google Cloud region.
  3. description: A description of the job that helps identify its purpose.
  4. schedule: A cron-like schedule according to which the job will be executed. The format * * * * * means the job will run every minute.
  5. time_zone: The time zone in which the schedule is interpreted. This is important for accurate timing of jobs.
  6. attempt_deadline: The maximum execution time for the job. After this period, the execution attempt will be automatically terminated.

retry_config Block

Defines the retry policy:

  • retry_count: The number of retry attempts for the job if the previous attempt failed.

http_target Block

Defines the HTTP request that will be executed when the job is triggered:

  • http_method: The HTTP method to be used for the request (e.g., GET, POST).
  • uri: The URI where the request will be sent.
  • body: The request body is in base64 format. In this case, it sends the JSON {"foo":"bar"}.
  • headers: HTTP headers that will be added to the request. Here, Content-Type is specified as application/json.

Conclusion

Using Terraform to manage Google Cloud Scheduler jobs provides flexibility and automation in managing tasks on a cloud platform. The described example demonstrates a basic configuration that can be expanded and adapted to meet the specific needs of projects. This makes Terraform a powerful tool in the cloud developer’s arsenal.

Thank you for reading until the end. Before you go:

Paul Ravvich

--

--

Paul Ravvich
Terraform for the Google Cloud Platform

Software Engineer with over 10 years of XP. Join me for tips on Programming, System Design, and productivity in tech! New articles every Tuesday and Thursday!