Cloud Run 작업에 이벤트 게시

이 빠른 시작에서는 Google Cloud프로젝트에 Eventarc Advanced 버스 및 등록을 만들어 이벤트 메시지를 게시하고 수신하는 방법을 보여줍니다.

  • 버스는 중앙 라우터 역할을 하며 이벤트 소스에서 메시지를 수신하거나 제공업체에서 게시한 메시지를 수신합니다.

  • 등록은 버스에서 수신한 메시지를 처리 파이프라인을 통해 하나 이상의 대상으로 라우팅합니다.

이 빠른 시작에서는 다음을 수행합니다.

  1. Cloud Run 작업 배포

  2. Eventarc Advanced 버스를 만듭니다.

  3. Eventarc Advanced 등록을 만듭니다.

  4. 버스에 이벤트 메시지를 게시합니다.

  5. 로그에서 이벤트 데이터를 확인하고 Cloud Run 작업이 성공적으로 실행되었는지 확인합니다.

gcloud CLI를 사용하여 이 빠른 시작을 완료할 수 있습니다.

시작하기 전에

조직에서 정의한 보안 제약조건으로 인해 다음 단계를 완료하지 못할 수 있습니다. 문제 해결 정보는 제한된 Google Cloud 환경에서 애플리케이션 개발을 참조하세요.

  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 Run and Eventarc APIs:

    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 eventarc.googleapis.com eventarcpublishing.googleapis.com run.googleapis.com 
  8. Install the Google Cloud CLI.

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

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

    gcloud init
  11. 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.

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

  13. Enable the Cloud Run and Eventarc APIs:

    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 eventarc.googleapis.com eventarcpublishing.googleapis.com run.googleapis.com 
  14. gcloud 구성요소를 업데이트합니다.
    gcloud components update
  15. 계정을 사용하여 로그인합니다.
    gcloud auth login
  16. 이 빠른 시작에서 사용되는 구성 변수를 설정합니다.
    REGION=REGION

    REGION을 버스의 지원되는 위치(예: us-central1)로 바꿉니다.

  17. 프로젝트 생성자에게는 기본 소유자 역할(roles/owner)이 부여됩니다. 기본적으로 Identity and Access Management(IAM) 역할에는 대부분의 Google Cloud리소스에 대한 전체 액세스에 필요한 권한이 포함되며, 이 단계를 건너뛸 수 있습니다.

    프로젝트 생성자가 아니면 프로젝트에서 적합한 주 구성원에 대해 필수 권한을 부여해야 합니다. 예를 들어 주 구성원은 Google 계정(최종 사용자)이거나 서비스 계정(애플리케이션 및 컴퓨팅 워크로드)일 수 있습니다.

    필수 권한

    빠른 시작을 완료하는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 다음 IAM 역할을 부여해 달라고 요청하세요.

    역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.

    커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.

  18. Eventarc 고급에 Cloud Run 작업을 실행하는 데 필요한 권한을 부여하려면 관리자에게Google Cloud 프로젝트의 Cloud Run 호출자 (roles/run.invoker) IAM 역할을 서비스 계정에 부여해 달라고 요청하세요.
    1. 서비스 계정을 만듭니다. 테스트 목적으로 이 서비스 계정을 Eventarc Advanced 파이프라인에 연결하여 파이프라인의 ID를 나타냅니다.
      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
      SERVICE_ACCOUNT_NAME을 서비스 계정의 이름으로 바꿉니다.
    2. 서비스 계정에 roles/run.invoker IAM 역할을 부여합니다.
      gcloud projects add-iam-policy-binding PROJECT_ID \
          --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \
          --role=roles/run.invoker
  19. Cloud Run 작업 배포

    Cloud Run 작업을 이벤트 대상으로 배포합니다. 요청을 리슨하고 제공하는 Cloud Run 서비스와 달리 Cloud Run 작업은 태스크만 실행하고 완료 시 종료됩니다. 작업에서는 요청을 리슨하거나 제공하지 않습니다.

    Pub/Sub 주제, 워크플로 또는 다른 HTTP 엔드포인트와 같은 다른 이벤트 대상은 지원됩니다. 자세한 내용은 이벤트 제공자 및 대상을 참조하세요.

    gcloud run jobs deploy 명령어를 사용하여 샘플 컨테이너에서 작업을 배포합니다.

    gcloud run jobs deploy JOB_NAME --image us-docker.pkg.dev/cloudrun/container/job:latest \
        --region=$REGION

    JOB_NAME을 Cloud Run 작업의 고유한 이름(예: my-job)으로 바꿉니다.

    Eventarc Advanced 버스 만들기

    버스는 메시지 소스에서 수신하거나 제공자에서 게시한 이벤트 메시지를 수신하며 메시지 라우터 역할을 합니다.

    자세한 내용은 메시지 라우팅을 위한 버스 만들기를 참조하세요.

    gcloud eventarc message-buses create 명령어를 사용하여 프로젝트에서 Eventarc Advanced 버스를 만듭니다.

    gcloud eventarc message-buses create BUS_NAME \
        --location=$REGION

    BUS_NAME을 버스의 ID 또는 정규화된 이름으로 바꿉니다(예: my-bus).

    Eventarc Advanced 등록 만들기

    등록은 대상에 라우팅되는 메시지를 결정합니다. 또한 이벤트 메시지의 대상을 구성하는 데 사용되는 파이프라인을 지정합니다. 이 경우 대상은 Cloud Run 작업입니다.

    자세한 내용은 이벤트를 수신하도록 등록 만들기를 참조하세요.

    gcloud CLI를 사용할 때는 먼저 파이프라인을 만든 다음 등록을 만듭니다.

    1. gcloud eventarc pipelines create 명령어를 사용하여 파이프라인을 만듭니다.

      gcloud eventarc pipelines create PIPELINE_NAME \
          --destinations=http_endpoint_uri='https://REGION-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/PROJECT_NUMBER/jobs/JOB_NAME:run',http_endpoint_message_binding_template='{"body": ""}',oauth_token_authentication_service_account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
          --input-payload-format-json= \
          --location=$REGION

      다음을 바꿉니다.

      • PIPELINE_NAME: 파이프라인의 ID 또는 정규화된 이름입니다(예: my-pipeline).
      • PROJECT_NUMBER: Google Cloud 프로젝트 번호 다음 명령어를 실행하여 프로젝트 번호를 검색할 수 있습니다.
      gcloud projects describe PROJECT_ID --format='value(projectNumber)'

      다음에 유의하세요.

      • http_endpoint_message_binding_template 키는 이벤트를 Cloud Run Admin API에서 예상하는 형식으로 변환합니다. 메시지 바인딩을 정의할 때는 페이로드에 액세스할 입력 형식을 구성해야 합니다.
      • oauth_token_authentication_service_account 키는 서비스 계정 이메일을 지정합니다. 이 이메일은 일반적으로 *.googleapis.com에 호스팅된 Google API를 호출할 때만 사용되는 OAuth 토큰을 생성하는 데 사용됩니다.
    2. gcloud eventarc enrollments create 명령어를 사용하여 등록을 만듭니다.

      gcloud eventarc enrollments create ENROLLMENT_NAME \
          --cel-match=MATCH_EXPRESSION \
          --destination-pipeline=PIPELINE_NAME \
          --message-bus=BUS_NAME \
          --message-bus-project=PROJECT_ID \
          --location=$REGION

      다음을 바꿉니다.

      • ENROLLMENT_NAME: 등록의 ID 또는 정규화된 이름입니다(예: my-enrollment).
      • MATCH_EXPRESSION: CEL을 사용하는 이 등록의 일치 표현식입니다(예:

        "message.type == 'hello-world-type'"
        

    버스에 이벤트 메시지 게시

    메시지를 버스에 직접 게시하려면 gcloud eventarc message-buses publish 명령어를 사용하거나 Eventarc Publishing REST API에 요청을 전송하면 됩니다. 자세한 내용은 이벤트 직접 게시를 참조하세요.

    메시지는 이벤트 데이터를 일반적인 방식으로 설명하기 위한 사양인 CloudEvents 형식이어야 합니다. data 요소는 이벤트의 페이로드입니다. 이 필드에는 형식이 올바른 JSON이 모두 들어갈 수 있습니다. CloudEvents 컨텍스트 속성에 관한 자세한 내용은 이벤트 형식을 참조하세요.

    다음은 Eventarc Advanced 버스에 이벤트를 직접 게시하는 예시입니다.

    예 1

    gcloud CLI와 --event-data 및 기타 이벤트 속성 플래그를 사용하여 버스에 이벤트를 게시합니다.

    gcloud eventarc message-buses publish BUS_NAME \
        --event-data='{"key": "hello-world-data"}' \
        --event-id=hello-world-id-1234 \
        --event-source=hello-world-source \
        --event-type=hello-world-type \
        --event-attributes="datacontenttype=application/json" \
        --location=$REGION
    

    예 2

    gcloud CLI와 --json-message 플래그를 사용하여 버스에 JSON 메시지로 이벤트를 게시합니다.

    gcloud eventarc message-buses publish BUS_NAME \
        --location=$REGION \
        --json-message='{"id": "hello-world-id-1234", "type":
     "hello-world-type", "source":
     "hello-world-source", "specversion": "1.0", "data":
     {"key": "hello-world-data"}}'
    

    이벤트를 게시하면 '이벤트 게시 완료' 메시지가 표시됩니다.

    Cloud Run 로그에서 이벤트 데이터 확인

    Eventarc Advanced 버스에 이벤트를 게시한 후 Cloud Run 작업의 로그를 확인하여 이벤트가 예상대로 수신되었는지 확인합니다.

    1. gcloud logging read 명령어를 사용하여 로그 항목을 필터링하고 출력을 반환합니다.

      gcloud logging read 'textPayload: "hello-world-data"'
      
    2. 다음과 같은 로그 항목을 찾습니다.

      insertId: 670808e70002b5c6477709ae
      labels:
      instanceId: 007989f2a10a4a33c21024f2c8e06a9de65d9b4fdc2ee27697a50379b3fab2f975b9233dc357d50b06270829b9b479d5a1ee54a10fa2cb2d98c5f77a0895e2be0f9e6e4b20
      logName: projects/PROJECT_ID/logs/run.googleapis.com%2Fstderr
      receiveTimestamp: '2025-10-06T21:15:22.988264020Z'
      resource:
      labels:
      ...
      type: cloud_run_revision
      textPayload: "[2025-10-06 21:15:22,676] INFO in server: Body: b'{\"key\": \"hello-world-data\"\
        }'"
      timestamp: '2025-10-06T21:15:22.675530Z'
      
    3. gcloud run jobs describe 명령어를 사용하여 Cloud Run 작업이 성공적으로 실행되었는지 확인합니다.

      gcloud run jobs describe JOB_NAME \
          --region=$REGION
      

      다음과 비슷한 출력이 표시됩니다.

      ✔ Job JOB_NAME in region us-central1
      Executed 1 time
      ...
      

    Eventarc Advanced 버스 및 등록을 만들고, 버스에 이벤트 메시지를 게시하고, 이벤트 수신자의 로그에서 예상 결과를 확인했습니다.

    삭제

    이 빠른 시작을 마친 후 요금이 계속 청구되지 않도록 하려면 만든 리소스를 삭제하세요.

    1. Cloud Run 작업을 삭제합니다.

    2. Eventarc Advanced 리소스를 삭제합니다.

      1. 등록을 삭제합니다.

      2. 파이프라인을 삭제합니다.

      3. 버스를 삭제합니다.

    또는 Google Cloud 프로젝트를 삭제하여 비용 청구를 방지할 수 있습니다. Google Cloud 프로젝트를 삭제하면 해당 프로젝트 내에서 사용되는 모든 리소스에 대한 청구가 중단됩니다.

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

    다음 단계