Set PubSub Subscription Expiration to Never Expire via Terraform

google-cloud-platformterraform

When creating a PubSub subscription manually you have the option of setting the expiration as "Never expire". Example:

expiration set to never expire

I am attempting to manage my cloud infrastructure through Terraform. The expiration there defaults to 31 days. I have some subscriptions were it is perfectly possible no activity may happen in 31 days and I don't want the subscription to disappear in that case.

Here is an example of the configuring the property from the Terraform documentation:

expiration_policy {
  ttl = "300000.5s"
}

What it doesn't say is how to set the policy to never expire. Looking at the GCP API docs it indicates the following structure:

"expirationPolicy": {
  object (ExpirationPolicy)
},

If I click into ExpirationPolicy then I see something of interest:

If ttl is not set, the associated resource never expires.

This makes me think I should just send an empty expiration policy:

expiration_policy {
}

But this gives me the following error:

Error: Missing required argument

  on main.tf line 39, in resource "google_pubsub_subscription" "mct_bloodrelay_staging":
  39:   expiration_policy {

The argument "ttl" is required, but no definition was found.

I'm thinking this is a terraform problem since the docs seem to indicate ttl might be left out and it looks like terraform is requiring it. But before I create a case on the Github project I figured I would post here to see if I am missing something.

Best Answer

Per the Terraform documentation for pusub subscription, the ttl parameter is required. The only way to get around this is to set it to empy string -

  expiration_policy {
    ttl = ""
  }

Updated: full pubsub subscription resource definition example -

  resource "google_pubsub_subscription" "test-sub" {
  name  = "test-sub"
  topic = "projects/PROJECT_ID/topics/TOPIC_NAME"
  expiration_policy {
    ttl = ""
  }
}

Related Topic