Skip to content

tomarv2/terraform-google-pubsub

Repository files navigation

Terraform module for Google PubSub

Versions

  • Module tested for Terraform 1.0.1.
  • Google provider version 4.12.0
  • main branch: Provider versions not pinned to keep up with Terraform releases
  • tags releases: Tags are pinned with versions (use in your releases)

Usage

Option 1:

terrafrom init
terraform plan -var='teamid=tryme' -var='prjid=project1'
terraform apply -var='teamid=tryme' -var='prjid=project1'
terraform destroy -var='teamid=tryme' -var='prjid=project1'

Note: With this option please take care of remote state storage

Option 2:

Recommended method (stores remote state in remote backend(S3, Azure storage, or Google bucket) using prjid and teamid to create directory structure):
  • Create python 3.8 virtual environment
python3 -m venv <venv name>
  • Install package:
pip install tfremote --upgrade
  • Set below environment variables:
export TF_GCLOUD_BUCKET=<remote state bucket name>
export TF_GCLOUD_PREFIX=<remote state bucket prefix>
export TF_GCLOUD_CREDENTIALS=<gcp credentials.json>
  • Updated examples directory with required values.

  • Run and verify the output before deploying:

tf -c=gcloud plan -var='teamid=foo' -var='prjid=bar'
  • Run below to deploy:
tf -c=gcloud apply -var='teamid=foo' -var='prjid=bar'
  • Run below to destroy:
tf -c=gcloud destroy -var='teamid=foo' -var='prjid=bar'

Note: Read more on tfremote

PubSub Topic - pull & push subscription
terraform {
  required_version = ">= 1.0.1"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 4.12.0"
    }
  }
}

provider "google" {
  region  = var.region
  project = var.project
}

module "pubsub" {
  source = "../"

  pull_subscriptions = [
    {
      name = "pull"
    },
  ]

  push_subscriptions = [
    {
      name          = "push"
      push_endpoint = "https://${var.project}.appspot.com/"
    },
  ]
  #-----------------------------------------------
  # Note: Do not change teamid and prjid once set.
  teamid = var.teamid
  prjid  = var.prjid
}

Please refer to examples directory link for references.

Requirements

Name Version
terraform >= 1.0.1
google ~> 4.12.0

Providers

Name Version
google ~> 4.12.0

Modules

No modules.

Resources

Name Type
google_pubsub_subscription.pull_subscriptions resource
google_pubsub_subscription.push_subscriptions resource
google_pubsub_topic.topic resource

Inputs

Name Description Type Default Required
create_topic Specify true if you want to create a topic bool true no
default_ack_deadline_seconds Default ack deadline in seconds string "10" no
message_storage_policy A map of storage policies. Default - inherit from organization's Resource Location Restriction policy. map(any) {} no
prjid Name of the project/stack e.g: mystack, nifieks, demoaci. Should not be changed after running 'tf apply' string n/a yes
pull_subscriptions The list of the pull subscriptions list(map(string)) [] no
push_subscriptions The list of the push subscriptions list(map(string)) [] no
teamid Name of the team/group e.g. devops, dataengineering. Should not be changed after running 'tf apply' string n/a yes
topic_kms_key_name The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. string null no
topic_name The Pub/Sub topic name string null no

Outputs

Name Description
pubsub_pull_message_retention PubSub pull subscription retention
pubsub_pull_subscription_id PubSub pull subscription id
pubsub_pull_subscription_name PubSub pull subscription name
pubsub_push_message_retention PubSub push subscription retention
pubsub_push_subscription_id PubSub push subscription id
pubsub_push_subscription_name PubSub push subscription name
pubsub_topic_id PubSub topic id
pubsub_topic_name PubSub topic name
pubsub_topic_project PubSub topic project