-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstir.tf
182 lines (153 loc) · 5.25 KB
/
stir.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
# Create a resource group if it doesn’t exist
resource "azurerm_resource_group" "mystirgroup" {
name = "stirGroup"
location = "${var.location}"
tags {
environment = "STIR Demo"
}
}
# Create virtual network
resource "azurerm_virtual_network" "mystirnetwork" {
name = "stirVnet"
address_space = ["10.0.0.0/16"]
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.mystirgroup.name}"
tags {
environment = "STIR Demo"
}
}
# Create subnet
resource "azurerm_subnet" "mystirsubnet" {
name = "stirSubnet"
resource_group_name = "${azurerm_resource_group.mystirgroup.name}"
virtual_network_name = "${azurerm_virtual_network.mystirnetwork.name}"
address_prefix = "10.0.1.0/24"
}
# Create public IPs
resource "azurerm_public_ip" "mystirpublicip" {
name = "stirPublicIP"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.mystirgroup.name}"
public_ip_address_allocation = "dynamic"
tags {
environment = "STIR Demo"
}
}
# Create Network Security Group and rule
resource "azurerm_network_security_group" "mystirsg" {
name = "stirNetworkSecurityGroup"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.mystirgroup.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 = "STIR Demo"
}
}
# Create network interface
resource "azurerm_network_interface" "mystirnic" {
name = "stirNIC"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.mystirgroup.name}"
network_security_group_id = "${azurerm_network_security_group.mystirsg.id}"
ip_configuration {
name = "stirNicConfiguration"
subnet_id = "${azurerm_subnet.mystirsubnet.id}"
private_ip_address_allocation = "dynamic"
public_ip_address_id = "${azurerm_public_ip.mystirpublicip.id}"
}
tags {
environment = "STIR Demo"
}
}
# 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.mystirgroup.name}"
}
byte_length = 8
}
# Create storage account for boot diagnostics
resource "azurerm_storage_account" "mystirstorageaccount" {
name = "diag${random_id.randomId.hex}"
resource_group_name = "${azurerm_resource_group.mystirgroup.name}"
location = "${var.location}"
account_tier = "Standard"
account_replication_type = "LRS"
tags {
environment = "STIR Demo"
}
}
# Create virtual machine
resource "azurerm_virtual_machine" "mystirvm" {
name = "stirVM"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.mystirgroup.name}"
network_interface_ids = ["${azurerm_network_interface.mystirnic.id}"]
vm_size = "${var.vm_size}"
storage_os_disk {
name = "stirOsDisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
delete_os_disk_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
version = "latest"
}
os_profile {
computer_name = "stirvm"
admin_username = "${var.vm_username}"
admin_password = "${var.vm_password}"
}
os_profile_linux_config {
disable_password_authentication = false
}
boot_diagnostics {
enabled = "true"
storage_uri = "${azurerm_storage_account.mystirstorageaccount.primary_blob_endpoint}"
}
provisioner "file" {
connection {
user = "${var.vm_username}"
password = "${var.vm_password}"
}
source = "provision.sh"
destination = "/home/${var.vm_username}/provision.sh"
}
provisioner "file" {
connection {
user = "${var.vm_username}"
password = "${var.vm_password}"
}
source = "install_prerequisites.sh"
destination = "/home/${var.vm_username}/install_prerequisites.sh"
}
provisioner "remote-exec" {
connection {
user = "${var.vm_username}"
password = "${var.vm_password}"
}
inline = [
"sudo do-release-upgrade -f DistUpgradeViewNonInteractive",
"sudo bash ~/install_prerequisites.sh",
"bash ~/provision.sh"
]
}
tags {
environment = "STIR Demo"
}
}