-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
138 lines (120 loc) · 3.57 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
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
#Azure Provider 2.16
provider "azurerm" {
version = "=2.16"
features {}
}
#local for dynamic use to config windows or linux properties
locals {
linux = var.os_type == "Linux" ? { dummy_create = true } : {}
windows = var.os_type == "Windows" ? { dummy_create = true } : {}
}
#Resource
resource "azurerm_resource_group" "main" {
name = "example"
location = "West Europe"
}
#Vnet
resource "azurerm_virtual_network" "main" {
name = "vnet-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
}
#Subnet
resource "azurerm_subnet" "Subnetinternal" {
name = "internal"
resource_group_name = azurerm_resource_group.main.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
}
#NIC
resource "azurerm_network_interface" "main" {
name = "nic-network"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.Subnetinternal.id
private_ip_address_allocation = "Dynamic"
private_ip_address = "10.0.2.5"
public_ip_address_id = azurerm_public_ip.main.id
}
}
#VM
resource "azurerm_virtual_machine" "main" {
name = "AutoOS-VM"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
network_interface_ids = [azurerm_network_interface.main.id]
vm_size = "Standard_DS1_v2"
# Uncomment this line to delete the OS disk automatically when deleting the VM
# delete_os_disk_on_termination = true
# Uncomment this line to delete the data disks automatically when deleting the VM
# delete_data_disks_on_termination = true
#Windows image
dynamic "storage_image_reference" {
for_each = local.windows
content {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
#linux image
dynamic "storage_image_reference" {
for_each = local.linux
content {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
}
#OS Disk
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
os_type = var.os_type
}
#OS Profile
os_profile {
computer_name = "vmlinux"
admin_username = "AdminUser"
admin_password = "Password@123456"
}
#linux [Note: Setting disable_password_authentication to false, because don't want to use SSH Authentication]
dynamic "os_profile_linux_config" {
for_each = local.linux
content {
disable_password_authentication = false
ssh_keys {
path = "/home/AdminUser/.ssh/authorized_keys"
key_data = ""
}
}
}
#Windows
dynamic "os_profile_windows_config" {
for_each = local.windows
content {
enable_automatic_upgrades = true
provision_vm_agent = true
}
}
tags = {
Vivek = "Auto vm os"
}
}
//Public Ip
resource "azurerm_public_ip" "main" {
name = "PublicIp-netwowk"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
allocation_method = "Static"
tags = {
Vivek = "public_ip"
}
}