-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
188 lines (175 loc) · 5.14 KB
/
main.tf
File metadata and controls
188 lines (175 loc) · 5.14 KB
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
# centralus
# eastasia
# southeastasia
# eastus
# eastus2
# westus
# westus2
# northcentralus
# southcentralus
# westcentralus
# northeurope
# westeurope
# japaneast
# japanwest
# brazilsouth
# australiasoutheast
# australiaeast
# westindia
# southindia
# centralindia
# canadacentral
# canadaeast
# uksouth
# ukwest
# koreacentral
# koreasouth
# francecentral
# southafricanorth
# uaenorth
# australiacentral
# switzerlandnorth
# germanywestcentral
# norwayeast
# jioindiawest
# westus3
# qatarcentral
# swedencentral
# australiacentral2
provider "azurerm" {
features {}
}
# Create a resource group
resource "azurerm_resource_group" "rg" {
name = "${var.resource_prefix}-RG"
location = var.node_location
tags = {
"Environment" = "Dev"
"Team" = "DevOps"
}
}
# Create a VNet within the resource group
resource "azurerm_virtual_network" "vnet" {
name = "${var.resource_prefix}-vnet"
address_space = var.node_address_space
location = var.node_location
resource_group_name = azurerm_resource_group.rg.name
}
# Create a subnets withing the VNet
resource "azurerm_subnet" "subnet" {
name = "${var.resource_prefix}-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.node_address_prefix
}
# Create Public IP
resource "azurerm_public_ip" "public_ip" {
count = var.node_count
name = "${var.resource_prefix}-${format("%02d", count.index)}-publicIP"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
domain_name_label = "${var.resource_prefix}-${count.index}"
tags = {
"environment" = "test"
}
}
# Create network interface
resource "azurerm_network_interface" "nic" {
count = var.node_count
name = "${var.resource_prefix}-${format("%02d", count.index)}-NIC"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = element(azurerm_public_ip.public_ip.*.id, count.index)
}
}
# Creating NSG
resource "azurerm_network_security_group" "nsg" {
name = "${var.resource_prefix}-NSG"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
security_rule {
name = "Inbound1"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "Outbound1"
priority = 100
direction = "Outbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
"environment" = "Test"
}
}
# Subnet and NSG association
resource "azurerm_subnet_network_security_group_association" "subnet_nsg_association" {
subnet_id = azurerm_subnet.subnet.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
# Virtual machine creation - Linux
resource "azurerm_virtual_machine" "linux-vm" {
count = var.node_count
name = "${var.resource_prefix}-${format("%02d", count.index)}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [element(azurerm_network_interface.nic.*.id, count.index)]
vm_size = "Standard_B1s"
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk-${count.index}"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "StandardSSD_LRS"
disk_size_gb = 64
}
os_profile {
computer_name = "linuxhost"
admin_username = var.vm_username
admin_password = var.vm_password
}
os_profile_linux_config {
disable_password_authentication = false
}
tags = {
"environment" = "Test"
}
connection {
type = "ssh"
host = azurerm_public_ip.public_ip[count.index].ip_address
password = var.vm_password
user = var.vm_username
}
provisioner "file" {
source = "start.sh"
destination = "/home/${var.vm_username}/start.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/${var.vm_username}/start.sh",
"/home/${var.vm_username}/start.sh"
]
}
}