Skip to content

googleStorageNotification

Creates a new notification configuration on a specified bucket, establishing a flow of event notifications from GCS to a Cloud Pub/Sub topic. For more information see the official documentation and API.

In order to enable notifications, a special Google Cloud Storage service account unique to the project must exist and have the IAM permission "projects.topics.publish" for a Cloud Pub/Sub topic in the project. This service account is not created automatically when a project is created. To ensure the service account exists and obtain its email address for use in granting the correct IAM permission, use the googleStorageProjectServiceAccount datasource's emailAddress value, and see below for an example of enabling notifications by granting the correct IAM permission. See the notifications documentation for more details.

NOTE: This resource can affect your storage IAM policy. If you are using this in the same config as your storage IAM policy resources, consider making this resource dependent on those IAM resources via dependsOn. This will safeguard against errors due to IAM race conditions.

Example Usage

/*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 googlePubsubTopicTopic = new google.pubsubTopic.PubsubTopic(
  this,
  "topic",
  {
    name: "default_topic",
  }
);
const googleStorageBucketBucket = new google.storageBucket.StorageBucket(
  this,
  "bucket",
  {
    location: "US",
    name: "default_bucket",
  }
);
const dataGoogleStorageProjectServiceAccountGcsAccount =
  new google.dataGoogleStorageProjectServiceAccount.DataGoogleStorageProjectServiceAccount(
    this,
    "gcs_account",
    {}
  );
const googlePubsubTopicIamBindingBinding =
  new google.pubsubTopicIamBinding.PubsubTopicIamBinding(this, "binding", {
    members: [
      `serviceAccount:\${${dataGoogleStorageProjectServiceAccountGcsAccount.emailAddress}}`,
    ],
    role: "roles/pubsub.publisher",
    topic: googlePubsubTopicTopic.id,
  });
new google.storageNotification.StorageNotification(this, "notification", {
  bucket: googleStorageBucketBucket.name,
  custom_attributes: [
    {
      "new-attribute": "new-attribute-value",
    },
  ],
  depends_on: [`\${${googlePubsubTopicIamBindingBinding.fqn}}`],
  event_types: ["OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE"],
  payload_format: "JSON_API_V1",
  topic: googlePubsubTopicTopic.id,
});

Argument Reference

The following arguments are supported:

  • bucket - (Required) The name of the bucket.

  • payloadFormat - (Required) The desired content of the Payload. One of "jsonApiV1" or "none".

  • topic - (Required) The Cloud PubSub topic to which this subscription publishes. Expects either the topic name, assumed to belong to the default GCP provider project, or the project-level name, i.e. projects/myGcpProject/topics/myTopic or myTopic. If the project is not set in the provider, you will need to use the project-level name.


  • customAttributes - (Optional) A set of key/value attribute pairs to attach to each Cloud PubSub message published for this notification subscription

  • eventTypes - (Optional) List of event type filters for this notification config. If not specified, Cloud Storage will send notifications for all event types. The valid types are: "objectFinalize", "objectMetadataUpdate", "objectDelete", "objectArchive"

  • objectNamePrefix - (Optional) Specifies a prefix path filter for this notification config. Cloud Storage will only send notifications for objects in this bucket whose names begin with the specified prefix.

Attributes Reference

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

  • notificationId - The ID of the created notification.

  • selfLink - The URI of the created resource.

Import

Storage notifications can be imported using the notification id in the format <bucketName>/notificationConfigs/<id> e.g.

$ terraform import google_storage_notification.notification default_bucket/notificationConfigs/102