diff --git a/quickstart/101-device-provisioning-service/main.tf b/quickstart/101-device-provisioning-service/main.tf new file mode 100644 index 00000000..d362026c --- /dev/null +++ b/quickstart/101-device-provisioning-service/main.tf @@ -0,0 +1,165 @@ +resource "random_pet" "rg_name" { + prefix = var.resource_group_name_prefix +} + +resource "azurerm_resource_group" "rg" { + location = var.resource_group_location + name = random_pet.rg_name.id +} + +# Create storage account & container +resource "random_string" "sa_name" { + length = 12 + special = false + upper = false +} + +resource "azurerm_storage_account" "sa" { + name = random_string.sa_name.id + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "azurerm_storage_container" "my_terraform_container" { + name = "mycontainer" + storage_account_name = azurerm_storage_account.sa.name + container_access_type = "private" +} + + +# Create an Event Hub & Authorization Rule +resource "random_pet" "eventhubnamespace_name" { + prefix = var.eventhub_namespace_name_prefix +} + +resource "azurerm_eventhub_namespace" "namespace" { + name = random_pet.eventhubnamespace_name.id + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + sku = "Basic" +} + +resource "azurerm_eventhub" "my_terraform_eventhub" { + name = "myEventHub" + resource_group_name = azurerm_resource_group.rg.name + namespace_name = azurerm_eventhub_namespace.namespace.name + partition_count = 2 + message_retention = 1 +} + +resource "azurerm_eventhub_authorization_rule" "my_terraform_authorization_rule" { + resource_group_name = azurerm_resource_group.rg.name + namespace_name = azurerm_eventhub_namespace.namespace.name + eventhub_name = azurerm_eventhub.my_terraform_eventhub.name + name = "acctest" + send = true +} + + +# Create an IoT Hub +resource "random_pet" "iothub_name" { + prefix = var.iothub_name_prefix + length = 1 +} + +resource "azurerm_iothub" "iothub" { + name = random_pet.iothub_name.id + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + + sku { + name = "S1" + capacity = "1" + } + + endpoint { + type = "AzureIotHub.StorageContainer" + connection_string = azurerm_storage_account.sa.primary_blob_connection_string + name = "export" + batch_frequency_in_seconds = 60 + max_chunk_size_in_bytes = 10485760 + container_name = azurerm_storage_container.my_terraform_container.name + encoding = "Avro" + file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}" + } + + endpoint { + type = "AzureIotHub.EventHub" + connection_string = azurerm_eventhub_authorization_rule.my_terraform_authorization_rule.primary_connection_string + name = "export2" + } + + route { + name = "export" + source = "DeviceMessages" + condition = "true" + endpoint_names = ["export"] + enabled = true + } + + route { + name = "export2" + source = "DeviceMessages" + condition = "true" + endpoint_names = ["export2"] + enabled = true + } + + enrichment { + key = "tenant" + value = "$twin.tags.Tenant" + endpoint_names = ["export", "export2"] + } + + cloud_to_device { + max_delivery_count = 30 + default_ttl = "PT1H" + feedback { + time_to_live = "PT1H10M" + max_delivery_count = 15 + lock_duration = "PT30S" + } + } + + tags = { + purpose = "testing" + } +} + +#Create IoT Hub Access Policy +resource "azurerm_iothub_shared_access_policy" "hubaccesspolicy" { + name = "terraform-policy" + resource_group_name = azurerm_resource_group.rg.name + iothub_name = azurerm_iothub.iothub.name + + registry_read = true + registry_write = true + service_connect = true +} + +# Create IoT Hub DPS +resource "random_pet" "dps_name" { + prefix = var.dps_name_prefix + length = 1 +} + +resource "azurerm_iothub_dps" "dps" { + name = random_pet.dps_name.id + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + allocation_policy = "Hashed" + + sku { + name = "S1" + capacity = "1" + } + + linked_hub { + connection_string = azurerm_iothub_shared_access_policy.hubaccesspolicy.primary_connection_string + location = azurerm_resource_group.rg.location + allocation_weight = 150 + apply_allocation_policy = true + } +} diff --git a/quickstart/101-device-provisioning-service/outputs.tf b/quickstart/101-device-provisioning-service/outputs.tf new file mode 100644 index 00000000..7f98955e --- /dev/null +++ b/quickstart/101-device-provisioning-service/outputs.tf @@ -0,0 +1,11 @@ +output "azurerm_iothub_name" { + value = azurerm_iothub.iothub.name +} + +output "azurerm_iothub_dps_name" { + value = azurerm_iothub_dps.dps.name +} + +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} \ No newline at end of file diff --git a/quickstart/101-device-provisioning-service/providers.tf b/quickstart/101-device-provisioning-service/providers.tf new file mode 100644 index 00000000..5343d826 --- /dev/null +++ b/quickstart/101-device-provisioning-service/providers.tf @@ -0,0 +1,18 @@ +terraform { + required_version = ">=0.12" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>2.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-device-provisioning-service/readme.md b/quickstart/101-device-provisioning-service/readme.md new file mode 100644 index 00000000..e056e328 --- /dev/null +++ b/quickstart/101-device-provisioning-service/readme.md @@ -0,0 +1,28 @@ +# Azure IoT Hub Device Provisioning Service + +This template deploys an instance of [Device Provisioning Service](https://learn.microsoft.com/azure/iot-dps/) on Azure. + +## Terraform resource types + +* [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) +* [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) +* [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +* [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account) +* [azurerm_storage_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_container) +* [azurerm_eventhub_namespace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/eventhub_namespace) +* [azurerm_eventhub](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/eventhub) +* [azurerm_eventhub_authorization_rule](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/eventhub_authorization_rule) +* [azurerm_iothub](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub) +* [azurerm_iothub_shared_access_policy](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub_shared_access_policy) +* [azurerm_iothub_dps](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub_dps) + +## Variables + +| Name | Description | Default | +| ---- | ----------- | ------- | +| `resource_group_location` | Location of the resource group. | `eastus` | +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so the name is unique in your Azure subscription. | `rg` | +| `storage_account_name_prefix` | Prefix of the storage account name that's combined with a random ID so name is unique in your Azure subscription. | `sa` | +| `eventhub_namespace_name_prefix` | Prefix of the event hub namespace name that's combined with a random ID so the name is unique in your Azure subscription. | `namespace` | +| `iothub_name_prefix` | Prefix of the IoT hub name that's combined with a random ID so the name is unique in your Azure subscription. | `iothub` | +| `dps_name_prefix` | Prefix of the dps name that's combined with a random ID so the name is unique in your Azure subscription. | `dps` | diff --git a/quickstart/101-device-provisioning-service/variables.tf b/quickstart/101-device-provisioning-service/variables.tf new file mode 100644 index 00000000..5c424c59 --- /dev/null +++ b/quickstart/101-device-provisioning-service/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + default = "rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "storage_account_name_prefix" { + default = "sa" + description = "Prefix of the storage account name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "eventhub_namespace_name_prefix" { + default = "namespace" + description = "Prefix of the event hub namespace name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "iothub_name_prefix" { + default = "iothub" + description = "Prefix of the iot hub name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "dps_name_prefix" { + default = "dps" + description = "Prefix of the dps name that's combined with a random ID so name is unique in your Azure subscription." +} \ No newline at end of file