Menjadwalkan fungsi Cloud Run HTTP


Tutorial ini menunjukkan cara menjadwalkan fungsi Cloud Run HTTP dengan menargetkan endpoint HTTP di Cloud Scheduler. Gunakan fungsi HTTP jika Anda ingin fungsi memiliki endpoint URL dan merespons permintaan HTTP, seperti untuk webhook.

Atau, jika Anda membuat fungsi CloudEvents, gunakan pemicu peristiwa Eventarc untuk menjadwalkan fungsi Anda agar merespons peristiwa dalam project Anda. Eventarc dapat memicu pesan pada topik Pub/Sub, perubahan dalam bucket Cloud Storage, dan lainnya. Untuk mengetahui informasi selengkapnya, lihat Jenis fungsi Cloud Run.

Menjadwalkan eksekusi fungsi Cloud Run adalah kasus penggunaan umum untuk Cloud Scheduler. Dalam tutorial ini, Anda akan:

  1. Buat fungsi Cloud Run HTTP sederhana.
  2. Buat tugas Cloud Scheduler.
  3. Jalankan tugas Cloud Scheduler.
  4. Pastikan tugas Cloud Scheduler memicu fungsi Cloud Run Anda.

Biaya

Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga. Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Sebelum memulai

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

Perhatikan bahwa Anda harus memberikan peran Cloud Run Invoker karena di fungsi Cloud Run, izin pemanggilan tersedia dengan mengelola layanan Cloud Run yang mendasarinya.

Membuat fungsi Cloud Run HTTP

Membuat fungsi HTTP yang dipicu oleh Cloud Scheduler.

  1. Pada konsol Google Cloud, buka halaman Cloud Run.

    Buka Cloud Run

  2. Klik Tulis fungsi.

  3. Di bagian Configure, lakukan hal berikut:

    1. Di kolom Nama layanan, masukkan nama untuk fungsi Anda.
    2. Di daftar Region, pilih region.
    3. Jangan ubah bahasa runtime default.
    4. Untuk autentikasi, pilih Wajibkan autentikasi.

      Jika Anda dikenai kebijakan organisasi pembatasan domain yang membatasi pemanggilan yang tidak terautentikasi untuk project, Anda perlu mengakses layanan yang di-deploy seperti yang dijelaskan di bagian Menguji layanan pribadi.

      Untuk informasi selengkapnya, lihat Mengautentikasi untuk pemanggilan.

  4. Setujui setelan default lainnya, lalu klik Create.

  5. Salin URL HTTP untuk fungsi.

Buat tugas Cloud Scheduler

Buat tugas Cloud Scheduler yang memicu tindakan menggunakan HTTP.

  1. Di konsol Google Cloud, buka halaman Cloud Scheduler.

    Buka Cloud Scheduler

  2. Klik Create job.

  3. Masukkan nama untuk tugas Anda.

  4. Di daftar Region, pilih region.

  5. Tentukan frekuensi untuk tugas Anda, menggunakan format unix-cron:

    30 16 * * 7
    

    Untuk mengetahui informasi selengkapnya, lihat Format tugas Cron dan zona waktu.

  6. Di daftar Zona waktu, pilih zona waktu.

  7. Klik Lanjutkan.

  8. Dalam daftar Jenis target, pilih HTTP.

  9. Di kolom URL, masukkan URL HTTP fungsi yang sebelumnya Anda salin.

  10. Dalam daftar Header autentikasi, pilih Tambahkan token OIDC.

  11. Dalam daftar Service account, pilih akun layanan yang Anda buat sebelumnya.

  12. Di kolom Audiens, masukkan URL HTTP fungsi yang sebelumnya Anda salin.

  13. Klik Buat.

Anda telah membuat tugas cron yang menjalankan fungsi Cloud Run pada pukul 16.30 pada hari Minggu.

Menjalankan tugas Cloud Scheduler

Sekarang Anda dapat menjalankan tugas yang dibuat.

  1. Di konsol Google Cloud, buka halaman Cloud Scheduler.

    Buka Cloud Scheduler

  2. Centang kotak untuk tugas yang Anda buat, lalu klik Paksa jalankan.

    Saat pertama kali dipanggil, tugas pertama yang dibuat dalam project dapat memerlukan waktu beberapa menit untuk dikonfigurasi dan dijalankan.

  3. Setelah tugas berjalan, Status eksekusi terakhir akan menunjukkan Success.

Memverifikasi hasil di fungsi Cloud Run

Anda dapat memverifikasi bahwa fungsi Cloud Run berhasil dipicu dan dijalankan oleh tugas cron.

  1. Pada konsol Google Cloud, buka halaman Cloud Run.

    Buka Cloud Run

  2. Pilih Filter services > Deployment type > Function. Tindakan ini akan memfilter daftar layanan agar hanya menampilkan fungsi.

  3. Klik nama fungsi.

    Halaman Detail layanan akan terbuka dan menampilkan metrik layanan.

  4. Klik tab Logs.

    Anda akan melihat entri log yang mirip dengan POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME

Pembersihan

Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

Hapus project

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Menghapus resource tutorial

  1. Di konsol Google Cloud, buka halaman Cloud Scheduler.

    Buka Cloud Scheduler

  2. Centang kotak di samping pekerjaan Anda.

  3. Klik Hapus dan konfirmasi penghapusan.

  4. Pada konsol Google Cloud, buka halaman Cloud Run.

    Buka Cloud Run

  5. Pilih kotak centang di samping layanan Anda.

  6. Klik Hapus dan konfirmasi penghapusan.

  7. Di konsol Google Cloud, buka halaman Akun Layanan.

    Buka Akun Layanan

  8. Centang kotak di samping akun layanan yang Anda buat.

  9. Klik Hapus dan konfirmasi penghapusan.

Langkah berikutnya