1. Answers
  2. Creating a GCP Pub/Sub Subscription

How do I build a GCP Pubsub Subscription?

To build a Google Cloud Pub/Sub subscription, you’ll need to set up both a Pub/Sub topic and a subscription that listens to the topic. This guide walks you through the configuration.

First, you define the Pub/Sub topic. A topic is a named resource to which messages are sent by publishers.

Next, you create a subscription to that topic. A subscription represents the stream of messages from a specific topic, to be delivered to the subscribing application.

Finally, you include stack exports to make it easy for other parts of your infrastructure to see the values you’re working with.

Here is the program:

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

// Create a Pub/Sub topic
const exampleTopic = new gcp.pubsub.Topic("example_topic", {name: "my-example-topic"});
// Create a Pub/Sub subscription
const exampleSubscription = new gcp.pubsub.Subscription("example_subscription", {
    name: "my-example-subscription",
    topic: exampleTopic.name,
    ackDeadlineSeconds: 20,
});
export const topicName = exampleTopic.name;
export const subscriptionName = exampleSubscription.name;
const config = new pulumi.Config();
// The project ID to deploy resources
const projectId = config.get("projectId") || "my-gcp-project-id";
// The GCP region to deploy resources
const region = config.get("region") || "us-central1";

In summary, you have created a Google Cloud Pub/Sub topic and a subscription to that topic, along with the necessary configuration and outputs to help integrate these resources into your infrastructure.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up