/
terraform_azure.tf
231 lines (195 loc) · 6.63 KB
/
terraform_azure.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# This script is based on this base script from the Azure docs:
# https://docs.microsoft.com/en-us/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure
# Configure the Microsoft Azure Provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
}
variable "rsa_key_location" {
type = string
description = "Path to your private SSH key"
default = "~/.ssh/id_rsa"
}
# Create a resource group if it doesn't exist
resource "azurerm_resource_group" "montithingsgroup" {
name = "montithingsResourceGroup"
location = "westeurope"
tags = {
environment = "MontiThings"
}
}
# Create virtual network
resource "azurerm_virtual_network" "montithingsnetwork" {
name = "myVnet"
address_space = ["10.0.0.0/16"]
location = "westeurope"
resource_group_name = azurerm_resource_group.montithingsgroup.name
tags = {
environment = "MontiThings"
}
}
# Create subnet
resource "azurerm_subnet" "montithingssubnet" {
name = "mySubnet"
resource_group_name = azurerm_resource_group.montithingsgroup.name
virtual_network_name = azurerm_virtual_network.montithingsnetwork.name
address_prefixes = ["10.0.1.0/24"]
}
# Create public IPs
resource "azurerm_public_ip" "montithingspublicip" {
name = "myPublicIP"
location = "westeurope"
resource_group_name = azurerm_resource_group.montithingsgroup.name
allocation_method = "Dynamic"
tags = {
environment = "MontiThings"
}
}
# Create Network Security Group and rule
resource "azurerm_network_security_group" "montithingsnsg" {
name = "myNetworkSecurityGroup"
location = "westeurope"
resource_group_name = azurerm_resource_group.montithingsgroup.name
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "MontiThings"
}
}
# Create network interface
resource "azurerm_network_interface" "montithingsnic" {
name = "myNIC"
location = "westeurope"
resource_group_name = azurerm_resource_group.montithingsgroup.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.montithingssubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.montithingspublicip.id
}
tags = {
environment = "MontiThings"
}
}
# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
network_interface_id = azurerm_network_interface.montithingsnic.id
network_security_group_id = azurerm_network_security_group.montithingsnsg.id
}
# Generate random text for a unique storage account name
resource "random_id" "randomId" {
keepers = {
# Generate a new ID only when a new resource group is defined
resource_group = azurerm_resource_group.montithingsgroup.name
}
byte_length = 8
}
# Create storage account for boot diagnostics
resource "azurerm_storage_account" "mystorageaccount" {
name = "diag${random_id.randomId.hex}"
resource_group_name = azurerm_resource_group.montithingsgroup.name
location = "westeurope"
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
environment = "MontiThings"
}
}
# Create (and display) an SSH key
resource "tls_private_key" "example_ssh" {
algorithm = "RSA"
rsa_bits = 4096
}
output "tls_private_key" {
value = tls_private_key.example_ssh.private_key_pem
sensitive = true
}
# Create virtual machine
resource "azurerm_linux_virtual_machine" "montithingsvm" {
name = "montithings"
location = "westeurope"
resource_group_name = azurerm_resource_group.montithingsgroup.name
network_interface_ids = [azurerm_network_interface.montithingsnic.id]
size = "Standard_B2s"
os_disk {
name = "myOsDisk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
computer_name = "montithings"
admin_username = "azureuser"
disable_password_authentication = true
admin_ssh_key {
username = "azureuser"
public_key = file("${var.rsa_key_location}.pub")
}
boot_diagnostics {
storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint
}
provisioner "remote-exec" {
inline = [
"git clone https://github.com/monticore/montithings",
"cd montithings",
"sudo cp docs/ascii-banner.txt /etc/motd",
"./installLinux.sh"
]
connection {
type = "ssh"
user = azurerm_linux_virtual_machine.montithingsvm.admin_username
private_key = file(var.rsa_key_location)
host = azurerm_linux_virtual_machine.montithingsvm.public_ip_address
}
}
tags = {
environment = "MontiThings"
}
}
resource "azurerm_container_group" "montithingscontainergroup" {
name = "montithings-containers"
location = azurerm_resource_group.montithingsgroup.location
resource_group_name = azurerm_resource_group.montithingsgroup.name
ip_address_type = "public"
os_type = "Linux"
dns_name_label = random_id.randomId.hex
container {
name = "hivemq-broker"
image = "hivemq/hivemq-ce"
cpu = "0.5"
memory = "1.5"
ports {
port = 1883
protocol = "TCP"
}
}
tags = {
environment = "MontiThings"
}
}
output "vm_ip_address" {
value = azurerm_linux_virtual_machine.montithingsvm.public_ip_address
}
output "mqtt_broker_ip_address" {
value = azurerm_container_group.montithingscontainergroup.ip_address
}