-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-vm-main.tf
135 lines (114 loc) · 4.45 KB
/
api-vm-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
###################
## API VM - Main ##
###################
# Create Network Security Group to Access the API VM from Internet
resource "azurerm_network_security_group" "api-vm-nsg" {
name = "${var.app_name}-${var.environment}-vm-nsg"
location = azurerm_resource_group.network-rg.location
resource_group_name = azurerm_resource_group.network-rg.name
security_rule {
name = "allow-rdp"
description = "allow-rdp"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
security_rule {
name = "allow-https"
description = "allow-https"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
tags = {
application = var.app_name
environment = var.environment
}
}
# Associate the NSG with the Subnet
resource "azurerm_subnet_network_security_group_association" "api-vm-nsg-association" {
depends_on=[azurerm_network_security_group.api-vm-nsg]
subnet_id = azurerm_subnet.network-subnet.id
network_security_group_id = azurerm_network_security_group.api-vm-nsg.id
}
# Get a Static Public IP for the API VM
resource "azurerm_public_ip" "api-vm-ip" {
name = "${var.app_name}-${var.environment}-vm-pip"
location = azurerm_resource_group.network-rg.location
resource_group_name = azurerm_resource_group.network-rg.name
allocation_method = "Static"
tags = {
application = var.app_name
environment = var.environment
}
}
# Create Network Card for the API VM
resource "azurerm_network_interface" "api-vm-nic" {
depends_on=[azurerm_public_ip.api-vm-ip]
name = "${var.app_name}-${var.environment}-vm-nic"
location = azurerm_resource_group.network-rg.location
resource_group_name = azurerm_resource_group.network-rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.network-subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.api-vm-ip.id
}
tags = {
application = var.app_name
environment = var.environment
}
}
# Create API Server
resource "azurerm_windows_virtual_machine" "api-vm" {
depends_on=[azurerm_network_interface.api-vm-nic]
name = "${var.app_name}-${var.environment}-vm"
location = azurerm_resource_group.network-rg.location
resource_group_name = azurerm_resource_group.network-rg.name
size = var.api_vm_size
network_interface_ids = [azurerm_network_interface.api-vm-nic.id]
computer_name = var.api_vm_name
admin_username = var.api_admin_username
admin_password = var.api_admin_password
os_disk {
name = "${var.app_name}-${var.environment}-vm-os-disk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
enable_automatic_updates = true
provision_vm_agent = true
tags = {
application = var.app_name
environment = var.environment
}
}
# Add Azure VM extension
resource "azurerm_virtual_machine_extension" "api-extension" {
name = "${var.app_name}-${var.environment}-vm-extension"
virtual_machine_id = azurerm_windows_virtual_machine.api-vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"fileUris": ["https://raw.githubusercontent.com/KopiCloud-AD-API/kopicloud-ad-api-setup-scripts/main/setup-win2019.ps1"],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File setup-win2019.ps1"
}
SETTINGS
}