LarryDpk
发布于 2021-11-13 / 870 阅读
0

Manage GCP Pubsub with Terraform

1 Introduction

Terraform is a tool to manage infrastructure on many platform like AWS, GCP and Azure. I will show you how to manage the GCP Pub/Sub with Terraform in this article.

For GCP project setup, please refer to: How to initiate the GCP project and use gcloud to access

2 Create Pub/Sub with Terraform

2.1 Download Terraform Plugin

Download the Terraform plugin for GCP:

$ export TERRAFORM_PLUGIN=/Users/larry/Software/terraform/plugins
$ mkdir -p ${TERRAFORM_PLUGIN}/registry.terraform.io/hashicorp/google/4.0.0/darwin_amd64
$ cd ${TERRAFORM_PLUGIN}/registry.terraform.io/hashicorp/google/4.0.0/darwin_amd64
$ wget https://releases.hashicorp.com/terraform-provider-google/4.0.0/terraform-provider-google_4.0.0_darwin_amd64.zip
$ unzip terraform-provider-google_4.0.0_darwin_amd64.zip

2.2 Terraform code

We need to provide the Terraform code to create the GCP Pub/Sub. For more details, please go to Terrafrom GCP.

version.tf:

terraform {
  required_version = "= 1.0.11"
  required_providers {

    google = {
      source  = "hashicorp/google"
      version = "= 4.0.0"
    }
  }
}

main.tf:

provider "google" {
  project     = "pkslow"
}

resource "google_pubsub_topic" "pkslow-poc" {
  name = "pkslow-poc"
}

resource "google_pubsub_subscription" "pkslow-poc" {
  name  = "pkslow-poc"
  topic = google_pubsub_topic.pkslow-poc.name

  labels = {
    foo = "bar"
  }

  # 20 minutes
  message_retention_duration = "1200s"
  retain_acked_messages      = true

  ack_deadline_seconds = 20

  expiration_policy {
    ttl = "300000.5s"
  }
  retry_policy {
    minimum_backoff = "10s"
  }

  enable_message_ordering    = true
}

2.3 init and apply

Initiate the Terraform project:

$ terraform init -plugin-dir=${TERRAFORM_PLUGIN}

Apply the change to the GCP:

$ terraform apply -auto-approve

If applied without error, we can check the Pub/Sub:

$ gcloud pubsub topics list
---
name: projects/pkslow/topics/pkslow-poc

$ gcloud pubsub subscriptions list
---
ackDeadlineSeconds: 20
enableMessageOrdering: true
expirationPolicy:
  ttl: 300000.500s
labels:
  foo: bar
messageRetentionDuration: 1200s
name: projects/pkslow/subscriptions/pkslow-poc
pushConfig: {}
retainAckedMessages: true
retryPolicy:
  maximumBackoff: 600s
  minimumBackoff: 10s
topic: projects/pkslow/topics/pkslow-poc

Note: We did not provide any credential info because Terraform will fetch from environment variable GOOGLE_APPLICATION_CREDENTIALS.

3 publish and pull messages

Use gcloud to publish the messages to Pub/Sub:

$ gcloud pubsub topics publish pkslow-poc --message="www.pkslow.com"
messageIds:
- '3491736520339885'

$ gcloud pubsub topics publish pkslow-poc --message="Larry Deng"
messageIds:
- '3491738650256958'

$ gcloud pubsub topics publish pkslow-poc --message="Hi, pkslower"
messageIds:
- '3491739306095970'

Fetch the messages from Pub/Sub:

$ gcloud pubsub subscriptions pull pkslow-poc --auto-ack

We can also monitor the MQ on GCP console:

4 Code

Code on GitHub: https://github.com/LarryDpk/pkslow-samples