https://cloud.google.com/functions/docs/tutorials/pubsub?hl=ja
-- 1. 前作業
gcloud init
gcloud auth list
gcloud --version
gcloud projects create project01-9999999 \
--name="project01"
gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet
gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111
gcloud services enable compute.googleapis.com --project project01-9999999
-- 2. Cloud Functions, Cloud Build, Artifact Registry, Eventarc, Logging, and Pub/Sub APIs を有効化
gcloud services list --enabled
gcloud services enable cloudfunctions.googleapis.com \
run.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
eventarc.googleapis.com \
logging.googleapis.com \
pubsub.googleapis.com \
--project project01-9999999
-- 3. Pub/Sub トピック作成
gcloud pubsub topics create topic01
gcloud pubsub topics list
-- 4. アプリケーションを準備する
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
cd python-docs-samples/functions/v2/pubsub/
ls -l
cat main.py
-- 5. 関数のデプロイ
gcloud functions deploy func01 \
--gen2 \
--runtime=python312 \
--region=asia-northeast1 \
--source=. \
--entry-point=subscribe \
--trigger-topic=topic01
gcloud functions list
-- 6. 関数のトリガー
gcloud pubsub topics publish topic01 --message="Friend"
gcloud functions logs read \
--gen2 \
--region=asia-northeast1 \
--limit=5 \
func01
注: ログが表示されるまでに数分かかることがあります。すぐに表示されない場合は、1~2 分後にもう一度確認してください。
-- 7. クリーンアップ
gcloud functions delete func01 --gen2 --region asia-northeast1 -q
gcloud functions list
gcloud pubsub topics delete topic01
gcloud pubsub topics list
gcloud projects list
gcloud projects delete project01-9999999 -q
gcloud beta billing projects unlink project01-9999999