[A-00163]TerraformでPubSubからTriggerしてCloudFunctionsを実行する
Google Cloud上にPubSubのトリガー起因でCloud Functionsを実行する仕組みを作る。
・Architecture

・Terraform作成
resource "google_cloudfunctions2_function" "default" {
    name = "pubsub-event-function"
    location = var.region
    description = "triggered by pubsub topic."
    build_config {
        runtime = "python311"
        entry_point = "hello_pubsub"
        environment_variables = {
            BUILD_CONFIG_TEST = "build_test"
        }
        source {
          storage_source {
            bucket = google_storage_bucket.default.name
            object = google_storage_bucket_object.default.name
          }
        }
    }
    service_config {
      max_instance_count = 1
      min_instance_count = 1
      available_memory = "256M"
      timeout_seconds = 60
      environment_variables = {
        SERVICE_CONFIG_TEST = "config_test"
      }
      ingress_settings = "ALLOW_INTERNAL_ONLY"
      all_traffic_on_latest_revision = true
      service_account_email = "<input your service account mail>"
    }
    event_trigger {
      trigger_region = var.region
      event_type = "google.cloud.pubsub.topic.v1.messagePublished"
      pubsub_topic = google_pubsub_topic.default.id
      retry_policy = "RETRY_POLICY_RETRY"
    }
}resource "random_id" "bucket_prefix" {
    byte_length = 8
}
data "archive_file" "default" {
    type = "zip"
    output_path = "/tmp/function-src.zip"
    source_dir = "function-src/"
}terraform {
  required_providers {
    google = {
        source = "hashicorp/google"
        version = "4.79.0"
    }
  }
}
provider "google" {
    project = var.project_id
    region = var.region
}resource "google_pubsub_topic" "default" {
    name = "event-trigger-topic"
}resource "google_storage_bucket" "default" {
    name = "${random_id.bucket_prefix.hex}-gcf-source"
    location = var.region
    uniform_bucket_level_access = false
}
resource "google_storage_bucket_object" "default" {
    name = "function-src.zip"
    bucket = google_storage_bucket.default.name
    source = data.archive_file.default.output_path
}variable "project_id" {
    type = string
    default = "<input your project id>"
}
variable "region" {
    type = string
    default = "asia-northeast1"
}・実行プログラムを配置
terraformソースと同じ場所にディレクトリを作成します。
mkdir function-src && cd function-src && touch main.py requirements.txtソースの中身は下記の通りです。
from cloudevents.http import CloudEvent
import functions_framework
@functions_framework.cloud_event
def hello_pubsub(cloud_event: CloudEvent) -> tuple:
    
    print("hello, funcsitons!")functions-framework==3.3.0
cloudevents==1.9.0・Terraformの実行
下記のコマンドを実行して環境を作成します。
terraform init
terraform plan
terraform apply実行が完了したらpub/subでメッセージをpublishしてみます。赤枠をクリックします。




メッセージをpublishしたら次にCloud Functionsを確認します。赤枠をクリックします。



今回はpythonにハードコーディングした”hello, funcsitions!”が出力されましたが、pubsubから受け取ったメッセージを出力することも可能です。
確認ができたので下記のコマンドを実行して後片付けします。
terraform destroy・Appendix
公式ドキュメントはこちら
https://cloud.google.com/functions/docs/tutorials/terraform-pubsub?hl=ja
https://cloud.google.com/eventarc/docs/creating-triggers-terraform?hl=ja
https://cloud.google.com/functions/docs/samples/functions-v2-full?hl=ja
参考文献はこちら
コメントを残す