diff --git a/terraform/basic_gke/main.tf b/terraform/basic_gke/main.tf index 37988c7..80f8624 100644 --- a/terraform/basic_gke/main.tf +++ b/terraform/basic_gke/main.tf @@ -207,6 +207,7 @@ module "cf_to_pubsub" { source = "../modules/cf_to_pubsub" project = var.project region = var.region + topic_name = "bigquery-etl" source_bucket = google_storage_bucket.bucket service_cloudbuild = google_project_service.cloudbuild } diff --git a/terraform/modules/cf_to_pubsub/cf_to_pubsub.tf b/terraform/modules/cf_to_pubsub/cf_to_pubsub.tf index 411e297..09d3305 100644 --- a/terraform/modules/cf_to_pubsub/cf_to_pubsub.tf +++ b/terraform/modules/cf_to_pubsub/cf_to_pubsub.tf @@ -11,6 +11,11 @@ variable "region" { type = string } +variable "topic_name" { + description = "The name of topic where the events should be published." + type = string +} + variable "source_bucket" { description = "Google storage bucket where the source code will be stored." } @@ -61,6 +66,11 @@ resource "google_cloudfunctions_function" "function" { ingress_settings = "ALLOW_ALL" # ingress_settings = "ALLOW_INTERNAL_ONLY" + environment_variables = { + GCP_PROJECT = var.project + GCP_TOPIC = var.topic_name + } + depends_on = [ var.service_cloudbuild ] diff --git a/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/main.py b/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/main.py index fe14ebd..14e321f 100644 --- a/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/main.py +++ b/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/main.py @@ -1,8 +1,14 @@ import json +import os + +from google.cloud import pubsub_v1 + +publisher = pubsub_v1.PublisherClient() +topic = publisher.topic_path(os.environ.get("GCP_PROJECT"), os.environ.get("GCP_TOPIC")) def push_to_pubsub(request_params): - print(json.dumps(request_params)) + publisher.publish(topic, json.dumps(request_params).encode("utf-8")).result() def main(request): diff --git a/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/requirements.txt b/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/requirements.txt index 46a48dd..fd39a8c 100644 --- a/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/requirements.txt +++ b/terraform/modules/cf_to_pubsub/functions/cf_to_pubsub/requirements.txt @@ -1 +1,2 @@ Flask==1.1.2 +google-cloud-pubsub==2.6.1