0
votes

I am trying to work with terraform modules to create event subscription pointing to storage queue as an endpoint to it.

Below is the module

  resource "azurerm_eventgrid_event_subscription" "events" {
  name                      = var.name
  scope                     = var.scope
  subject_filter            = var.subject_filter
  storage_queue_endpoint    = var.storage_queue_endpoint
  }

and terraform is

module "storage_account__event_subscription" {
  source       = "../modules/event"
  name         = "testevent"
  scope        = test
  subject_filter = {
    subject_begins_with = "/blobServices/default/containers/test/blobs/in"
  }

  storage_queue_endpoint = {
    storage_account_id = test
    queue_name         = test
  }
}

Error message:

: subject_filter { Blocks of type "subject_filter" are not expected here. Error: Unsupported block type on azure.tf line 90, in module "storage_account__event_subscription": : storage_queue_endpoint { Blocks of type "storage_queue_endpoint" are not expected here.

How do i parse the optional fields properly in terraform modules ?

1

1 Answers

0
votes

In you module:

  resource "azurerm_eventgrid_event_subscription" "events" {
  name                      = var.name
  scope                     = var.scope
  subject_filter            = {
    subject_begins_with = var.subject_begins_with
}
  storage_queue_endpoint    = var.storage_queue_endpoint
  }

Formatting is off here so make sure to run terraform fmt to account for my poor formatting. Also add the variable to the variables.tf file.

Your Terraform file:

module "storage_account__event_subscription" {
  source       = "../modules/event"
  name         = "testevent"
  scope        = test
  subject_begins_with = "/blobServices/default/containers/test/blobs/in"

  storage_queue_endpoint = {
    storage_account_id = test
    queue_name         = test
  }
}

You create the full structure in the module and then you assign the variables in the terraform file.

Anything that will have the same, or generally the same, value can have a default value set in the variables.tf as well so that you get smaller chunks in the TF file.