I can easily publish a message manually with JSON format in a pub/sub Topic using Console (GUI) as shown bellow and no error in my Cloud Function logs and works well.
Message:
{"account": "gcp", "key": "projects/xxxxxx/secrets/my-secret-v2/v/2"}
But I want to do it in my terraform code to publish this JSON message format and not sure if this is possible to do it with Terraform. Also, I've tried it with gcloud command but I've found a error message that can't read JSON format in the Cloud Function logs .
gcloud pubsub topics publish MY_TOPIC --message '{"account": "gcp", "key": "projects/xxxxxx/secrets/my-secret-v2/v/2"}'
This is the error in Cloud Function logs:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
My question is how can I publish this JSON format message via Terraform or gcloud?
My Terraform code:
resource "google_pubsub_topic" "topic" {
name = var.topic
project = var.project
Subscription:
resource "google_pubsub_subscription" "pubsub_subscription" {
name = var.pubsub_subscription
topic = google_pubsub_topic.topic.name
project = var.project
message_retention_duration = "86400s"
retain_acked_messages = false
enable_message_ordering = true
ack_deadline_seconds = 10
expiration_policy {
ttl = ""
}
Cloud Function:
resource "google_cloudfunctions_function" "function_gcp" {
project = var.project
region = "northamerica-northeast1"
name = "testing"
description = "GCP Test"
runtime = "python39"
event_trigger {
event_type = "google.pubsub.topic.publish"
resource = "projects/${var.project}/topics/${google_pubsub_topic.topic.name}"
}
available_memory_mb = 256
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.code.name
service_account_email = google_service_account.email
entry_point = "python-file"
labels = {
"application" : "my-application",
"source" : "terraform"
}
}
The issue is resolved by adding """ to the JSON message.
{"""account""": """gcp""", """key""": """projects/xxxxxx/secrets/my-secret-v2/v/2"""}