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

     1  # ---------------------------------------------------------------------------------------------------------------------
     2  # DEPLOY AN ADVANCED AZURE VIRTUAL MACHINE
     3  # This is an advanced example of how to deploy an Azure Virtual Machine in an availability set, managed disk 
     4  # and networking with a public IP.
     5  # ---------------------------------------------------------------------------------------------------------------------
     6  # See test/azure/terraform_azure_vm_example_test.go for how to write automated tests for this code.
     7  # ---------------------------------------------------------------------------------------------------------------------
     8  
     9  provider "azurerm" {
    10    version = "~> 2.29"
    11    features {}
    12  }
    13  
    14  # ---------------------------------------------------------------------------------------------------------------------
    15  # PIN TERRAFORM VERSION
    16  # ---------------------------------------------------------------------------------------------------------------------
    17  
    18  terraform {
    19    # This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting
    20    # 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
    21    # forwards compatible with 0.13.x code.
    22    required_version = ">= 0.12.26"
    23  }
    24  
    25  # ---------------------------------------------------------------------------------------------------------------------
    26  # DEPLOY A RESOURCE GROUP
    27  # ---------------------------------------------------------------------------------------------------------------------
    28  
    29  resource "azurerm_resource_group" "vm_rg" {
    30    name     = "terratest-vm-rg-${var.postfix}"
    31    location = var.location
    32  }
    33  
    34  # ---------------------------------------------------------------------------------------------------------------------
    35  # DEPLOY NETWORK RESOURCES
    36  # This network includes a public address for integration test demonstration purposes
    37  # ---------------------------------------------------------------------------------------------------------------------
    38  
    39  resource "azurerm_virtual_network" "vnet" {
    40    name                = "vnet-${var.postfix}"
    41    address_space       = ["10.0.0.0/16"]
    42    location            = azurerm_resource_group.vm_rg.location
    43    resource_group_name = azurerm_resource_group.vm_rg.name
    44  }
    45  
    46  resource "azurerm_subnet" "subnet" {
    47    name                 = "subnet-${var.postfix}"
    48    resource_group_name  = azurerm_resource_group.vm_rg.name
    49    virtual_network_name = azurerm_virtual_network.vnet.name
    50    address_prefixes     = [var.subnet_prefix]
    51  }
    52  
    53  resource "azurerm_public_ip" "pip" {
    54    name                    = "pip-${var.postfix}"
    55    resource_group_name     = azurerm_resource_group.vm_rg.name
    56    location                = azurerm_resource_group.vm_rg.location
    57    allocation_method       = "Static"
    58    ip_version              = "IPv4"
    59    sku                     = "Standard"
    60    idle_timeout_in_minutes = "4"
    61  }
    62  
    63  # Public and Private IPs assigned to one NIC for test demonstration purposes
    64  resource "azurerm_network_interface" "nic" {
    65    name                = "nic-${var.postfix}"
    66    location            = azurerm_resource_group.vm_rg.location
    67    resource_group_name = azurerm_resource_group.vm_rg.name
    68  
    69    ip_configuration {
    70      name                          = "terratestconfiguration1"
    71      subnet_id                     = azurerm_subnet.subnet.id
    72      private_ip_address_allocation = "Static"
    73      private_ip_address            = var.private_ip
    74      public_ip_address_id          = azurerm_public_ip.pip.id
    75    }
    76  }
    77  
    78  # ---------------------------------------------------------------------------------------------------------------------
    79  # DEPLOY AN AVAILABILITY SET
    80  # ---------------------------------------------------------------------------------------------------------------------
    81  
    82  resource "azurerm_availability_set" "avs" {
    83    name                        = "avs-${var.postfix}"
    84    location                    = azurerm_resource_group.vm_rg.location
    85    resource_group_name         = azurerm_resource_group.vm_rg.name
    86    platform_fault_domain_count = 2
    87    managed                     = true
    88  }
    89  
    90  # ---------------------------------------------------------------------------------------------------------------------
    91  # DEPLOY VIRTUAL MACHINE
    92  # This VM does not actually do anything and is the smallest size VM available with a Windows image
    93  # ---------------------------------------------------------------------------------------------------------------------
    94  
    95  resource "azurerm_virtual_machine" "vm_example" {
    96    name                             = "vm-${var.postfix}"
    97    location                         = azurerm_resource_group.vm_rg.location
    98    resource_group_name              = azurerm_resource_group.vm_rg.name
    99    network_interface_ids            = [azurerm_network_interface.nic.id]
   100    availability_set_id              = azurerm_availability_set.avs.id
   101    vm_size                          = var.vm_size
   102    license_type                     = var.vm_license_type
   103    delete_os_disk_on_termination    = true
   104    delete_data_disks_on_termination = true
   105  
   106    storage_image_reference {
   107      publisher = var.vm_image_publisher
   108      offer     = var.vm_image_offer
   109      sku       = var.vm_image_sku
   110      version   = var.vm_image_version
   111    }
   112  
   113    storage_os_disk {
   114      name              = "osdisk-${var.postfix}"
   115      caching           = "ReadWrite"
   116      create_option     = "FromImage"
   117      managed_disk_type = var.disk_type
   118    }
   119  
   120    os_profile {
   121      computer_name  = "vm-${var.postfix}"
   122      admin_username = var.user_name
   123      admin_password = random_password.rand.result
   124    }
   125  
   126    os_profile_windows_config {
   127      provision_vm_agent = true
   128    }
   129  
   130    tags = {
   131      "Version"     = "0.0.1"
   132      "Environment" = "dev"
   133    }
   134  
   135    depends_on = [random_password.rand]
   136  }
   137  
   138  # Random password is used as an example to simplify the deployment and improve the security of the remote VM.
   139  # This is not as a production recommendation as the password is stored in the Terraform state file.
   140  resource "random_password" "rand" {
   141    length           = 16
   142    override_special = "-_%@"
   143    min_upper        = "1"
   144    min_lower        = "1"
   145    min_numeric      = "1"
   146    min_special      = "1"
   147  }
   148  
   149  # ---------------------------------------------------------------------------------------------------------------------
   150  # ATTACH A MANAGED DISK TO THE VIRTUAL MACHINE
   151  # ---------------------------------------------------------------------------------------------------------------------
   152  
   153  resource "azurerm_managed_disk" "disk" {
   154    name                 = "disk-${var.postfix}"
   155    location             = azurerm_resource_group.vm_rg.location
   156    resource_group_name  = azurerm_resource_group.vm_rg.name
   157    storage_account_type = var.disk_type
   158    create_option        = "Empty"
   159    disk_size_gb         = 10
   160  }
   161  
   162  resource "azurerm_virtual_machine_data_disk_attachment" "vm_disk" {
   163    managed_disk_id    = azurerm_managed_disk.disk.id
   164    virtual_machine_id = azurerm_virtual_machine.vm_example.id
   165    caching            = "ReadWrite"
   166    lun                = 10
   167  }