Skip to content

googlePubsubTopic

A named resource to which messages are sent by publishers.

To get more information about Topic, see:

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

Example Usage - Pubsub Topic Basic

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.pubsubTopic.PubsubTopic(this, "example", {
  labels: [
    {
      foo: "bar",
    },
  ],
  message_retention_duration: "86600s",
  name: "example-topic",
});

Example Usage - Pubsub Topic Cmek

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleKmsKeyRingKeyRing = new google.kmsKeyRing.KmsKeyRing(
  this,
  "key_ring",
  {
    location: "global",
    name: "example-keyring",
  }
);
const googleKmsCryptoKeyCryptoKey = new google.kmsCryptoKey.KmsCryptoKey(
  this,
  "crypto_key",
  {
    key_ring: googleKmsKeyRingKeyRing.id,
    name: "example-key",
  }
);
new google.pubsubTopic.PubsubTopic(this, "example", {
  kms_key_name: googleKmsCryptoKeyCryptoKey.id,
  name: "example-topic",
});

Example Usage - Pubsub Topic Geo Restricted

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.pubsubTopic.PubsubTopic(this, "example", {
  message_storage_policy: [
    {
      allowed_persistence_regions: ["europe-west3"],
    },
  ],
  name: "example-topic",
});

Example Usage - Pubsub Topic Schema Settings

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googlePubsubSchemaExample = new google.pubsubSchema.PubsubSchema(
  this,
  "example",
  {
    definition:
      '{\n  "type" : "record",\n  "name" : "Avro",\n  "fields" : [\n    {\n      "name" : "StringField",\n      "type" : "string"\n    },\n    {\n      "name" : "IntField",\n      "type" : "int"\n    }\n  ]\n}\n',
    name: "example",
    type: "AVRO",
  }
);
const googlePubsubTopicExample = new google.pubsubTopic.PubsubTopic(
  this,
  "example_1",
  {
    depends_on: [`\${${googlePubsubSchemaExample.fqn}}`],
    name: "example-topic",
    schema_settings: [
      {
        encoding: "JSON",
        schema: "projects/my-project-name/schemas/example",
      },
    ],
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
googlePubsubTopicExample.overrideLogicalId("example");

Argument Reference

The following arguments are supported:

  • name - (Required) Name of the topic.

  • kmsKeyName - (Optional) 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-{{projectNumber}}@gcpSaPubsubIamGserviceaccountCom) must have roles/cloudkmsCryptoKeyEncrypterDecrypter to use this feature. The expected format is projects/*/locations/*/keyRings/*/cryptoKeys/*

  • labels - (Optional) A set of key/value label pairs to assign to this Topic.

  • messageStoragePolicy - (Optional) Policy constraining the set of Google Cloud Platform regions where messages published to the topic may be stored. If not present, then no constraints are in effect. Structure is documented below.

  • schemaSettings - (Optional) Settings for validating messages published against a schema. Structure is documented below.

  • messageRetentionDuration - (Optional) Indicates the minimum duration to retain a message after it is published to the topic. If this field is set, messages published to the topic in the last messageRetentionDuration are always available to subscribers. For instance, it allows any attached subscription to seek to a timestamp that is up to messageRetentionDuration in the past. If this field is not set, message retention is controlled by settings on individual subscriptions. Cannot be more than 31 days or less than 10 minutes.

  • project - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.

The messageStoragePolicy block supports:

  • allowedPersistenceRegions - (Required) 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.

The schemaSettings block supports:

  • schema - (Required) The name of the schema that messages published should be validated against. Format is projects/{project}/schemas/{schema}. The value of this field will be deleted-schema if the schema has been deleted.

  • encoding - (Optional) The encoding of messages validated against schema. Default value is encodingUnspecified. Possible values are encodingUnspecified, json, and binary.

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

  • id - an identifier for the resource with format projects/{{project}}/topics/{{name}}

Timeouts

This resource provides the following Timeouts configuration options:

  • create - Default is 20 minutes.
  • update - Default is 20 minutes.
  • delete - Default is 20 minutes.

Import

Topic can be imported using any of these accepted formats:

$ terraform import google_pubsub_topic.default projects/{{project}}/topics/{{name}}
$ terraform import google_pubsub_topic.default {{project}}/{{name}}
$ terraform import google_pubsub_topic.default {{name}}

User Project Overrides

This resource supports User Project Overrides.