-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
163 lines (138 loc) · 5.11 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
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
# We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.75.0"
}
}
}
provider "azurerm" {
features {}
# More information on the authentication methods supported by
# the AzureRM Provider can be found here:
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
subscription_id = var.subscription_id
client_id = var.client_id
client_secret = var.client_secret
tenant_id = var.tenant_id
}
data "azurerm_resource_group" "hcmxexample" {
name = var.resource_group_name
}
resource "azurerm_public_ip" "hcmxexample" {
name = var.vm_name
resource_group_name = data.azurerm_resource_group.hcmxexample.name
location = var.location
allocation_method = "Dynamic"
domain_name_label = var.domain_name_label
}
data "azurerm_virtual_network" "hcmxexample" {
name = var.virtual_network
resource_group_name = data.azurerm_resource_group.hcmxexample.name
}
data "azurerm_subnet" "hcmxexample" {
name = var.subnet
resource_group_name = data.azurerm_resource_group.hcmxexample.name
virtual_network_name = data.azurerm_virtual_network.hcmxexample.name
}
resource "azurerm_network_interface" "hcmxexample" {
name = var.vm_name
location = var.location
resource_group_name = data.azurerm_resource_group.hcmxexample.name
ip_configuration {
name = var.vm_name
subnet_id = data.azurerm_subnet.hcmxexample.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.hcmxexample.id
}
}
resource "azurerm_linux_virtual_machine" "hcmxexample" {
count = var.os_type=="linux" ? 1 : 0
name = var.vm_name
resource_group_name = data.azurerm_resource_group.hcmxexample.name
location = var.location
size = var.vm_size
admin_username = var.vm_username
admin_password = var.password
disable_password_authentication = false
network_interface_ids = [
azurerm_network_interface.hcmxexample.id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = var.type_of_storage
}
source_image_reference {
publisher = var.publisher
offer = var.offer
sku = var.sku
version = var.os_version
}
}
resource "azurerm_windows_virtual_machine" "hcmxexample" {
count = var.os_type=="windows" ? 1 : 0
name = var.vm_name
resource_group_name = data.azurerm_resource_group.hcmxexample.name
location = var.location
size = var.vm_size
admin_username = var.vm_username
admin_password = var.password
network_interface_ids = [
azurerm_network_interface.hcmxexample.id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = var.type_of_storage
}
source_image_reference {
publisher = var.publisher
offer = var.offer
sku = var.sku
version = var.os_version
}
}
resource "azurerm_managed_disk" "hcmxexample" {
name = "${var.vm_name}-disk"
location = var.location
resource_group_name = data.azurerm_resource_group.hcmxexample.name
storage_account_type = var.type_of_storage
create_option = "Empty"
disk_size_gb = var.disk_size
}
resource "azurerm_virtual_machine_data_disk_attachment" "hcmxexample" {
managed_disk_id = azurerm_managed_disk.hcmxexample.id
virtual_machine_id = var.os_type=="linux" ? azurerm_linux_virtual_machine.hcmxexample[0].id : azurerm_windows_virtual_machine.hcmxexample[0].id
lun = "10"
caching = "ReadWrite"
}
data "azurerm_public_ip" "hcmxexample" {
name = var.vm_name
resource_group_name = var.os_type=="linux" ? azurerm_linux_virtual_machine.hcmxexample[0].resource_group_name : azurerm_windows_virtual_machine.hcmxexample[0].resource_group_name
}
data "azurerm_network_interface" "hcmxexample" {
name = var.vm_name
resource_group_name = var.os_type=="linux" ? azurerm_linux_virtual_machine.hcmxexample[0].resource_group_name : azurerm_windows_virtual_machine.hcmxexample[0].resource_group_name
}
output "public_ip_address" {
value = data.azurerm_public_ip.hcmxexample.ip_address
}
output "network_interface_name" {
value = azurerm_network_interface.hcmxexample.name
}
output "private_ip_address" {
value = data.azurerm_network_interface.hcmxexample.private_ip_address
}
output "primary_dns_name" {
value = data.azurerm_public_ip.hcmxexample.fqdn
}
output "virtual_machine_id" {
value = var.os_type=="linux" ? azurerm_linux_virtual_machine.hcmxexample[0].virtual_machine_id : azurerm_windows_virtual_machine.hcmxexample[0].virtual_machine_id
}
output "cloud_instance_id" {
value = var.os_type=="linux" ? azurerm_linux_virtual_machine.hcmxexample[0].id : azurerm_windows_virtual_machine.hcmxexample[0].id
}
output "data_disk_name" {
value = azurerm_managed_disk.hcmxexample.name
}