github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/examples/azure/terraform-azure-availabilityset-example/main.tf (about)

     1  # ---------------------------------------------------------------------------------------------------------------------
     2  # DEPLOY AN AZURE AVAILABILITY SET
     3  # This is an example of how to deploy an Azure Availability Set with a Virtual Machine in the availability set 
     4  # and the minimum network resources for the VM.
     5  # ---------------------------------------------------------------------------------------------------------------------
     6  # See test/azure/terraform_azure_availabilityset_example_test.go for how to write automated tests for this code.
     7  # ---------------------------------------------------------------------------------------------------------------------
     8  
     9  provider "azurerm" {
    10    version = "~> 2.20"
    11    features {}
    12  }
    13  
    14  # ---------------------------------------------------------------------------------------------------------------------
    15  # PIN TERRAFORM VERSION TO >= 0.12
    16  # The examples have been upgraded to 0.12 syntax
    17  # ---------------------------------------------------------------------------------------------------------------------
    18  
    19  terraform {
    20    # This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting
    21    # 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
    22    # forwards compatible with 0.13.x code.
    23    required_version = ">= 0.12.26"
    24  }
    25  
    26  # ---------------------------------------------------------------------------------------------------------------------
    27  # DEPLOY A RESOURCE GROUP
    28  # ---------------------------------------------------------------------------------------------------------------------
    29  
    30  resource "azurerm_resource_group" "avs" {
    31    name     = "terratest-avs-rg-${var.postfix}"
    32    location = var.location
    33  }
    34  
    35  # ---------------------------------------------------------------------------------------------------------------------
    36  # DEPLOY THE AVAILABILITY SET
    37  # ---------------------------------------------------------------------------------------------------------------------
    38  
    39  resource "azurerm_availability_set" "avs" {
    40    name                        = "avs-${var.postfix}"
    41    location                    = azurerm_resource_group.avs.location
    42    resource_group_name         = azurerm_resource_group.avs.name
    43    platform_fault_domain_count = var.avs_fault_domain_count
    44    managed                     = true
    45  }
    46  
    47  # ---------------------------------------------------------------------------------------------------------------------
    48  # DEPLOY MINIMAL NETWORK RESOURCES FOR VM
    49  # ---------------------------------------------------------------------------------------------------------------------
    50  
    51  resource "azurerm_virtual_network" "avs" {
    52    name                = "vnet-${var.postfix}"
    53    address_space       = ["10.0.0.0/16"]
    54    location            = azurerm_resource_group.avs.location
    55    resource_group_name = azurerm_resource_group.avs.name
    56  }
    57  
    58  resource "azurerm_subnet" "avs" {
    59    name                 = "subnet-${var.postfix}"
    60    resource_group_name  = azurerm_resource_group.avs.name
    61    virtual_network_name = azurerm_virtual_network.avs.name
    62    address_prefixes     = ["10.0.17.0/24"]
    63  }
    64  
    65  resource "azurerm_network_interface" "avs" {
    66    name                = "nic-${var.postfix}"
    67    location            = azurerm_resource_group.avs.location
    68    resource_group_name = azurerm_resource_group.avs.name
    69  
    70    ip_configuration {
    71      name                          = "config-${var.postfix}-01"
    72      subnet_id                     = azurerm_subnet.avs.id
    73      private_ip_address_allocation = "Dynamic"
    74    }
    75  }
    76  
    77  # ---------------------------------------------------------------------------------------------------------------------
    78  # DEPLOY VIRTUAL MACHINE
    79  # This VM does not actually do anything and is the smallest size VM available with an Ubuntu image
    80  # ---------------------------------------------------------------------------------------------------------------------
    81  
    82  resource "azurerm_virtual_machine" "avs" {
    83    name                             = "vm-${var.postfix}"
    84    location                         = azurerm_resource_group.avs.location
    85    resource_group_name              = azurerm_resource_group.avs.name
    86    network_interface_ids            = [azurerm_network_interface.avs.id]
    87    availability_set_id              = azurerm_availability_set.avs.id
    88    vm_size                          = "Standard_B1ls"
    89    delete_os_disk_on_termination    = true
    90    delete_data_disks_on_termination = true
    91  
    92    storage_image_reference {
    93      publisher = "Canonical"
    94      offer     = "UbuntuServer"
    95      sku       = "18.04-LTS"
    96      version   = "latest"
    97    }
    98  
    99    storage_os_disk {
   100      name              = "osdisk-${var.postfix}"
   101      caching           = "None"
   102      create_option     = "FromImage"
   103      managed_disk_type = "Standard_LRS"
   104    }
   105  
   106    os_profile {
   107      computer_name  = "vm-${var.postfix}"
   108      admin_username = "testadmin"
   109      admin_password = random_password.avs.result
   110    }
   111  
   112    os_profile_linux_config {
   113      disable_password_authentication = false
   114    }
   115  
   116    depends_on = [random_password.avs]
   117  }
   118  
   119  resource "random_password" "avs" {
   120    length           = 16
   121    override_special = "-_%@"
   122    min_upper        = "1"
   123    min_lower        = "1"
   124    min_numeric      = "1"
   125    min_special      = "1"
   126  }