|
| 1 | +resource "random_pet" "rg_name" { |
| 2 | + prefix = var.resource_group_name_prefix |
| 3 | +} |
| 4 | + |
| 5 | +resource "azurerm_resource_group" "rg" { |
| 6 | + location = var.resource_group_location |
| 7 | + name = random_pet.rg_name.id |
| 8 | +} |
| 9 | + |
| 10 | +# Create storage account & container |
| 11 | +resource "random_string" "sa_name" { |
| 12 | + length = 12 |
| 13 | + special = false |
| 14 | + upper = false |
| 15 | +} |
| 16 | + |
| 17 | +resource "azurerm_storage_account" "sa" { |
| 18 | + name = random_string.sa_name.id |
| 19 | + resource_group_name = azurerm_resource_group.rg.name |
| 20 | + location = azurerm_resource_group.rg.location |
| 21 | + account_tier = "Standard" |
| 22 | + account_replication_type = "LRS" |
| 23 | +} |
| 24 | + |
| 25 | +resource "azurerm_storage_container" "my_terraform_container" { |
| 26 | + name = "mycontainer" |
| 27 | + storage_account_name = azurerm_storage_account.sa.name |
| 28 | + container_access_type = "private" |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +# Create an Event Hub & Authorization Rule |
| 33 | +resource "random_pet" "eventhub_namespace_name" { |
| 34 | + prefix = var.eventhub_namespace_name_prefix |
| 35 | +} |
| 36 | + |
| 37 | +resource "azurerm_eventhub_namespace" "namespace" { |
| 38 | + name = random_pet.eventhub_namespace_name.id |
| 39 | + resource_group_name = azurerm_resource_group.rg.name |
| 40 | + location = azurerm_resource_group.rg.location |
| 41 | + sku = "Basic" |
| 42 | +} |
| 43 | + |
| 44 | +resource "azurerm_eventhub" "my_terraform_eventhub" { |
| 45 | + name = "myEventHub" |
| 46 | + resource_group_name = azurerm_resource_group.rg.name |
| 47 | + namespace_name = azurerm_eventhub_namespace.namespace.name |
| 48 | + partition_count = 2 |
| 49 | + message_retention = 1 |
| 50 | +} |
| 51 | + |
| 52 | +resource "azurerm_eventhub_authorization_rule" "my_terraform_authorization_rule" { |
| 53 | + resource_group_name = azurerm_resource_group.rg.name |
| 54 | + namespace_name = azurerm_eventhub_namespace.namespace.name |
| 55 | + eventhub_name = azurerm_eventhub.my_terraform_eventhub.name |
| 56 | + name = "acctest" |
| 57 | + send = true |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +# Create an IoT Hub |
| 62 | +resource "random_pet" "iothub_name" { |
| 63 | + prefix = var.iothub_name_prefix |
| 64 | + length = 1 |
| 65 | +} |
| 66 | + |
| 67 | +resource "azurerm_iothub" "iothub" { |
| 68 | + name = random_pet.iothub_name.id |
| 69 | + resource_group_name = azurerm_resource_group.rg.name |
| 70 | + location = azurerm_resource_group.rg.location |
| 71 | + |
| 72 | + sku { |
| 73 | + name = "S1" |
| 74 | + capacity = 1 |
| 75 | + } |
| 76 | + |
| 77 | + endpoint { |
| 78 | + type = "AzureIotHub.StorageContainer" |
| 79 | + connection_string = azurerm_storage_account.sa.primary_blob_connection_string |
| 80 | + name = "export" |
| 81 | + batch_frequency_in_seconds = 60 |
| 82 | + max_chunk_size_in_bytes = 10485760 |
| 83 | + container_name = azurerm_storage_container.my_terraform_container.name |
| 84 | + encoding = "Avro" |
| 85 | + file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}" |
| 86 | + } |
| 87 | + |
| 88 | + endpoint { |
| 89 | + type = "AzureIotHub.EventHub" |
| 90 | + connection_string = azurerm_eventhub_authorization_rule.my_terraform_authorization_rule.primary_connection_string |
| 91 | + name = "export2" |
| 92 | + } |
| 93 | + |
| 94 | + route { |
| 95 | + name = "export" |
| 96 | + source = "DeviceMessages" |
| 97 | + condition = "true" |
| 98 | + endpoint_names = ["export"] |
| 99 | + enabled = true |
| 100 | + } |
| 101 | + |
| 102 | + route { |
| 103 | + name = "export2" |
| 104 | + source = "DeviceMessages" |
| 105 | + condition = "true" |
| 106 | + endpoint_names = ["export2"] |
| 107 | + enabled = true |
| 108 | + } |
| 109 | + |
| 110 | + enrichment { |
| 111 | + key = "tenant" |
| 112 | + value = "$twin.tags.Tenant" |
| 113 | + endpoint_names = ["export", "export2"] |
| 114 | + } |
| 115 | + |
| 116 | + cloud_to_device { |
| 117 | + max_delivery_count = 30 |
| 118 | + default_ttl = "PT1H" |
| 119 | + feedback { |
| 120 | + time_to_live = "PT1H10M" |
| 121 | + max_delivery_count = 15 |
| 122 | + lock_duration = "PT30S" |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + tags = { |
| 127 | + purpose = "testing" |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +#Create IoT Hub Access Policy |
| 132 | +resource "azurerm_iothub_shared_access_policy" "hub_access_policy" { |
| 133 | + name = "terraform-policy" |
| 134 | + resource_group_name = azurerm_resource_group.rg.name |
| 135 | + iothub_name = azurerm_iothub.iothub.name |
| 136 | + |
| 137 | + registry_read = true |
| 138 | + registry_write = true |
| 139 | + service_connect = true |
| 140 | +} |
| 141 | + |
| 142 | +# Create IoT Hub DPS |
| 143 | +resource "random_pet" "dps_name" { |
| 144 | + prefix = var.dps_name_prefix |
| 145 | + length = 1 |
| 146 | +} |
| 147 | + |
| 148 | +resource "azurerm_iothub_dps" "dps" { |
| 149 | + name = random_pet.dps_name.id |
| 150 | + resource_group_name = azurerm_resource_group.rg.name |
| 151 | + location = azurerm_resource_group.rg.location |
| 152 | + allocation_policy = "Hashed" |
| 153 | + |
| 154 | + sku { |
| 155 | + name = "S1" |
| 156 | + capacity = 1 |
| 157 | + } |
| 158 | + |
| 159 | + linked_hub { |
| 160 | + connection_string = azurerm_iothub_shared_access_policy.hub_access_policy.primary_connection_string |
| 161 | + location = azurerm_resource_group.rg.location |
| 162 | + allocation_weight = 150 |
| 163 | + apply_allocation_policy = true |
| 164 | + } |
| 165 | +} |
0 commit comments