angelcervera
angelcervera

Reputation: 4209

Creating an Azure event-subscription filtering by Microsoft.Storage.BlobCreated event using Terraform

What is the equivalent of this Azure Cli example (from Azure doc) using Terraform Azure provider 2.19.0

az eventgrid resource event-subscription create -g myResourceGroup \
--provider-namespace Microsoft.Storage --resource-type storageAccounts \
--resource-name myblobstorage12345 --name myFuncSub  \
--included-event-types Microsoft.Storage.BlobCreated \
--subject-begins-with /blobServices/default/containers/images/blobs/ \
--endpoint https://mystoragetriggeredfunction.azurewebsites.net/runtime/webhooks/eventgrid?functionName=imageresizefunc&code=<key>

Note: Following this Terraform Github Issue, resource_group_name and topic_name are deprecated.

Upvotes: 0

Views: 2254

Answers (2)

Charles Xu
Charles Xu

Reputation: 31462

I'm afraid you use the wrong Azure CLI command, there is only CLI command for Event Grid like az eventgrid event-subscription create, and it does not have the parameter for the resource group. So you also do not need to care about it in the Terraform code.

Update:

With more attention to the parameter scope:

Specifies the scope at which the EventGrid Event Subscription should be created.

You can also understand it from parameter --source-resource-id the Azure CLI command :

--source-resource-id

Fully qualified identifier of the Azure resource to which the event subscription needs to be created.

Terraform does not describe it clearly. So we need to understand it from the CLI or Azure REST API, that's the actual thing finally used when you create resources in Azure.

And Terraform also does not support all the things that Azure supports. I'm also confused with Terraform sometimes. When this time, I recommend you using the CLI command directly or run the CLI command inside the Terraform code.

Upvotes: 0

angelcervera
angelcervera

Reputation: 4209

So it looks like Terraform is using the scope parameter to inference part of the parameters.

So this is the equivalent in Terraform:

resource "azurerm_eventgrid_event_subscription" "my_func_sub" {
  name  = "myFuncSub"
  scope = azurerm_storage_account.images.id

  included_event_types = [
    "Microsoft.Storage.BlobCreated"
  ]

  subject_filter {
    subject_begins_with = "/blobServices/default/containers/${azurerm_storage_container.images.name}/blobs/"
  }

  webhook_endpoint {
    url = "https://mystoragetriggeredfunction.azurewebsites.net/runtime/webhooks/eventgrid?functionName=imageresizefunc&code=<key>"
  }

}

Ofcourse, you need to replace azurerm_storage_container.images and the webhook url with rigth values in your case.

It is important pay attention to scope. It should be the id of the resource that will publish events. In our case, it is a storage container.

Upvotes: 2

Related Questions