Cloud Tasks 큐에 HTTP 대상 태스크 추가

이 빠른 시작에서는 Cloud Tasks API를 사용하여 Cloud Tasks 큐에 HTTP 대상 태스크를 추가하는 방법을 설명합니다.

시작하기 전에

  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. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  4. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  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 (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 Tasks API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudtasks.googleapis.com
  8. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    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. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudtasks.admin, roles/logging.viewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: Your project ID.
    • USER_IDENTIFIER: The identifier for your user account. For example, myemail@example.com.
    • ROLE: The IAM role that you grant to your user account.
  10. Install the Google Cloud CLI.

  11. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  12. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  13. 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 (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.

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

  15. Enable the Cloud Tasks API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudtasks.googleapis.com
  16. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    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.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudtasks.admin, roles/logging.viewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: Your project ID.
    • USER_IDENTIFIER: The identifier for your user account. For example, myemail@example.com.
    • ROLE: The IAM role that you grant to your user account.
  18. Cloud Tasks 큐 만들기

    gcloud tasks queues create 명령어를 사용하여 큐를 만듭니다.

    1. 터미널에서 모든 작업을 로깅하는 대기열을 만듭니다.

      gcloud tasks queues create QUEUE_NAME \
          --log-sampling-ratio=1.0 \
          --location=REGION
      

      다음을 바꿉니다.

      • QUEUE_NAME: Cloud Tasks 큐의 이름
      • REGION: 서비스를 배포한 지원되는 리전 또는 앱
    2. 대기열이 초기화될 때까지 기다린 다음 성공적으로 만들어졌는지 확인합니다.

      gcloud tasks queues describe QUEUE_NAME \
          --location=REGION
      

      출력은 다음과 비슷하게 표시됩니다.

       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
      

    Cloud Tasks 큐에 태스크 추가

    gcloud tasks create-http-task 명령어를 사용하여 HTTP 엔드포인트를 대상으로 하는 태스크를 만들고 큐에 태스크를 추가합니다.

    1. 태스크를 만들고 만든 대기열에 추가한 다음 태스크를 HTTP 엔드포인트로 전달합니다.

      gcloud tasks create-http-task \
          --queue=QUEUE_NAME \
          --url=URL_PATH \
          --method=GET \
          --location=REGION \
          --project=PROJECT_ID
      

      URL_PATH를 요청이 전송될 전체 URL 경로로 바꿉니다. 예를 들면 https://www.google.com입니다.

      경로는 http:// 또는 https://로 시작해야 합니다.

    2. 로그를 읽어 태스크가 성공적으로 실행되었는지 확인합니다.

      gcloud logging read --limit=3
      

      로그는 다음과 비슷하게 표시됩니다.

      jsonPayload:
      '@type': type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog
      task: projects/PROJECT_ID/locations/REGION/queues/QUEUE_NAME/tasks/TASK_ID
      taskCreationLog:
         scheduleTime: '2024-07-04T19:00:27.801837Z'
         status: OK
         targetAddress: GET https://www.google.com/
         targetType: HTTP
      

    삭제

    이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 Google Cloud 프로젝트를 삭제합니다.

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

    또는 Cloud Tasks 큐를 삭제할 수 있습니다.

    gcloud tasks queues delete QUEUE_NAME \
        --location=REGION
    

    다음 단계