Calling or invoking a Google Cloud service such as Cloud Run functions
or Cloud Run from Workflows is done through an HTTP
request. The most common HTTP request methods have a call shortcut (such as
[http.get](https://docs.cloud.google.com/workflows/docs/reference/stdlib/http/get.md.txt)
and [http.post](https://docs.cloud.google.com/workflows/docs/reference/stdlib/http/post.md.txt)), but you can make
any type of HTTP request by setting the `call` field to `http.request` and
specifying the type of request using the `method` field. For
more information, see [Make an HTTP request](https://docs.cloud.google.com/workflows/docs/http-requests.md.txt).

> [!NOTE]
> **Note:** Invoking a Google Cloud service through an HTTP request shouldn't be confused with using Workflows [connectors](https://docs.cloud.google.com/workflows/docs/connectors.md.txt) to perform API operations with other Google Cloud products.

#### To send authenticated requests:

- Your workflow must be associated with a service account that has been granted
  one or more Identity and Access Management (IAM) roles containing the required
  permissions.

- You must explicitly add authentication information to your workflow
  definition. By default, HTTP requests don't contain identity or access tokens
  for security reasons.

For more information, see
[Grant a workflow permission to access Google Cloud resources](https://docs.cloud.google.com/workflows/docs/authentication.md.txt).

## When to call a service


How do you know when to create steps in YAML or JSON using the Workflows
syntax or when to create a service---for example, a Cloud Run service
or a Cloud Run function---to do the work instead?

Use Workflows to call services from the workflow itself and
handle the results, and to execute simple tasks like making an HTTP call.
Workflows can invoke services, parse responses, and construct
inputs for other connected services. Calling a service allows you to avoid the
complications of extra invocations, additional dependencies, and services
calling services.

Create services to do any work that is too complex for Workflows;
for example, implementing reusable business logic, complex computations, or
transformations that are not supported by Workflows expressions and its
standard library. A complicated case is typically easier to implement in code,
instead of using YAML or JSON and the Workflows syntax.

### Request timeout and payload limits

When calling a Cloud Run service or
Cloud Run function over HTTP, remember that synchronous HTTP
requests (`http.get`, `http.post`, `http.request`) in Workflows
default to a 5-minute (300 seconds) timeout, support a maximum timeout of 30
minutes (1,800 seconds), and allow a 2 MB maximum response size (subject to the
512 KB variable memory limit if stored in variables).

Although Cloud Run services and Cloud Run functions
(2nd gen) support request timeouts up to 60 minutes, any synchronous `http.*`
step in Workflows times out with a `TimeoutError` after its
configured timeout (up to 30 minutes). For workloads running longer than 30
minutes or returning large payloads, use
[Cloud Run jobs](https://docs.cloud.google.com/workflows/docs/calling-run-functions.md.txt#cloud-run-jobs) or
[callback endpoints](https://docs.cloud.google.com/workflows/docs/creating-callback-endpoints.md.txt). For a
comparison of limits and recommended patterns, see
[Align timeouts and payload sizes across services](https://docs.cloud.google.com/workflows/docs/best-practice.md.txt#timeouts-and-payloads).

## Invoke services that are restricted to internal ingress

Workflows can invoke Cloud Run functions
or Cloud Run services in the same Google Cloud project
or VPC Service Controls perimeter that have ingress restricted to internal traffic.
With this configuration, your services are unreachable from the internet but can
be reached from Workflows.

To apply these restrictions, you must adjust the ingress settings of your
service or function. Note that the Cloud Run service must be
reached on its `run.app` URL and not at a custom domain. For more information,
see [Restrict network ingress](https://docs.cloud.google.com/run/docs/securing/ingress.md.txt) (for
Cloud Run and Cloud Run functions 2nd gen) and
[Configure network settings (1st gen)](https://docs.cloud.google.com/functions/1stgendocs/networking/network-settings.md.txt)
(for Cloud Run functions 1st gen). No other changes are needed to
your workflow.

## Use a service account with the required permissions

When making requests to other Google Cloud services, your workflow must
be associated with a service account that has the correct permissions to access
the requested resources. To learn what service account is associated with an
existing workflow, see [Verify a workflow's associated service account](https://docs.cloud.google.com/workflows/docs/authentication.md.txt#verify-service-account).

When setting up a service account, you associate the requesting identity with the
resource you want to give it access to---you make the requesting identity a
**principal** of the resource---and then assign it the appropriate [role](https://docs.cloud.google.com/iam/docs/understanding-roles).
The role defines what permissions the identity has in the context of the resource.

For example, to configure a receiving Cloud Run function (1st
gen) to accept requests from a specific calling function or service, you need to
add the caller's service account as a principal on the receiving function and
grant that principal the Cloud Functions Invoker
(`roles/cloudfunctions.invoker`) role. Similarly, to set up a service account
for Cloud Run services or Cloud Run functions
(2nd gen), you grant that service account the Cloud Run Invoker
(`roles/run.invoker`) role. To learn more, see
[Access control with IAM](https://docs.cloud.google.com/run/docs/securing/managing-access.md.txt)
or the [Cloud Run authentication overview](https://docs.cloud.google.com/run/docs/authenticating/overview.md.txt).

### Invoke Cloud Run functions

In Cloud Run functions, invocation permissions are available by
managing the underlying Cloud Run service. If your workflow is invoking
a Cloud Run functions service, you don't need to grant the caller's
service account the [Cloud Functions Invoker](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudfunctions.md.txt#cloudfunctions.invoker)
(`roles/cloudfunctions.invoker`) IAM role. Instead, you must
grant the
[Cloud Run Invoker](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.invoker)
(`roles/run.invoker`) IAM role.

For more information, see
[Compare Cloud Run functions](https://docs.cloud.google.com/run/docs/functions/comparison.md.txt).

## Add authentication information to your workflow

> [!NOTE]
> **Note:** This authentication method is restricted to HTTPS endpoints.

When making requests to Cloud Run functions or Cloud Run,
use OIDC to authenticate.

To make an HTTP request using OIDC, add an `auth` section to the `args` section
of your workflow's definition, after you specify the URL. In this example, a
request is sent to invoke a Cloud Run function:

<br />

### YAML

```yaml
  - step_A:
      call: http.get
      args:
          url: https://us-central1-project.cloudfunctions.net/functionA
          query:
              firstNumber: 4
              secondNumber: 6
              operation: sum
          auth:
              type: OIDC
              audience: OIDC_AUDIENCE
    
```

### JSON

```json
    [
      {
        "step_A": {
          "call": "http.get",
          "args": {
            "url": "https://us-central1-project.cloudfunctions.net/functionA",
            "query": {
              "firstNumber": 4,
              "secondNumber": 6,
              "operation": "sum"
            },
            "auth": {
              "type": "OIDC",
              "audience": "OIDC_AUDIENCE"
            }
          }
        }
      }
    ]
      
```
The optional `audience` parameter specifies the OIDC audience (`OIDC_AUDIENCE`) for the token. By default, it's set to the same value as `url`; however, if `url` includes additional path segments beyond the service's or function's root URL, set `audience` to the root URL (for example, `https://us-central1-project.cloudfunctions.net/functionA`).

<br />

## Specify the media type for the response data

If the `Content-Type` header for the response specifies an
`application/json` media type, the JSON response that is stored in a
variable is automatically converted to a map that can be accessed.

If necessary, modify the API that is being called to specify an
`application/json` media type for the `Content-Type`
response header. Otherwise, you can use the
[`json.decode`](https://docs.cloud.google.com/workflows/docs/reference/stdlib/json/decode.md.txt)
and [`text.encode`](https://docs.cloud.google.com/workflows/docs/reference/stdlib/text/encode.md.txt) functions to
convert the response body into a map. For example:

    json.decode(text.encode(RESPONSE_FROM_API))

For more information, see
[Access HTTP response data saved in a variable](https://docs.cloud.google.com/workflows/docs/http-requests.md.txt#access-data).

## Execute Cloud Run jobs

Unlike Cloud Run services,
[Cloud Run jobs](https://docs.cloud.google.com/run/docs/create-jobs.md.txt) don't listen for or
serve HTTP requests. To execute Cloud Run jobs from a workflow,
use the
[Cloud Run Admin API connector](https://docs.cloud.google.com/workflows/docs/reference/googleapis/run/Overview.md.txt).
If a job takes longer than 30 minutes to complete, configure both the job's
[task timeout](https://docs.cloud.google.com/run/docs/configuring/task-timeout.md.txt) (10 minutes by default,
configurable up to 168 hours, or 7 days) and `connector_params.timeout` in the
connector call (which defaults to 1,800 seconds and can be set up to
31,536,000 seconds, or 1 year).

For an end-to-end example of executing a Cloud Run job that
processes data passed as environment variables to the job, see
[Execute a Cloud Run job using Workflows](https://docs.cloud.google.com/workflows/docs/tutorials/execute-cloud-run-jobs.md.txt).

For an end-to-end example of executing a Cloud Run job that
processes data stored in a Cloud Storage bucket allowing you to
encrypt the data using customer-managed encryption keys (CMEK), see
[Execute a Cloud Run job that processes event data in Cloud Storage](https://docs.cloud.google.com/workflows/docs/tutorials/execute-cloud-run-jobs-stored-events.md.txt).

## What's next

- [Invoke with an HTTPS Request](https://docs.cloud.google.com/run/docs/triggering/https-request.md.txt)
- [Use Workflows with Cloud Run and Cloud Run functions tutorial](https://docs.cloud.google.com/workflows/docs/tutorials/run/cloud-run.md.txt)