Skip to content

Commit f4775c5

Browse files
committed
new quickstart sample 101-device-provisioning-service
1 parent 467e0a6 commit f4775c5

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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" "eventhubnamespace_name" {
34+
prefix = var.eventhub_namespace_name_prefix
35+
}
36+
37+
resource "azurerm_eventhub_namespace" "namespace" {
38+
name = random_pet.eventhubnamespace_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" "hubaccesspolicy" {
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.hubaccesspolicy.primary_connection_string
161+
location = azurerm_resource_group.rg.location
162+
allocation_weight = 150
163+
apply_allocation_policy = true
164+
}
165+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "azurerm_iothub_name" {
2+
value = azurerm_iothub.iothub.name
3+
}
4+
5+
output "azurerm_iothub_dps_name" {
6+
value = azurerm_iothub_dps.dps.name
7+
}
8+
9+
output "resource_group_name" {
10+
value = azurerm_resource_group.rg.name
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_version = ">=0.12"
3+
4+
required_providers {
5+
azurerm = {
6+
source = "hashicorp/azurerm"
7+
version = "~>2.0"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = "~>3.0"
12+
}
13+
}
14+
}
15+
16+
provider "azurerm" {
17+
features {}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Azure IoT Hub Device Provisioning Service
2+
3+
This template deploys an instance of [Device Provisioning Service](https://learn.microsoft.com/azure/iot-dps/) on Azure.
4+
5+
## Terraform resource types
6+
7+
* [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet)
8+
* [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string)
9+
* [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group)
10+
* [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account)
11+
* [azurerm_storage_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_container)
12+
* [azurerm_eventhub_namespace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/eventhub_namespace)
13+
* [azurerm_eventhub](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/eventhub)
14+
* [azurerm_eventhub_authorization_rule](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/eventhub_authorization_rule)
15+
* [azurerm_iothub](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub)
16+
* [azurerm_iothub_shared_access_policy](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub_shared_access_policy)
17+
* [azurerm_iothub_dps](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub_dps)
18+
19+
## Variables
20+
21+
| Name | Description | Default |
22+
| ---- | ----------- | ------- |
23+
| `resource_group_location` | Location of the resource group. | `eastus` |
24+
| `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` |
25+
| `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` |
26+
| `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` |
27+
| `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` |
28+
| `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` |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "resource_group_location" {
2+
default = "eastus"
3+
description = "Location of the resource group."
4+
}
5+
6+
variable "resource_group_name_prefix" {
7+
default = "rg"
8+
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
9+
}
10+
11+
variable "storage_account_name_prefix" {
12+
default = "sa"
13+
description = "Prefix of the storage account name that's combined with a random ID so name is unique in your Azure subscription."
14+
}
15+
16+
variable "eventhub_namespace_name_prefix" {
17+
default = "namespace"
18+
description = "Prefix of the event hub namespace name that's combined with a random ID so name is unique in your Azure subscription."
19+
}
20+
21+
variable "iothub_name_prefix" {
22+
default = "iothub"
23+
description = "Prefix of the iot hub name that's combined with a random ID so name is unique in your Azure subscription."
24+
}
25+
26+
variable "dps_name_prefix" {
27+
default = "dps"
28+
description = "Prefix of the dps name that's combined with a random ID so name is unique in your Azure subscription."
29+
}

0 commit comments

Comments
 (0)