이 튜토리얼에서는 Cloud Scheduler에서 HTTP 엔드포인트를 타겟팅하여 HTTP Cloud Run 함수를 예약하는 방법을 보여줍니다. 함수가 URL 엔드포인트를 포함하고 웹훅과 같은 HTTP 요청에 응답하도록 하려면 HTTP 함수를 사용합니다.
또는 CloudEvents 함수를 만든 경우 Eventarc 이벤트 트리거를 사용하여 프로젝트 내 이벤트에 응답하도록 함수를 예약합니다. Eventarc는 Pub/Sub 주제의 메시지, Cloud Storage 버킷의 변경사항 등을 트리거할 수 있습니다. 자세한 내용은 Cloud Run 함수 유형을 참고하세요.
Cloud Run 함수 실행 예약은 Cloud Scheduler의 일반적인 사용 사례입니다. 이 튜토리얼에서는 다음 단계를 진행합니다.
비용
이 문서에서는 비용이 청구될 수 있는 Google Cloud구성요소( )를 사용합니다.
프로젝트 사용량을 기준으로 예상 비용을 산출하려면 가격 계산기를 사용합니다.
시작하기 전에
- 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.
-
In the Google Cloud console, on the project selector page, select or create 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click Add another role and add each additional role.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
In the Google Cloud console, on the project selector page, select or create 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click Add another role and add each additional role.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
Cloud Run 소스 개발자 (
roles/run.sourceDeveloper) -
Cloud Scheduler 관리자 (
roles/cloudscheduler.admin) -
로그 뷰 접근자 (
roles/logging.viewAccessor) -
프로젝트 IAM 관리자(
roles/resourcemanager.projectIamAdmin) -
서비스 계정 관리자(
roles/iam.serviceAccountAdmin) -
서비스 계정 사용자(
roles/iam.serviceAccountUser) -
Google Cloud 콘솔에서 IAM 페이지로 이동합니다.
IAM으로 이동 - 프로젝트를 선택합니다.
- 액세스 권한 부여를 클릭합니다.
-
새 주 구성원 필드에 사용자 식별자를 입력합니다. 일반적으로 Cloud Run 서비스를 배포하는 데 사용되는 이메일 주소입니다.
- 역할 선택 목록에서 역할을 선택합니다.
- 역할을 추가로 부여하려면 다른 역할 추가를 클릭하고 각 역할을 추가합니다.
- 저장을 클릭합니다.
- PROJECT_NUMBER를 Google Cloud 프로젝트 번호로 바꿉니다.
- PROJECT_ID를 Google Cloud 프로젝트 ID로 바꿉니다.
- PRINCIPAL을 바인딩을 추가할 계정으로 바꿉니다. 일반적으로 Cloud Run 서비스를 배포하는 데 사용되는 이메일 주소입니다.
- ROLE을 배포자 계정에 추가할 역할로 바꿉니다.
Google Cloud 콘솔에서 Cloud Run 페이지로 이동합니다.
함수 작성을 클릭합니다.
구성 섹션에서 다음을 수행합니다.
- 서비스 이름 필드에 함수 이름을 입력합니다.
- 리전 목록에서 리전을 선택합니다.
- 기본 런타임 언어를 수정하지 마세요.
인증에서 인증 필요를 선택합니다.
프로젝트에서 인증되지 않은 호출을 제한하는 도메인 제한 조직 정책이 적용되는 경우 비공개 서비스 테스트의 설명대로 배포된 서비스에 액세스해야 합니다.
자세한 내용은 호출 인증을 참고하세요.
다른 기본값을 그대로 두고 만들기를 클릭합니다.
함수의 HTTP URL을 복사합니다.
Google Cloud 콘솔에서 Cloud Scheduler 페이지로 이동합니다.
작업 만들기를 클릭합니다.
작업 이름을 입력합니다.
리전 목록에서 리전을 선택합니다.
unix-cron 형식을 사용하여 작업의 빈도를 지정합니다.
30 16 * * 7자세한 내용은 크론 작업 형식 및 시간대를 참조하세요.
시간대 목록에서 시간대를 선택합니다.
계속을 클릭합니다.
대상 유형 목록에서 HTTP를 선택합니다.
URL 필드에 이전에 복사한 함수의 HTTP URL을 입력합니다.
인증 헤더 목록에서 OIDC 토큰 추가를 선택합니다.
서비스 계정 목록에서 이전에 만든 서비스 계정을 선택합니다.
Audience(대상) 필드에 이전에 복사한 함수의 HTTP URL을 입력합니다.
만들기를 클릭합니다.
Google Cloud 콘솔에서 Cloud Scheduler 페이지로 이동합니다.
자신이 만든 작업의 체크박스를 선택하고 강제 실행을 클릭합니다.
처음 호출될 때는 프로젝트에서 만든 첫 번째 작업을 구성하고 실행하는 데 몇 분 정도 걸릴 수 있습니다.
작업이 실행되면 최종 실행 상태가
Success로 표시됩니다.Google Cloud 콘솔에서 Cloud Run 페이지로 이동합니다.
서비스 필터링 > 배포 유형 > 함수를 선택합니다. 이렇게 하면 서비스 목록이 필터링되어 함수만 표시됩니다.
함수 이름을 클릭합니다.
서비스 세부정보 페이지가 열리고 서비스 측정항목이 표시됩니다.
로그 탭을 클릭합니다.
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME과 비슷한 로그 항목이 표시됩니다.- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Google Cloud 콘솔에서 Cloud Scheduler 페이지로 이동합니다.
작업 옆의 체크박스를 선택합니다.
삭제를 클릭하고 삭제되었는지 확인합니다.
Google Cloud 콘솔에서 Cloud Run 페이지로 이동합니다.
서비스 옆에 있는 체크박스를 선택합니다.
삭제를 클릭하고 삭제되었는지 확인합니다.
Google Cloud 콘솔에서 서비스 계정 페이지로 이동합니다.
자신이 만든 서비스 계정 옆에 있는 체크박스를 선택합니다.
삭제를 클릭하고 삭제되었는지 확인합니다.
- Cloud Scheduler를 사용하여 일정에 따라 Cloud Run 트리거
- CloudEvents 함수를 작성하여 프로젝트 내 이벤트에 응답하도록 함수를 예약합니다.
Cloud Run 함수에서는 기본 Cloud Run 서비스를 관리하여 호출 권한을 사용할 수 있으므로 Cloud Run 호출자 역할을 부여해야 합니다.
필요한 역할
튜토리얼을 완료하는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 다음 IAM 역할을 부여해 달라고 요청하세요.
역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.
커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.
역할 부여
콘솔
gcloud
프로젝트에서 계정에 필요한 IAM 역할을 부여하려면 다음 안내를 따르세요.
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL \ --role=ROLE
다음과 같이 바꿉니다.
HTTP Cloud Run 함수 만들기
Cloud Scheduler에 의해 트리거되는 HTTP 함수를 만듭니다.
Terraform을 사용하여 Cloud Run 함수를 배포하는 방법을 비롯한 자세한 내용은 Cloud Run 함수 배포를 참고하세요.
Cloud Scheduler 작업 만들기
HTTP를 사용하여 작업을 트리거하는 Cloud Scheduler 작업을 만듭니다.
일요일 오후 4시 30분에 Cloud Run 함수를 실행하는 크론 작업을 만들었습니다.
Cloud Scheduler 작업 실행
이제 만든 작업을 실행할 수 있습니다.
Cloud Run 함수에서 결과 확인
Cloud Run 함수가 크론 작업에 의해 성공적으로 트리거되고 실행되는지 확인할 수 있습니다.
삭제
이 튜토리얼에서 사용된 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 프로젝트를 삭제하거나 프로젝트를 유지하고 개별 리소스를 삭제하세요.