Google Cloud Pub/Sub Topic

This page shows how to write Terraform for Cloud Pub/Sub Topic and write them securely.

google_pubsub_topic (Terraform)

The Topic in Cloud Pub/Sub can be configured in Terraform with the resource name google_pubsub_topic. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub
resource "google_pubsub_topic" "bigquery-append-tsv" {
    name = "bigquery-append-tsv"
    labels = {user = "trellis"}

resource "google_pubsub_topic" "bigquery-import-csv" {
resource "google_pubsub_topic" "version_every_minute" {
  name = "version_every_minute"

resource "google_pubsub_topic" "version_every_two_minutes" {
  name = "version_every_two_minutes"
resource "google_pubsub_topic" "taskbuilds" {
  name = "taskbuilds"

# Create for PubSub Topic for cleanups
# terraform import google_pubsub_topic.cleanups cleanups
resource "google_pubsub_topic" "version_every_minute" {
  name = "version_every_minute"

resource "google_pubsub_topic" "version_every_two_minutes" {
  name = "version_every_two_minutes"
resource "google_pubsub_topic" "foo" {
  project = var.project
  name    =

resource "google_pubsub_subscription" "foo" {

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).


The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. Your project's PubSub service account ('service-[[PROJECT_NUMBER]]') must have 'roles/cloudkms.cryptoKeyEncrypterDecrypter' to use this feature. The expected format is 'projects//locations//keyRings//cryptoKeys/'

  • labels optional - map from string to string

A set of key/value label pairs to assign to this Topic.

Name of the topic.

  • project optional computed - string
  • message_storage_policy list block

    A list of IDs of GCP regions where messages that are published to the topic may be persisted in storage. Messages published by publishers running in non-allowed GCP regions (or running outside of GCP altogether) will be routed for storage in one of the allowed regions. An empty list means that no regions are allowed, and is not a valid configuration.

  • timeouts single block

Explanation in Terraform Registry

A named resource to which messages are sent by publishers. To get more information about Topic, see:

  • API documentation
  • How-to Guides
    • Managing Topics

      Note: You can retrieve the email of the Google Managed Pub/Sub Service Account used for forwarding by using the google_project_service_identity resource.

Frequently asked questions

What is Google Cloud Pub/Sub Topic?

Google Cloud Pub/Sub Topic is a resource for Cloud Pub/Sub of Google Cloud Platform. Settings can be wrote in Terraform.

Where can I find the example code for the Google Cloud Pub/Sub Topic?

For Terraform, the StanfordBioinformatics/trellis-mvp-terraform, chtest0410/tfsample and geosentry/cloud source code examples are useful. See the Terraform Example section for further details.


Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.