0

I've been struggling for a while in Terraform to create an Event Subscription in an Azure Event Grid

As-per screenshot....

EVENT SUBSCRIPTION DETAILS
NAME : EventGrid-Sub1
(don't need to change Event Schema) 

TOPIC DETAILS
Event Grid Domain
Topic Resource: EDG-SBX-EventGrid1
Domain Type: EventGrid-DomainTopic1 

ENDPOINT DETAILS
Endpoint Type: Event Hubs
Endpoint : eh-sbx-Ingestion 

I've been using these as reference, but it seems not only a bit chicken-and-egg, but pieces missing?

https://www.terraform.io/docs/providers/azurerm/r/eventgrid_event_subscription.html https://www.terraform.io/docs/providers/azurerm/r/eventgrid_topic.html

Has anyone got this working in Terraform?

Thanks in advance

Azure Screenshot on Event Grids / Create Event Subscription screen

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
nmca70
  • 183
  • 3
  • 16
  • Hey, I've added backticks around the screenshot details to highlight it a bit. Can you add what you think your tf script might look like and highlight the bits you're missing? – Software Engineer Oct 25 '19 at 19:21

1 Answers1

1

@nmca70 There are a couple of ways to achieve this:

  1. Create an ARM template from the final deployment and then run that ARM template using Terraform:

https://www.terraform.io/docs/providers/azurerm/r/template_deployment.html

  1. Create resources in the below order:

A sample:

resource "azurerm_resource_group" "test" {
  name     = "resourceGroup1"
  location = "West US 2"
}

resource "azurerm_eventhub_namespace" "test" {
  name                = "acceptanceTestEventHubNamespace"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  sku                 = "Standard"
  capacity            = 1
  kafka_enabled       = false

  tags = {
    environment = "Production"
  }
}

resource "azurerm_eventhub" "test" {
  name                = "acceptanceTestEventHub"
  namespace_name      = "${azurerm_eventhub_namespace.test.name}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  partition_count     = 2
  message_retention   = 1
}

resource "azurerm_eventgrid_topic" "test" {
  name                = "my-eventgrid-topic"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"

  tags = {
    environment = "Production"
  }
}

resource "azurerm_eventgrid_domain" "test" {
  name                = "my-eventgrid-domain"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  input_schema        = "eventgridschema"

  input_mapping_fields= {
    topic = "my-eventgrid-topic"
  }

  tags = {
    environment = "Production"
  }
}

resource "azurerm_eventgrid_event_subscription" "default" {
  name  = "defaultEventSubscription"
  scope = "${azurerm_resource_group.default.id}"
  event_delivery_schema = "EventGridSchema"
  topic_name = "my-eventgrid-topic"

  eventhub_endpoint {
    storage_account_id = "${azurerm_eventhub.test.id}"
  }
}

Hope this helps!

AmanGarg-MSFT
  • 1,123
  • 6
  • 10
  • Note, there's a typo there: "input_mapping_fields" not "input_mapping_fields=". Also, input_schema should be "EventGridSchema", I believe. When trying this, it says InputMappingSchema isn't compatible with EventGridSchema. – Kirk Marple Nov 06 '19 at 05:08
  • Which version of Terraform and Azurerm provider are you using? – AmanGarg-MSFT Nov 06 '19 at 05:29
  • My reference: https://www.terraform.io/docs/providers/azurerm/r/eventgrid_domain.html – AmanGarg-MSFT Nov 06 '19 at 07:58
  • Unless I'm misunderstanding the OP, there is no need to create a topic here since they are using a domain. Unfortunately, topics have different contextual meanings dependant upon whether it is a domain topic or a grid topic. In a domain, a topic only exists when a subscription is created that refers to it - I can see no good reason why you would create an OG event grid topic and then subscribe to it inside the domain. I think the OP is looking for a domain that has a topic within it to which the subscription is attached. – Steve Pettifer Jan 09 '20 at 08:45
  • Also, do not use an ARM template for this: Mixing ARM and TF works, but it is harder to manage and should only be used when there is no alternative (for instance when the AzureRM provider cannot do something you require). The TF provider actually works perfectly well for this, and we have many subscriptions to an event grid domain managed entirely by Terraform. – Steve Pettifer Jan 09 '20 at 08:49