INVALID_ARGUMENT occurs when trying to pass request body from Cloud Scheduler to Cloud Run Job

I am currently trying to invoke and execute a Cloud Run Job from Cloud Scheduler using Terraform. The goal is to pass data to the Cloud Run Job via the request body. However, I'm encountering an error when attempting to do so.

Here are the details:

I have defined a Cloud Scheduler job resource in Terraform with the following configuration:

 

resource "google_cloud_scheduler_job" "email_sending_scheduler" {
  paused           = false
  name             = "email_sending_scheduler_id_997"
  description      = "project to be sent with id 997"
  schedule         = "0 12 * * *"
  time_zone        = "Asia/Tokyo"

  http_target {
    http_method = "POST"
    uri         = var.job_execution_uri
    body        = base64encode("{\"campaign_id\":997}")
    headers = {
      "Content-Type" = "application/json"
      "User-Agent"   = "Google-Cloud-Scheduler"
    }
    oauth_token {
      scope                 = "https://www.googleapis.com/auth/cloud-platform"
      service_account_email = google_service_account.scheduler_sa.email
    }
  }
}

 

In the above configuration, the uri is set to var.job_execution_uri, which is an output value from another Terraform resource. The actual value of var.job_execution_uri is:

 

https://us-west1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/projectNumber/jobs/email-sending-job:run

 

The Terraform configuration for the Cloud Run Job itself is as follows:

 

resource "google_cloud_run_v2_job" "email_sending_job" {
  name     = "email-sending-job"
  project  = var.project
  location = var.region

  template {
    template {
      containers {
        image = "us-west1-docker.pkg.dev/${var.project}/app/email:latest"
      }
    }
  }
}

 

When I apply this Terraform configuration and execute the scheduler, I receive the following error:

 

{"@type":"type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished", "debugInfo":"URL_ERROR-ERROR_OTHER", "jobName":"projects/projectsName/locations/us-west1/jobs/email_sending_scheduler_id_997", "status":"INVALID_ARGUMENT", "targetType":"HTTP", "url":"https://us-west1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/projectNumber/jobs/email-sending-job:run"}

 

However, the following two patterns were successful.

1. Sending the request without authentication and using an external URI:

 

resource "google_cloud_scheduler_job" "email_sending_scheduler" {
    paused           = false
    name             = "email_sending_scheduler_id_997"
    description      = "project to be sent with id 997"
    schedule         = "0 12 * * *"
    time_zone        = "Asia/Tokyo"
  
    http_target {
      http_method = "POST"
      uri         = "https://example.com/"
      body        = base64encode("{\"campaign_id\":997}")
      headers = {
        "Content-Type" = "application/json"
        "User-Agent"   = "Google-Cloud-Scheduler"
      }
    }
  }

 

2. Sending an empty request body

 

resource "google_cloud_scheduler_job" "email_sending_scheduler" {
    paused           = false
    name             = "email_sending_scheduler_id_997"
    description      = "project to be sent with id 997"
    schedule         = "0 12 * * *"
    time_zone        = "Asia/Tokyo"

  http_target {
    http_method = "POST"
    uri         = var.job_execution_uri
    body        = base64encode("{}")
    headers = {
      "Content-Type" = "application/json"
      "User-Agent"   = "Google-Cloud-Scheduler"
    }
    oauth_token {
      scope                 = "https://www.googleapis.com/auth/cloud-platform"
      service_account_email = google_service_account.scheduler_sa.email
    }
  }
}

 

The above two patterns were successful in executing the Cloud Run Job (although the Cloud Run Job itself failed), while the initial pattern caused the INVALID_ARGUMENT error, preventing the Cloud Run Job from running at all.

I would appreciate any insights from the community on the correct way to structure and pass the request body data from Cloud Scheduler to the Cloud Run Job.

 

 

Solved Solved
0 1 122
1 ACCEPTED SOLUTION

The problem was solved using the following approach

  http_target {
    http_method = "POST"
    uri         = var.job_execution_uri
    body        = base64encode("{\"overrides\":{\"containerOverrides\":[{\"env\":[{\"name\":\"campaign_id\",\"value\":\"997\"}]}]}}")
    headers = {
      "Content-Type" = "application/json"
      "User-Agent"   = "Google-Cloud-Scheduler"
    }
    oauth_token {
      scope                 = "https://www.googleapis.com/auth/cloud-platform"
      service_account_email = google_service_account.scheduler_sa.email
    }
  }

View solution in original post

1 REPLY 1

The problem was solved using the following approach

  http_target {
    http_method = "POST"
    uri         = var.job_execution_uri
    body        = base64encode("{\"overrides\":{\"containerOverrides\":[{\"env\":[{\"name\":\"campaign_id\",\"value\":\"997\"}]}]}}")
    headers = {
      "Content-Type" = "application/json"
      "User-Agent"   = "Google-Cloud-Scheduler"
    }
    oauth_token {
      scope                 = "https://www.googleapis.com/auth/cloud-platform"
      service_account_email = google_service_account.scheduler_sa.email
    }
  }