From f4775c5bf65fad1ef92181123e117a53d73bc054 Mon Sep 17 00:00:00 2001 From: Kelly Gremban Date: Thu, 29 Sep 2022 14:52:07 -0700 Subject: [PATCH 1/4] new quickstart sample 101-device-provisioning-service --- .../101-device-provisioning-service/main.tf | 165 ++++++++++++++++++ .../outputs.tf | 11 ++ .../providers.tf | 18 ++ .../101-device-provisioning-service/readme.md | 28 +++ .../variables.tf | 29 +++ 5 files changed, 251 insertions(+) create mode 100644 quickstart/101-device-provisioning-service/main.tf create mode 100644 quickstart/101-device-provisioning-service/outputs.tf create mode 100644 quickstart/101-device-provisioning-service/providers.tf create mode 100644 quickstart/101-device-provisioning-service/readme.md create mode 100644 quickstart/101-device-provisioning-service/variables.tf 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 From 8a9f0ff0648ef380e2fe09b978febfdaa4da2292 Mon Sep 17 00:00:00 2001 From: Kelly Gremban Date: Mon, 3 Oct 2022 09:38:38 -0700 Subject: [PATCH 2/4] rename folder --- .../main.tf | 0 .../outputs.tf | 0 .../providers.tf | 0 .../readme.md | 0 .../variables.tf | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename quickstart/{101-device-provisioning-service => 201-iot-hub-with-device-provisioning-service}/main.tf (100%) rename quickstart/{101-device-provisioning-service => 201-iot-hub-with-device-provisioning-service}/outputs.tf (100%) rename quickstart/{101-device-provisioning-service => 201-iot-hub-with-device-provisioning-service}/providers.tf (100%) rename quickstart/{101-device-provisioning-service => 201-iot-hub-with-device-provisioning-service}/readme.md (100%) rename quickstart/{101-device-provisioning-service => 201-iot-hub-with-device-provisioning-service}/variables.tf (100%) diff --git a/quickstart/101-device-provisioning-service/main.tf b/quickstart/201-iot-hub-with-device-provisioning-service/main.tf similarity index 100% rename from quickstart/101-device-provisioning-service/main.tf rename to quickstart/201-iot-hub-with-device-provisioning-service/main.tf diff --git a/quickstart/101-device-provisioning-service/outputs.tf b/quickstart/201-iot-hub-with-device-provisioning-service/outputs.tf similarity index 100% rename from quickstart/101-device-provisioning-service/outputs.tf rename to quickstart/201-iot-hub-with-device-provisioning-service/outputs.tf diff --git a/quickstart/101-device-provisioning-service/providers.tf b/quickstart/201-iot-hub-with-device-provisioning-service/providers.tf similarity index 100% rename from quickstart/101-device-provisioning-service/providers.tf rename to quickstart/201-iot-hub-with-device-provisioning-service/providers.tf diff --git a/quickstart/101-device-provisioning-service/readme.md b/quickstart/201-iot-hub-with-device-provisioning-service/readme.md similarity index 100% rename from quickstart/101-device-provisioning-service/readme.md rename to quickstart/201-iot-hub-with-device-provisioning-service/readme.md diff --git a/quickstart/101-device-provisioning-service/variables.tf b/quickstart/201-iot-hub-with-device-provisioning-service/variables.tf similarity index 100% rename from quickstart/101-device-provisioning-service/variables.tf rename to quickstart/201-iot-hub-with-device-provisioning-service/variables.tf From db43ecd3a56c0fe5ac0b36503d3eef10e3502cb5 Mon Sep 17 00:00:00 2001 From: Kelly Gremban Date: Wed, 12 Oct 2022 11:43:29 -0700 Subject: [PATCH 3/4] lonegunmanb feedback --- .../main.tf | 14 +++++++------- .../providers.tf | 4 ++-- .../readme.md | 1 - .../variables.tf | 5 ----- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/quickstart/201-iot-hub-with-device-provisioning-service/main.tf b/quickstart/201-iot-hub-with-device-provisioning-service/main.tf index d362026c..1009e0d0 100644 --- a/quickstart/201-iot-hub-with-device-provisioning-service/main.tf +++ b/quickstart/201-iot-hub-with-device-provisioning-service/main.tf @@ -9,9 +9,9 @@ resource "azurerm_resource_group" "rg" { # Create storage account & container resource "random_string" "sa_name" { - length = 12 + length = 12 special = false - upper = false + upper = false } resource "azurerm_storage_account" "sa" { @@ -30,12 +30,12 @@ resource "azurerm_storage_container" "my_terraform_container" { # Create an Event Hub & Authorization Rule -resource "random_pet" "eventhubnamespace_name" { +resource "random_pet" "eventhub_namespace_name" { prefix = var.eventhub_namespace_name_prefix } resource "azurerm_eventhub_namespace" "namespace" { - name = random_pet.eventhubnamespace_name.id + name = random_pet.eventhub_namespace_name.id resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location sku = "Basic" @@ -82,7 +82,7 @@ resource "azurerm_iothub" "iothub" { 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}" + file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}" } endpoint { @@ -129,7 +129,7 @@ resource "azurerm_iothub" "iothub" { } #Create IoT Hub Access Policy -resource "azurerm_iothub_shared_access_policy" "hubaccesspolicy" { +resource "azurerm_iothub_shared_access_policy" "hub_access_policy" { name = "terraform-policy" resource_group_name = azurerm_resource_group.rg.name iothub_name = azurerm_iothub.iothub.name @@ -157,7 +157,7 @@ resource "azurerm_iothub_dps" "dps" { } linked_hub { - connection_string = azurerm_iothub_shared_access_policy.hubaccesspolicy.primary_connection_string + connection_string = azurerm_iothub_shared_access_policy.hub_access_policy.primary_connection_string location = azurerm_resource_group.rg.location allocation_weight = 150 apply_allocation_policy = true diff --git a/quickstart/201-iot-hub-with-device-provisioning-service/providers.tf b/quickstart/201-iot-hub-with-device-provisioning-service/providers.tf index 5343d826..fa0c5391 100644 --- a/quickstart/201-iot-hub-with-device-provisioning-service/providers.tf +++ b/quickstart/201-iot-hub-with-device-provisioning-service/providers.tf @@ -1,10 +1,10 @@ terraform { - required_version = ">=0.12" + required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~>2.0" + version = ">=3.0" } random = { source = "hashicorp/random" diff --git a/quickstart/201-iot-hub-with-device-provisioning-service/readme.md b/quickstart/201-iot-hub-with-device-provisioning-service/readme.md index e056e328..e3356c23 100644 --- a/quickstart/201-iot-hub-with-device-provisioning-service/readme.md +++ b/quickstart/201-iot-hub-with-device-provisioning-service/readme.md @@ -22,7 +22,6 @@ This template deploys an instance of [Device Provisioning Service](https://learn | ---- | ----------- | ------- | | `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/201-iot-hub-with-device-provisioning-service/variables.tf b/quickstart/201-iot-hub-with-device-provisioning-service/variables.tf index 5c424c59..3daf4b0e 100644 --- a/quickstart/201-iot-hub-with-device-provisioning-service/variables.tf +++ b/quickstart/201-iot-hub-with-device-provisioning-service/variables.tf @@ -8,11 +8,6 @@ variable "resource_group_name_prefix" { 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." From 24645b6398fff488b3f89bbaf7313b405559f9bb Mon Sep 17 00:00:00 2001 From: Kelly Gremban Date: Fri, 14 Oct 2022 11:15:49 -0700 Subject: [PATCH 4/4] Review fixes and updated readme --- .../201-iot-hub-with-device-provisioning-service/main.tf | 8 ++++---- .../readme.md | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/quickstart/201-iot-hub-with-device-provisioning-service/main.tf b/quickstart/201-iot-hub-with-device-provisioning-service/main.tf index 1009e0d0..a05a8eed 100644 --- a/quickstart/201-iot-hub-with-device-provisioning-service/main.tf +++ b/quickstart/201-iot-hub-with-device-provisioning-service/main.tf @@ -71,7 +71,7 @@ resource "azurerm_iothub" "iothub" { sku { name = "S1" - capacity = "1" + capacity = 1 } endpoint { @@ -134,8 +134,8 @@ resource "azurerm_iothub_shared_access_policy" "hub_access_policy" { resource_group_name = azurerm_resource_group.rg.name iothub_name = azurerm_iothub.iothub.name - registry_read = true - registry_write = true + registry_read = true + registry_write = true service_connect = true } @@ -153,7 +153,7 @@ resource "azurerm_iothub_dps" "dps" { sku { name = "S1" - capacity = "1" + capacity = 1 } linked_hub { diff --git a/quickstart/201-iot-hub-with-device-provisioning-service/readme.md b/quickstart/201-iot-hub-with-device-provisioning-service/readme.md index e3356c23..dcc911bb 100644 --- a/quickstart/201-iot-hub-with-device-provisioning-service/readme.md +++ b/quickstart/201-iot-hub-with-device-provisioning-service/readme.md @@ -1,6 +1,6 @@ -# Azure IoT Hub Device Provisioning Service +# Azure IoT Hub and IoT Hub Device Provisioning Service -This template deploys an instance of [Device Provisioning Service](https://learn.microsoft.com/azure/iot-dps/) on Azure. +This template deploys an instance of [Azure IoT Hub](https://learn.microsoft.com/azure/iot-hub/) and [IoT Hub Device Provisioning Service](https://learn.microsoft.com/azure/iot-dps/) on Azure. ## Terraform resource types