Add an App Engine task to a Cloud Tasks queue

This quickstart shows you how to add an App Engine task to a Cloud Tasks queue using the Cloud Tasks API.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get 300ドル in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloudinit
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Resource Manager and Cloud Tasks API:

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    gcloudservicesenablecloudresourcemanager.googleapis.comtasks.googleapis.com
  8. If you're using a local shell, then create local authentication credentials for your user account:

    gcloudauthapplication-defaultlogin

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

    gcloudinit
  12. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. Verify that billing is enabled for your Google Cloud project.

  14. Enable the Cloud Resource Manager and Cloud Tasks API:

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    gcloudservicesenablecloudresourcemanager.googleapis.comtasks.googleapis.com
  15. If you're using a local shell, then create local authentication credentials for your user account:

    gcloudauthapplication-defaultlogin

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  16. The App Engine default service account is automatically created when you use App Engine. You can use this service account when trying out this quickstart. However, depending on your organization policy configuration, the default service account might not automatically be granted the Editor role on your project. If that is the case, you must grant the service account the following roles:
    1. Artifact Registry Administrator (roles/artifactregistry.admin)
    2. Artifact Registry Create-on-Push Writer (roles/artifactregistry.createOnPushWriter)
    3. Compute Admin (roles/compute.admin)
    4. Logs Writer (roles/logging.logWriter)
    5. Storage Object Viewer (roles/storage.objectViewer)

Add an App Engine application

When you target an App Engine task, and before you can deploy an app to the App Engine standard environment, you must add an App Engine application to your project.

  1. In the Google Cloud console, go to the App Engine page.

    Go to App Engine

  2. In the Welcome to App Engine dialog, do one of the following:

    • If you have already created an App Engine application and there is a Your App Engine application has been created message displayed, you can then skip the remaining steps in this section and proceed with the steps in the Install and deploy the sample section.

      or

    • If you haven't created an App Engine application yet, then click Create application and continue with the remaining steps in this section.

  3. Select a region for your application and make a note of it.

    Note that europe-west and us-central are called, respectively, europe-west1 and us-central1 in Cloud Tasks commands.

  4. Don't select a service account; the default App Engine service account is used.

  5. Click Next.

    The application is configured and created. This can take a couple of minutes.

  6. Don't download the Cloud SDK; instead, click I'll do this later.

    You should see a Your App Engine application has been created message.

Install and deploy the sample

The Node.js sample used in this quickstart consists of two files: createTask.js is run locally as a command-line tool to create and add tasks to the Tasks queue; server.js is deployed on App Engine as a worker service to process the task.

  1. In your terminal, clone the sample application repository to your local machine.

    gitclonehttps://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    
  2. Navigate to the directory that contains the sample code.

    cdnodejs-docs-samples/cloud-tasks/snippets
    
  3. Install all dependencies using a Node.js package manager.

    You can use NPM:

    npminstall
    

    Or, you can use Yarn:

    yarninstall
    
  4. Deploy the worker service (server.js) to the App Engine standard environment.

    gcloudappdeployapp.yaml
    
  5. Make sure that the app containing the service is running.

    gcloudappbrowse
    
  6. In your browser, go to the provided link. For example:

    https://PROJECT_ID.uc.r.appspot.com/
    

    You should see Hello, World! displayed.

Create a Cloud Tasks queue

Use the gcloud tasks queues create command to create your queue in the environment you've prepared.

  1. In your terminal, create a queue that logs all operations.

    gcloudtasksqueuescreateQUEUE_NAME\
    --log-sampling-ratio=1.0\
    --location=REGION
    

    Replace the following:

    • QUEUE_NAME: a name for your Cloud Tasks queue
    • REGION: the region you deployed your app in
  2. Wait for the queue to initialize and then verify that it was created successfully.

    gcloudtasksqueuesdescribeQUEUE_NAME\
    --location=REGION
    

    The output should be similar to the following:

     name: projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_NAME
     rateLimits:
     maxBurstSize: 100
     maxConcurrentDispatches: 1000
     maxDispatchesPerSecond: 500.0
     retryConfig:
     maxAttempts: 100
     maxBackoff: 3600s
     maxDoublings: 16
     minBackoff: 0.100s
     state: RUNNING
    

Add a task to the Cloud Tasks queue

Create a task, add it to the queue you created, and deliver that task to the worker service.

  1. Set the following environment variables. The client uses this information to create the request.

    exportPROJECT_ID=PROJECT_ID
    exportLOCATION_ID=REGION
    exportQUEUE_ID=QUEUE_NAME
    
  2. Create a task with a payload of hello and add that task to your queue. The payload can be any data from the request that the worker service needs to process the task.

    nodecreateTask.js$PROJECT_ID$QUEUE_ID$LOCATION_IDhello
    
  3. Verify that the task was executed by displaying the logs of the worker service.

    gcloudapplogsread
    

    The logs should look similar to the following:

    2024年06月20日 15:00:00 default[20240620t143852] "POST /log_payload HTTP/1.1" 200
    2024年06月20日 15:00:00 default[20240620t143852] App listening on port 8081
    2024年06月20日 15:00:00 default[20240620t143852] Press Ctrl+C to quit.
    2024年06月20日 15:00:00 default[20240620t143852] Received task with payload: hello
    

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

Alternatively, you can delete the resources you created:

  1. Delete the Cloud Tasks queue:

    gcloudtasksqueuesdeleteQUEUE_NAME\
    --location=REGION
    
  2. Disable the App Engine application.

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年08月26日 UTC.