github.com/alkar/terraform@v0.9.6-0.20170517124458-a4cddf6ebf59/examples/azure-traffic-manager-vm/main.tf (about)

     1  # provider "azurerm" {
     2  #   subscription_id = "REPLACE-WITH-YOUR-SUBSCRIPTION-ID"
     3  #   client_id       = "REPLACE-WITH-YOUR-CLIENT-ID"
     4  #   client_secret   = "REPLACE-WITH-YOUR-CLIENT-SECRET"
     5  #   tenant_id       = "REPLACE-WITH-YOUR-TENANT-ID"
     6  # }
     7  
     8  resource "azurerm_resource_group" "rg" {
     9    name     = "${var.resource_group}"
    10    location = "${var.location}"
    11  }
    12  
    13  resource "azurerm_public_ip" "pip" {
    14    name                         = "ip${count.index}"
    15    location                     = "${var.location}"
    16    resource_group_name          = "${azurerm_resource_group.rg.name}"
    17    public_ip_address_allocation = "dynamic"
    18    domain_name_label            = "${var.dns_name}${count.index}"
    19    count                        = "${var.num_vms}"
    20  }
    21  
    22  resource "azurerm_virtual_network" "vnet" {
    23    name                = "${var.vnet}"
    24    location            = "${var.location}"
    25    address_space       = ["${var.address_space}"]
    26    resource_group_name = "${azurerm_resource_group.rg.name}"
    27  }
    28  
    29  resource "azurerm_subnet" "subnet" {
    30    name                 = "${var.subnet_name}"
    31    virtual_network_name = "${azurerm_virtual_network.vnet.name}"
    32    resource_group_name  = "${azurerm_resource_group.rg.name}"
    33    address_prefix       = "${var.subnet_prefix}"
    34  }
    35  
    36  resource "azurerm_network_interface" "nic" {
    37    name                = "nic${count.index}"
    38    location            = "${var.location}"
    39    resource_group_name = "${azurerm_resource_group.rg.name}"
    40    count               = "${var.num_vms}"
    41  
    42    ip_configuration {
    43      name                          = "ipconfig${count.index}"
    44      subnet_id                     = "${azurerm_subnet.subnet.id}"
    45      private_ip_address_allocation = "Dynamic"
    46      public_ip_address_id          = "${element(azurerm_public_ip.pip.*.id, count.index)}"
    47    }
    48  }
    49  
    50  resource "azurerm_virtual_machine" "vm" {
    51    name                  = "vm${count.index}"
    52    location              = "${var.location}"
    53    resource_group_name   = "${azurerm_resource_group.rg.name}"
    54    vm_size               = "${var.vm_size}"
    55    count                 = "${var.num_vms}"
    56    network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]
    57  
    58    storage_image_reference {
    59      publisher = "${var.image_publisher}"
    60      offer     = "${var.image_offer}"
    61      sku       = "${var.image_sku}"
    62      version   = "${var.image_version}"
    63    }
    64  
    65    storage_os_disk {
    66      name          = "osdisk${count.index}"
    67      create_option = "FromImage"
    68    }
    69  
    70    os_profile {
    71      computer_name  = "vm${count.index}"
    72      admin_username = "${var.admin_username}"
    73      admin_password = "${var.admin_password}"
    74    }
    75  
    76    os_profile_linux_config {
    77      disable_password_authentication = false
    78    }
    79  }
    80  
    81  resource "azurerm_virtual_machine_extension" "ext" {
    82    depends_on                 = ["azurerm_virtual_machine.vm"]
    83    name                       = "CustomScript"
    84    location                   = "${var.location}"
    85    resource_group_name        = "${azurerm_resource_group.rg.name}"
    86    virtual_machine_name       = "vm${count.index}"
    87    publisher                  = "Microsoft.Azure.Extensions"
    88    type                       = "CustomScript"
    89    type_handler_version       = "2.0"
    90    count                      = "${var.num_vms}"
    91    auto_upgrade_minor_version = true
    92  
    93    settings = <<SETTINGS
    94      {
    95          "commandToExecute": "sudo bash -c 'apt-get update && apt-get -y install apache2' "
    96      }
    97  SETTINGS
    98  }
    99  
   100  resource "azurerm_traffic_manager_profile" "profile" {
   101    name                   = "trafficmanagerprofile"
   102    resource_group_name    = "${azurerm_resource_group.rg.name}"
   103    traffic_routing_method = "Weighted"
   104  
   105    dns_config {
   106      relative_name = "${azurerm_resource_group.rg.name}"
   107      ttl           = 30
   108    }
   109  
   110    monitor_config {
   111      protocol = "http"
   112      port     = 80
   113      path     = "/"
   114    }
   115  }
   116  
   117  resource "azurerm_traffic_manager_endpoint" "endpoint" {
   118    name                = "endpoint${count.index}"
   119    resource_group_name = "${azurerm_resource_group.rg.name}"
   120    profile_name        = "${azurerm_traffic_manager_profile.profile.name}"
   121    target_resource_id  = "${element(azurerm_public_ip.pip.*.id, count.index)}"
   122    type                = "azureEndpoints"
   123    weight              = 1
   124    count               = 3
   125  }