You can use Cloud Tasks to securely enqueue a task to be asynchronously processed by a Cloud Run service. Typical use cases include:
- Preserving requests through unexpected production incidents
- Smoothing traffic spikes by delaying work that is not user-facing
- Speeding user response time by delegating slow background operations to be handled by another service, such as database updates or batch processing
- Limiting the call rate to backing services like databases and third-party APIs
This page shows how to enqueue tasks that are securely pushed via the HTTPS protocol to a private Cloud Run service. It describes required behavior for the private Cloud Run service, required service account permissions, task queue creation, and task creation.
Before you start
Enable the Cloud Tasks API on the project you are using.
Deploying a Cloud Run service to handle tasks
To deploy a service that accepts tasks sent to the task queue,
deploy the service in the same way as any other
Cloud Run service. The Cloud Run service must return an
HTTP 200
code to confirm proper processing of the task.
Tasks will be pushed to this Cloud Run service as HTTPS requests by Cloud Tasks.
Creating a task queue
To create a task queue, use the command
gcloud tasks queues create QUEUE-ID
replacing QUEUE-ID with the name you want to give to your task queue:
it must be unique in your project. If you are prompted to create an App Engine
app in your project, respond y
to create it. Cloud Tasks uses
this for the queue: make sure you choose the same location as you are using for
your Cloud Run service.
The default task queue configuration should work in most cases. However, you can optionally set different rate limits and retry parameters if you want.
Creating a service account to associate with the tasks
You must create a service account that will be associated with the enqueued tasks. This service account must have the Cloud Run Invoker IAM role to allow the task queue to push tasks to the Cloud Run (fully managed) service. .
Console
Visit the Create service account key page in the Cloud Console.
From the Service account list, select New service account.
In the Service account name field, enter the name you want to use for the service account.
Click Create.
Copy the service account email to use in the following steps.
Click Continue if prompted to specify permissions.
Visit the Cloud Run Services page in the Cloud Console.
Select your service in the displayed list.
If necessary, click the Show Info Panel/Hide Info Panel toggle in the far right of the page to show information.
Locate the Permissions tab, and in that tab, click Add Member.
Paste your service account email into the New members field.
From the Role dropdown menu, select Cloud Run > Cloud Run Invoker.
Click Save.
Command line
Create the service account:
gcloud iam service-accounts create SERVICE-ACCOUNT_NAME \ --display-name "DISPLAYED-SERVICE-ACCOUNT_NAME"
Replace
- SERVICE-ACCOUNT_NAME with a lower case name unique within
your Google Cloud project, for example
my-invoker-service-account-name
. - DISPLAYED-SERVICE-ACCOUNT-NAME with the name you want to
display for this service account, for example, in the console, for example,
My Invoker Service Account
.
- SERVICE-ACCOUNT_NAME with a lower case name unique within
your Google Cloud project, for example
For Cloud Run, give your service account permission to invoke your service:
gcloud run services add-iam-policy-binding SERVICE \ --member=serviceAccount:SERVICE-ACCOUNT_NAME@PROJECT-ID.iam.gserviceaccount.com \ --role=roles/run.invoker
Replace
- SERVICE with the name of the service you want to be invoked by Cloud Tasks.
- SERVICE-ACCOUNT_NAME with the name of the service account.
- PROJECT-ID with your Google Cloud project ID.
Creating HTTP tasks with authentication tokens
When you create a task to send to the task queue, you specify the project, the location, queue name, the email of the previously created service account to associate with tasks, the URL of the private Cloud Run service that will run the task, and any other data you need to send.
Refer to the
Cloud Tasks API documentation for details on the
task request body.
Note that requests that contain data payloads must use the HTTP PUT
or
POST
method.
The code that enqueues the tasks must have the necessary IAM permissions to do so, such as the Cloud Tasks Enqueuer role. Your code will have the necessary IAM permissions if you use the default service account on Cloud Run (fully managed).
The following examples create task requests that also include the creation of a header token. OIDC tokens are used in the examples. To use an OAuth token, replace the OIDC parameter with the language appropriate OAuth parameter in constructing the request.
Python
Note the requirements.txt
file:
Java
Note the pom.xml
file:
Go
Node.js
Note the package.json
file:
What's next
- Logging and viewing logs
- Monitoring health and performance
- Triggering from Pub/Sub
- Invoking with HTTPS
- Running services on a schedule