terraform/examples/azure-traffic-manager-vm/main.tf

126 lines
4.0 KiB
HCL

# provider "azurerm" {
# subscription_id = "REPLACE-WITH-YOUR-SUBSCRIPTION-ID"
# client_id = "REPLACE-WITH-YOUR-CLIENT-ID"
# client_secret = "REPLACE-WITH-YOUR-CLIENT-SECRET"
# tenant_id = "REPLACE-WITH-YOUR-TENANT-ID"
# }
resource "azurerm_resource_group" "rg" {
name = "${var.resource_group}"
location = "${var.location}"
}
resource "azurerm_public_ip" "pip" {
name = "ip${count.index}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
public_ip_address_allocation = "dynamic"
domain_name_label = "${var.dns_name}${count.index}"
count = "${var.num_vms}"
}
resource "azurerm_virtual_network" "vnet" {
name = "${var.vnet}"
location = "${var.location}"
address_space = ["${var.address_space}"]
resource_group_name = "${azurerm_resource_group.rg.name}"
}
resource "azurerm_subnet" "subnet" {
name = "${var.subnet_name}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.rg.name}"
address_prefix = "${var.subnet_prefix}"
}
resource "azurerm_network_interface" "nic" {
name = "nic${count.index}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
count = "${var.num_vms}"
ip_configuration {
name = "ipconfig${count.index}"
subnet_id = "${azurerm_subnet.subnet.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${element(azurerm_public_ip.pip.*.id, count.index)}"
}
}
resource "azurerm_virtual_machine" "vm" {
name = "vm${count.index}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
vm_size = "${var.vm_size}"
count = "${var.num_vms}"
network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]
storage_image_reference {
publisher = "${var.image_publisher}"
offer = "${var.image_offer}"
sku = "${var.image_sku}"
version = "${var.image_version}"
}
storage_os_disk {
name = "osdisk${count.index}"
create_option = "FromImage"
}
os_profile {
computer_name = "vm${count.index}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
resource "azurerm_virtual_machine_extension" "ext" {
depends_on = ["azurerm_virtual_machine.vm"]
name = "CustomScript"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
virtual_machine_name = "vm${count.index}"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
count = "${var.num_vms}"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"commandToExecute": "sudo bash -c 'apt-get update && apt-get -y install apache2' "
}
SETTINGS
}
resource "azurerm_traffic_manager_profile" "profile" {
name = "trafficmanagerprofile"
resource_group_name = "${azurerm_resource_group.rg.name}"
traffic_routing_method = "Weighted"
dns_config {
relative_name = "${azurerm_resource_group.rg.name}"
ttl = 30
}
monitor_config {
protocol = "http"
port = 80
path = "/"
}
}
resource "azurerm_traffic_manager_endpoint" "endpoint" {
name = "endpoint${count.index}"
resource_group_name = "${azurerm_resource_group.rg.name}"
profile_name = "${azurerm_traffic_manager_profile.profile.name}"
target_resource_id = "${element(azurerm_public_ip.pip.*.id, count.index)}"
type = "azureEndpoints"
weight = 1
count = 3
}