このチュートリアルでは、Cloud Audit Logs のトリガーを使用して、イベント ドリブンの Cloud Run 関数を作成、デプロイ、トリガーする方法について説明します。
Cloud Run 関数を使用すると、Cloud Audit Logs のエントリから関数をトリガーできます。多くの Google Cloud プロダクトでは、プロダクト内で重要なアクションが発生すると、Cloud Audit Logs にログが書き込まれます。これらのログエントリは Cloud Run 関数の実行をリアルタイムでトリガーできるため、ユーザーはこれらを自動的に処理したり、操作することができます。
これらのログは、Google Cloud 上のさまざまなイベントによって生成され、ほとんどの Google Cloud プロダクトを対象としています。そのため、Cloud Audit Logs トリガーを使用すると、Google Cloud のほとんどの状態変化に対応する関数を作成できます。
このチュートリアルでは、Cloud Audit Logs トリガーを使用して、新しく作成された Compute Engine インスタンスに、作成したエンティティ(個人またはサービス アカウント)の名前のラベルを付ける方法を学習します。
Cloud Audit Logs を初めて使用する場合は、Cloud Audit Logs のドキュメントで詳細をご確認ください。
目標
- Compute Engine VM インスタンスの作成時に Cloud Audit Logs イベントを受信するイベント ドリブンの Cloud Run 関数を作成する。
- 関数をトリガーする Compute Engine VM インスタンスを作成する。インスタンスが作成されたときに、作成したエンティティ(ユーザーまたはサービス アカウント)の名前をラベルとしてインスタンスに設定する。
費用
このドキュメントでは、Google Cloud の次の課金対象のコンポーネントを使用します。
- Cloud Run functions
- Cloud Build
- Pub/Sub
- Artifact Registry
- Eventarc
- Cloud Logging
- Compute Engine
For details, see Cloud Run functions pricing.
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
始める前に
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Run, Cloud Build, Artifact Registry, Eventarc, Logging, Compute Engine, and Pub/Sub APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Run, Cloud Build, Artifact Registry, Eventarc, Logging, Compute Engine, and Pub/Sub APIs.
- Cloud SDK をインストールし、初期化します。
gcloud
コンポーネントを更新します。- 開発環境を準備します。
gcloud components update
コマンド プロンプトが必要な場合は、Google Cloud Shell を使用できます。Google Cloud Shell は、Google Cloud SDK がすでに含まれているコマンドライン環境であるため、インストールする必要はありません。Google Cloud SDK は、Compute Engine の仮想マシンにもプリインストールされています。
前提条件
Google Cloud コンソールで [IAM と管理] > [監査ログ] ページを開きます。
Compute Engine API で、Cloud Audit Logs の管理読み取り、データ読み取り、データ書き込みの各ログタイプを有効にします。
Compute Engine サービス アカウントに
Editor
ロールがあるかどうかを確認します。このサービス アカウントは、Cloud Run 関数のサービス ID として使用されます。テーブルで
PROJECT_NUMBER-compute@developer.gserviceaccount.com
エントリを見つけて、Roles
列を確認します。列にEditor
が含まれている場合は、次の手順をスキップできます。それ以外の場合は、次の手順に進んで、必要なロールをサービス アカウントに割り当てます。プロジェクトの Compute Engine サービス アカウントに
eventarc.eventReceiver
ロールを付与します。PROJECT_ID=$(gcloud config get-value project) PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)') # Allow service account token creation gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role roles/eventarc.eventReceiver
Pub/Sub トリガーが関数を実行できるように、プロジェクトの Compute Engine サービス アカウントに
run.invoker
ロールを付与します。PROJECT_ID=$(gcloud config get-value project) PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)') # Allow service account token creation gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role roles/run.invoker
プロジェクトの Compute Engine サービス アカウントに
compute.instanceAdmin
ロールを付与して、VM インスタンスを取得してラベルを設定するために必要な権限を関数コードに付与します。PROJECT_ID=$(gcloud config get-value project) PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)') # Allow service account token creation gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role roles/compute.instanceAdmin
アプリケーションの準備
ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
または、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Cloud Audit Logs にアクセスするための Cloud Run 関数のサンプルコードがあるディレクトリに移動します。
Node.js
cd nodejs-docs-samples/functions/v2/autoLabelInstance/
Python
cd python-docs-samples/functions/v2/label_gce_instance/
Go
cd golang-samples/functions/functionsv2/label_gce_instance/
Java
cd java-docs-samples/functions/v2/label-compute-instance/
サンプルコードを見てみましょう。
Node.js
Python
Go
Java
関数のデプロイ
Cloud Audit Logs トリガーを使用して関数をデプロイするには、サンプルコード(Java の場合は pom.xml
ファイル)を含むディレクトリで次のコマンドを実行します。
Node.js
gcloud functions deploy nodejs-cal-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=autoLabelInstance \
--trigger-location=REGION
\
--trigger-event-filters="type=google.cloud.audit.log.v1.written" \
--trigger-event-filters="serviceName=compute.googleapis.com" \
--trigger-event-filters="methodName=v1.compute.instances.insert"
サポートされている Node.js バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Python
gcloud functions deploy python-cal-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=label_gce_instance \
--trigger-location=REGION
\
--trigger-event-filters="type=google.cloud.audit.log.v1.written" \
--trigger-event-filters="serviceName=compute.googleapis.com" \
--trigger-event-filters="methodName=v1.compute.instances.insert"
サポートされている Python バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Go
gcloud functions deploy go-cal-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=label-gce-instance \
--trigger-location=REGION
\
--trigger-event-filters="type=google.cloud.audit.log.v1.written" \
--trigger-event-filters="serviceName=compute.googleapis.com" \
--trigger-event-filters="methodName=v1.compute.instances.insert"
サポートされている Go バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Java
gcloud functions deploy java-cal-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.AutoLabelInstance \ --memory=512MB \
--trigger-location=REGION
\
--trigger-event-filters="type=google.cloud.audit.log.v1.written" \
--trigger-event-filters="serviceName=compute.googleapis.com" \
--trigger-event-filters="methodName=v1.compute.instances.insert"
サポートされている Java バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
上記のデプロイ コマンドでは、VM の作成に対応する次のイベント フィルタ パラメータを指定します。
type
: Cloud Audit Logs イベントタイプ(google.cloud.audit.log.v1.written
)。serviceName
: ログエントリを生成した Google Cloud サービスの名前。この場合はcompute.googleapis.com
です。methodName
: ログエントリを生成した API メソッドの名前。この場合はv1.compute.instances.insert
です。
関数のトリガー
関数がデプロイされたら、動作を確認します。
Compute Engine VM インスタンスを作成します。
gcloud compute instances create
YOUR_INSTANCE_NAME
--zoneYOUR_ZONE
または、Google Cloud コンソールに移動して、[VM を作成] をクリックします。
次のコマンドを実行して、インスタンスに適切なラベルが設定されていることを確認します。
gcloud compute instances describe
YOUR_INSTANCE_NAME
\ --zoneYOUR_ZONE \ --format 'value(labels)'
creator=YOURNAMEYOUR_DOMAIN
形式のラベルが付いているはずです。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
プロジェクトの削除
課金されないようにする最も簡単な方法は、チュートリアル用に作成したプロジェクトを削除することです。
プロジェクトを削除するには:
- 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.
関数の削除
Cloud Run 関数を削除しても、Cloud Storage に保存されたリソースは削除されません。
このチュートリアルで作成した Cloud Run 関数を削除するには、次のコマンドを実行します。
Node.js
gcloud functions delete nodejs-cal-function --gen2 --region REGION
Python
gcloud functions delete python-cal-function --gen2 --region REGION
Go
gcloud functions delete go-cal-function --gen2 --region REGION
Java
gcloud functions delete java-cal-function --gen2 --region REGION
Google Cloud コンソールから Cloud Run 関数を削除することもできます。
Compute Engine VM インスタンスを削除する
このチュートリアルで作成した Compute Engine VM インスタンスを削除するには、次のコマンドを実行します。
gcloud compute instances deleteYOUR_INSTANCE_NAME
--zoneYOUR_ZONE