2023-02-13 12:28:10 +03:00
|
|
|
resource "random_string" "rg" {
|
|
|
|
length = 8
|
|
|
|
upper = false
|
|
|
|
special = false
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_resource_group" "rg" {
|
|
|
|
name = "101-application-gateway-${random_string.rg.result}"
|
2022-02-25 01:14:36 +03:00
|
|
|
location = "eastus"
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:28:10 +03:00
|
|
|
resource "azurerm_virtual_network" "vnet" {
|
2022-02-25 01:14:36 +03:00
|
|
|
name = "myVNet"
|
2023-02-13 12:28:10 +03:00
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
|
|
|
location = azurerm_resource_group.rg.location
|
2022-02-25 01:14:36 +03:00
|
|
|
address_space = ["10.21.0.0/16"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_subnet" "frontend" {
|
|
|
|
name = "myAGSubnet"
|
2023-02-13 12:28:10 +03:00
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
|
|
|
virtual_network_name = azurerm_virtual_network.vnet.name
|
2022-02-25 01:14:36 +03:00
|
|
|
address_prefixes = ["10.21.0.0/24"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_subnet" "backend" {
|
|
|
|
name = "myBackendSubnet"
|
2023-02-13 12:28:10 +03:00
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
|
|
|
virtual_network_name = azurerm_virtual_network.vnet.name
|
2022-02-25 01:14:36 +03:00
|
|
|
address_prefixes = ["10.21.1.0/24"]
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:28:10 +03:00
|
|
|
resource "azurerm_public_ip" "pip" {
|
2022-02-25 01:14:36 +03:00
|
|
|
name = "myAGPublicIPAddress"
|
2023-02-13 12:28:10 +03:00
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
|
|
|
location = azurerm_resource_group.rg.location
|
2022-02-25 01:14:36 +03:00
|
|
|
allocation_method = "Static"
|
|
|
|
sku = "Standard"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-14 05:00:49 +03:00
|
|
|
resource "azurerm_application_gateway" "main" {
|
2022-02-25 01:14:36 +03:00
|
|
|
name = "myAppGateway"
|
2023-02-13 12:28:10 +03:00
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
|
|
|
location = azurerm_resource_group.rg.location
|
2022-02-25 01:14:36 +03:00
|
|
|
|
|
|
|
sku {
|
|
|
|
name = "Standard_v2"
|
|
|
|
tier = "Standard_v2"
|
|
|
|
capacity = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
gateway_ip_configuration {
|
|
|
|
name = "my-gateway-ip-configuration"
|
|
|
|
subnet_id = azurerm_subnet.frontend.id
|
|
|
|
}
|
|
|
|
|
|
|
|
frontend_port {
|
|
|
|
name = var.frontend_port_name
|
|
|
|
port = 80
|
|
|
|
}
|
|
|
|
|
|
|
|
frontend_ip_configuration {
|
|
|
|
name = var.frontend_ip_configuration_name
|
2023-02-13 12:28:10 +03:00
|
|
|
public_ip_address_id = azurerm_public_ip.pip.id
|
2022-02-25 01:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
backend_address_pool {
|
|
|
|
name = var.backend_address_pool_name
|
|
|
|
}
|
|
|
|
|
|
|
|
backend_http_settings {
|
|
|
|
name = var.http_setting_name
|
|
|
|
cookie_based_affinity = "Disabled"
|
|
|
|
port = 80
|
|
|
|
protocol = "Http"
|
|
|
|
request_timeout = 60
|
|
|
|
}
|
|
|
|
|
|
|
|
http_listener {
|
|
|
|
name = var.listener_name
|
|
|
|
frontend_ip_configuration_name = var.frontend_ip_configuration_name
|
|
|
|
frontend_port_name = var.frontend_port_name
|
|
|
|
protocol = "Http"
|
|
|
|
}
|
|
|
|
|
|
|
|
request_routing_rule {
|
|
|
|
name = var.request_routing_rule_name
|
|
|
|
rule_type = "Basic"
|
|
|
|
http_listener_name = var.listener_name
|
|
|
|
backend_address_pool_name = var.backend_address_pool_name
|
|
|
|
backend_http_settings_name = var.http_setting_name
|
2023-02-13 12:28:10 +03:00
|
|
|
priority = 1
|
2022-02-25 01:14:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_network_interface" "nic" {
|
2023-02-13 12:28:10 +03:00
|
|
|
count = 2
|
2022-02-25 01:14:36 +03:00
|
|
|
name = "nic-${count.index+1}"
|
2023-02-13 12:28:10 +03:00
|
|
|
location = azurerm_resource_group.rg.location
|
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
2022-02-25 01:14:36 +03:00
|
|
|
|
|
|
|
ip_configuration {
|
|
|
|
name = "nic-ipconfig-${count.index+1}"
|
|
|
|
subnet_id = azurerm_subnet.backend.id
|
|
|
|
private_ip_address_allocation = "Dynamic"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:28:10 +03:00
|
|
|
resource "azurerm_network_interface_application_gateway_backend_address_pool_association" "nic-assoc" {
|
|
|
|
count = 2
|
2022-02-25 01:14:36 +03:00
|
|
|
network_interface_id = azurerm_network_interface.nic[count.index].id
|
|
|
|
ip_configuration_name = "nic-ipconfig-${count.index+1}"
|
2023-02-14 05:00:49 +03:00
|
|
|
backend_address_pool_id = one(azurerm_application_gateway.main.backend_address_pool).id
|
2022-02-25 01:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_password" "password" {
|
2023-02-13 12:28:10 +03:00
|
|
|
length = 16
|
2022-02-25 01:14:36 +03:00
|
|
|
special = true
|
2023-02-13 12:28:10 +03:00
|
|
|
lower = true
|
|
|
|
upper = true
|
|
|
|
numeric = true
|
2022-02-25 01:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_windows_virtual_machine" "vm" {
|
2023-02-13 12:28:10 +03:00
|
|
|
count = 2
|
2022-02-25 01:14:36 +03:00
|
|
|
name = "myVM${count.index+1}"
|
2023-02-13 12:28:10 +03:00
|
|
|
resource_group_name = azurerm_resource_group.rg.name
|
|
|
|
location = azurerm_resource_group.rg.location
|
2022-02-25 01:14:36 +03:00
|
|
|
size = "Standard_DS1_v2"
|
|
|
|
admin_username = "azureadmin"
|
|
|
|
admin_password = random_password.password.result
|
|
|
|
|
|
|
|
network_interface_ids = [
|
|
|
|
azurerm_network_interface.nic[count.index].id,
|
|
|
|
]
|
|
|
|
|
|
|
|
os_disk {
|
|
|
|
caching = "ReadWrite"
|
|
|
|
storage_account_type = "Standard_LRS"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
source_image_reference {
|
|
|
|
publisher = "MicrosoftWindowsServer"
|
|
|
|
offer = "WindowsServer"
|
|
|
|
sku = "2019-Datacenter"
|
|
|
|
version = "latest"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_virtual_machine_extension" "vm-extensions" {
|
2023-02-13 12:28:10 +03:00
|
|
|
count = 2
|
2022-02-25 01:14:36 +03:00
|
|
|
name = "vm${count.index+1}-ext"
|
|
|
|
virtual_machine_id = azurerm_windows_virtual_machine.vm[count.index].id
|
|
|
|
publisher = "Microsoft.Compute"
|
|
|
|
type = "CustomScriptExtension"
|
|
|
|
type_handler_version = "1.10"
|
|
|
|
|
|
|
|
settings = <<SETTINGS
|
|
|
|
{
|
|
|
|
"commandToExecute": "powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"
|
|
|
|
}
|
|
|
|
SETTINGS
|
|
|
|
|
|
|
|
}
|