Skip to content

Commit

Permalink
Merged PR 2: Terraform script for EventHub
Browse files Browse the repository at this point in the history
Since ability to manage eventhub namespace firewall and virtual network rules from Terraform is not supported yet (hashicorp/terraform-provider-azurerm#2579) we need to use ARM templates.

Another challenge - azurerm_template_deployment fails to pass "array" type parameters properly with error
Error: azurerm_template_deployment.eventhub: parameters (subnetIds): '' expected type 'string', got unconvertible type '[]interface {}’
(hashicorp/terraform-provider-azurerm#2579 closed but doesn’t seem resolved)

Related work items: #47
  • Loading branch information
Anastasia Zolochevska committed Apr 17, 2019
1 parent 1ac75e4 commit 3414d20
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ crash.log
# .tfvars files are managed as part of configuration and so should be included in
# version control.
#
# example.tfvars
*.tfvars
!sample.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
Expand Down
103 changes: 103 additions & 0 deletions terraform/azure/eventhub.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

resource "azurerm_template_deployment" "eventhub" {
name = "${var.prefix}-eventhub"
resource_group_name = "${azurerm_resource_group.rg.name}"

template_body = <<DEPLOY
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"prefix": {
"type": "string",
"defaultValue": "[take(tolower(uniqueString(resourceGroup().id)), 12)]"
},
"subnetIds": {
"type": "array",
"defaultValue": ${var.subnetIds}
},
"messageRetentionInDays": {
"type": "string",
"defaultValue": "1"
},
"partitionCount": {
"type": "string",
"defaultValue": "4"
}
},
"variables": {
"ehNamespace": "[concat('eh-', parameters('prefix'))]",
"ehName": "telemetry",
"consumerGroups": [
"databricks"
]
},
"resources": [
{
"apiVersion": "2017-04-01",
"type": "Microsoft.EventHub/namespaces",
"name": "[variables('ehNamespace')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard",
"tier": "Standard"
},
"properties": {
"kafkaEnabled": true
}
},
{
"dependsOn": [
"[resourceId('Microsoft.EventHub/namespaces', variables('ehNamespace'))]"
],
"apiVersion": "2017-04-01",
"type": "Microsoft.EventHub/namespaces/eventhubs",
"name": "[concat(variables('ehNamespace'), '/', variables('ehName'))]",
"properties": {
"messageRetentionInDays": "[parameters('messageRetentionInDays')]",
"partitionCount": "[parameters('partitionCount')]"
}
},
{
"dependsOn": [
"[resourceId('Microsoft.EventHub/namespaces/eventHubs', variables('ehNamespace'), variables('ehName'))]"
],
"copy": {
"name": "cgCopy",
"count": "[length(variables('consumerGroups'))]"
},
"apiVersion": "2017-04-01",
"type": "Microsoft.EventHub/namespaces/eventhubs/consumergroups",
"name": "[concat(variables('ehNamespace'), '/', variables('ehName'), '/', variables('consumerGroups')[copyIndex()])]",
"properties": {}
},
{
"dependsOn": [
"[resourceId('Microsoft.EventHub/namespaces', variables('ehNamespace'))]"
],
"copy": {
"name": "networkRuleCopy",
"count": "[length(parameters('subnetIds'))]"
},
"apiVersion": "2018-01-01-preview",
"type": "Microsoft.EventHub/namespaces/virtualnetworkrules",
"name": "[concat(variables('ehNamespace'), '/vnet-', copyIndex())]",
"properties": {
"virtualNetworkSubnetId": "[parameters('subnetIds')[copyIndex()]]"
}
}
],
"outputs": {}
}
DEPLOY

# these key-value pairs are passed into the ARM Template's `parameters` block
parameters = {
"prefix" = "${var.prefix}",
"messageRetentionInDays" = "${var.messageRetentionInDays}",
"partitionCount" = "${var.partitionCount}"
}

deployment_mode = "Incremental"
}
10 changes: 10 additions & 0 deletions terraform/azure/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
provider "azurerm" {
version = "~>1.24"
}

resource "azurerm_resource_group" "rg" {
name = "${var.resource_group_name}"
location = "${var.location}"
}


6 changes: 6 additions & 0 deletions terraform/azure/sample.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource_group_name = "rg"
location = "westus2"
prefix = "foo"
partitionCount = 4
messageRetentionInDays = 1
subnetIds = "[\"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RG_NAME>/providers/Microsoft.Network/virtualNetworks/databricks-vnet/subnets/<SUBNET_NAME>\",\"/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RG_NAME>/providers/Microsoft.Network/virtualNetworks/databricks-vnet/subnets/<SUBNET_NAME>\"]"
30 changes: 30 additions & 0 deletions terraform/azure/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Common properties

variable "resource_group_name" {
description = "The name of the resource group."
}

variable "location" {
description = "The location/region where the resources are created."
}

variable "prefix" {
description = "The Prefix used for resources."
}


# EventHub

variable "subnetIds" {
description = "IDs of subnets. Event Hub Namespace will only accept connections from these subnets."
type = "string"
}

variable "partitionCount" {
description = "The number of partitions must be between 2 and 32. The partition count is not changeable."
type = "string"
}
variable "messageRetentionInDays" {
description = "The Event Hubs Standard tier allows message retention period for a maximum of seven days."
type = "string"
}

0 comments on commit 3414d20

Please sign in to comment.