-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
65 lines (57 loc) · 1.82 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
variable "public_ip" {}
resource "azurerm_resource_group" "rg" {
name = "${var.name}-rg"
location = var.location
}
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub
resource "azurerm_iothub" "iothub" {
name = "${var.name}-iot-hub"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku {
# Free Tier
# https://azure.microsoft.com/en-us/pricing/details/iot-hub/
name = "F1"
capacity = "1"
}
public_network_access_enabled = true
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub#network_rule_set
# portal / IoT Hub / Networking / Public network access: Selected IP ranges
network_rule_set {
default_action = "Deny"
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/iothub#ip_rule
ip_rule {
name = "myIP"
ip_mask = var.public_ip
action = "Allow"
}
}
tags = {
purpose = "demo"
}
}
resource "null_resource" "iot_device" {
triggers = {
iot_hub_name = azurerm_iothub.iothub.name
iot_hub_device = var.iot_device
}
provisioner "local-exec" {
command = <<EOF
echo ${self.triggers.iot_hub_name}
echo ${self.triggers.iot_hub_device}
export IOT_HUB_NAME=${self.triggers.iot_hub_name}
export IOT_HUB_DEVICE_NAME=${self.triggers.iot_hub_device}
bash ${path.module}/scripts/device.sh create
EOF
}
provisioner "local-exec" {
when = destroy
command = <<EOF
echo ${self.triggers.iot_hub_name}
echo ${self.triggers.iot_hub_device}
export IOT_HUB_NAME=${self.triggers.iot_hub_name}
export IOT_HUB_DEVICE_NAME=${self.triggers.iot_hub_device}
bash ${path.module}/scripts/device.sh delete
EOF
}
}