[A-00162]TerraformでCloud Functions×Cloud Storage×Eventarcを作成する

Google Cloud上にイベントトリガー型のファンクションを作成したいと思います。

・ Architecture

・Terraformの作成

resource "google_cloudfunctions2_function" "default" {
    name = "hello-function"
    location = var.region
    description = "tutorial function"

    build_config {
      runtime = "python311"
      entry_point = "hello_gcs"
      source {
        storage_source {
            bucket = google_storage_bucket.default.name
            object = google_storage_bucket_object.default.name
        }
      }
    }

    service_config {
      max_instance_count = 1
      available_memory = "256M"
      timeout_seconds = 60
    }

    event_trigger {
        trigger_region = var.region
        event_type = "google.cloud.storage.object.v1.finalized"
        event_filters {
          attribute = "bucket"
          value = google_storage_bucket.default.name
        }
    }
}

output "function_uri" {
    value = google_cloudfunctions2_function.default.service_config[0].uri
}
terraform {
  required_providers {
    google = {
        source = "hashicorp/google"
        version = "4.79.0"
    }
  }
}

provider "google" {
    project = var.project_id
    region = var.region
}
resource "google_storage_bucket" "default" {
    name = "tutorial-cf-20230922"
    location = var.region
    uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "default" {
    name = "function-src.zip"
    bucket = google_storage_bucket.default.name
    source = "./function-src.zip"
}
variable "project_id" {
    type = string
    default = "<project_id>"
}

variable "region" {
    type = string
    default = "asia-northeast1"
}

・ファンクションプログラムの作成

from cloudevents.http import CloudEvent

import functions_framework


# Triggered by a change in a storage bucket
@functions_framework.cloud_event
def hello_gcs(cloud_event: CloudEvent) -> tuple:
    # """This function is triggered by a change in a storage bucket.

    # Args:
    #     cloud_event: The CloudEvent that triggered this function.
    # Returns:
    #     The event ID, event type, bucket, name, metageneration, and timeCreated.
    # """
    # data = cloud_event.data

    # event_id = cloud_event["id"]
    # event_type = cloud_event["type"]

    # bucket = data["bucket"]
    # name = data["name"]
    # metageneration = data["metageneration"]
    # timeCreated = data["timeCreated"]
    # updated = data["updated"]

    # print(f"Event ID: {event_id}")
    # print(f"Event type: {event_type}")
    # print(f"Bucket: {bucket}")
    # print(f"File: {name}")
    # print(f"Metageneration: {metageneration}")
    # print(f"Created: {timeCreated}")
    # print(f"Updated: {updated}")

    # return event_id, event_type, bucket, name, metageneration, timeCreated, updated
    print("Hello,world")


# [END functions_cloudevent_storage]
functions-framework==3.3.0
cloudevents==1.9.0

上記の2ファイルをzipに固めます。

zip -r function-src.zip ./*

・Zipファイルの配置

作成したzipファイルをterraformソースと同じ場所に配置しておきます。

・Terraform実行

terraform init
terraform plan
terraform apply

上記を実行、完了したらCloud Functionsの画面を確認します。

上記の赤枠のストレージに適当なファイルをアップロードするとこのファンクションが起動し、下記のように起動された形跡が記録されます。

確認が完了したら次のコマンドで後片付けします。

terraform destroy

・Appendix

公式ドキュメントはこちら

https://cloud.google.com/functions/docs/tutorials/terraform?hl=ja

https://cloud.google.com/functions/docs/samples/functions-helloworld-get?hl=ja#functions_helloworld_get-python

参考文献はこちら

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions2_function

https://zenn.dev/eastresident/articles/151572a7ec5a29

【Python】Cloud FunctionsとCloud Storageを連携して動かしてみる

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*