This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
1-create-vm.tf
196 lines (169 loc) · 5.53 KB
/
1-create-vm.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
# Configure the input variables. Replace each with your value.
variable "resourcegroupname" {
type = string
default = "myResourceGroup"
}
variable "regionname" {
type = string
default = "japanwest"
}
variable "prefix" {
type = string
default = "myGameBackend"
}
variable "loginusername" {
type = string
default = "azureuser"
}
variable "vmname" {
type = string
default = "myVirtualMachine"
}
variable "vmsize" {
type = string
default = "Standard_B1s"
}
variable "vmdatadisksize" {
type = string
default = "5"
}
variable "ubuntuosversion" {
type = string
default = "16.04-LTS"
}
variable "authenticationkey" {
type = string
default = "ssh-rsa AAAAB3Nzxxxxxxxxxx"
}
variable "addressprefix" {
type = string
default = "10.0.0.0/16"
}
variable "subnetprefix" {
type = string
default = "10.0.0.0/24"
}
# Create a resource group if it doesn’t exist
resource "azurerm_resource_group" "main" {
name = var.resourcegroupname
location = var.regionname
}
# Create virtual network
resource "azurerm_virtual_network" "image" {
name = "${var.vmname}VNET"
address_space = [var.addressprefix]
location = var.regionname
resource_group_name = "${azurerm_resource_group.main.name}"
}
# Create subnet
resource "azurerm_subnet" "image" {
name = "${var.vmname}Subnet"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.image.name}"
address_prefix = var.subnetprefix
}
# Create public IPs
resource "azurerm_public_ip" "image" {
name = "${var.vmname}PublicIP"
location = var.regionname
resource_group_name = "${azurerm_resource_group.main.name}"
allocation_method = "Dynamic"
}
# Create Network Security Group and rule
resource "azurerm_network_security_group" "image" {
name = "${var.vmname}NSG"
location = var.regionname
resource_group_name = "${azurerm_resource_group.main.name}"
security_rule {
name = "HTTP"
priority = 900
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "HTTPS"
priority = 901
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "SSH"
priority = 902
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Create network interface
resource "azurerm_network_interface" "image" {
name = "${var.vmname}NIC"
location = var.regionname
resource_group_name = "${azurerm_resource_group.main.name}"
network_security_group_id = "${azurerm_network_security_group.image.id}"
ip_configuration {
name = "ipconfig${var.vmname}"
subnet_id = "${azurerm_subnet.image.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.image.id}"
}
}
# Create virtual machine
resource "azurerm_virtual_machine" "image" {
name = var.vmname
location = var.regionname
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.image.id}"]
vm_size = var.vmsize
storage_os_disk {
name = "${var.vmname}OSDisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = var.ubuntuosversion
version = "latest"
}
os_profile {
computer_name = var.vmname
admin_username = var.loginusername
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/${var.loginusername}/.ssh/authorized_keys"
key_data = var.authenticationkey
}
}
}
# Create managed disk
resource "azurerm_managed_disk" "data" {
name = "${var.vmname}DataDisk1"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = var.vmdatadisksize
}
resource "azurerm_virtual_machine_data_disk_attachment" "data" {
managed_disk_id = "${azurerm_managed_disk.data.id}"
virtual_machine_id = "${azurerm_virtual_machine.image.id}"
lun = "10"
caching = "ReadWrite"
}