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

     1  # ---------------------------------------------------------------------------------------------------------------------
     2  # DEPLOY AN AZURE VIRTUAL MACHINE
     3  # This is an example of how to deploy an Azure Virtual Machine with the minimum network resources.
     4  # ---------------------------------------------------------------------------------------------------------------------
     5  # See test/azure/terraform_azure_example_test.go for how to write automated tests for this code.
     6  # ---------------------------------------------------------------------------------------------------------------------
     7  
     8  provider "azurerm" {
     9    version = "~> 2.29"
    10    features {}
    11  }
    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" "main" {
    31    name     = "terratest-rg-${var.postfix}"
    32    location = "East US"
    33  }
    34  
    35  # ---------------------------------------------------------------------------------------------------------------------
    36  # DEPLOY VIRTUAL NETWORK RESOURCES
    37  # ---------------------------------------------------------------------------------------------------------------------
    38  
    39  resource "azurerm_virtual_network" "main" {
    40    name                = "vnet-${var.postfix}"
    41    address_space       = ["10.0.0.0/16"]
    42    location            = azurerm_resource_group.main.location
    43    resource_group_name = azurerm_resource_group.main.name
    44  }
    45  
    46  resource "azurerm_subnet" "internal" {
    47    name                 = "subnet-${var.postfix}"
    48    resource_group_name  = azurerm_resource_group.main.name
    49    virtual_network_name = azurerm_virtual_network.main.name
    50    address_prefixes     = ["10.0.17.0/24"]
    51  }
    52  
    53  resource "azurerm_network_interface" "main" {
    54    name                = "nic-${var.postfix}"
    55    location            = azurerm_resource_group.main.location
    56    resource_group_name = azurerm_resource_group.main.name
    57  
    58    ip_configuration {
    59      name                          = "terratestconfiguration1"
    60      subnet_id                     = azurerm_subnet.internal.id
    61      private_ip_address_allocation = "Dynamic"
    62    }
    63  }
    64  
    65  # ---------------------------------------------------------------------------------------------------------------------
    66  # DEPLOY A VIRTUAL MACHINE RUNNING UBUNTU
    67  # ---------------------------------------------------------------------------------------------------------------------
    68  
    69  resource "azurerm_virtual_machine" "main" {
    70    name                             = "vm-${var.postfix}"
    71    location                         = azurerm_resource_group.main.location
    72    resource_group_name              = azurerm_resource_group.main.name
    73    network_interface_ids            = [azurerm_network_interface.main.id]
    74    vm_size                          = "Standard_B1s"
    75    delete_os_disk_on_termination    = true
    76    delete_data_disks_on_termination = true
    77  
    78    storage_image_reference {
    79      publisher = "Canonical"
    80      offer     = "UbuntuServer"
    81      sku       = "16.04-LTS"
    82      version   = "latest"
    83    }
    84  
    85    storage_os_disk {
    86      name              = "terratestosdisk1"
    87      caching           = "ReadWrite"
    88      create_option     = "FromImage"
    89      managed_disk_type = "Standard_LRS"
    90    }
    91  
    92    os_profile {
    93      computer_name  = "vm-${var.postfix}"
    94      admin_username = var.username
    95      admin_password = random_password.main.result
    96    }
    97  
    98    os_profile_linux_config {
    99      disable_password_authentication = false
   100    }
   101  
   102    depends_on = [random_password.main]
   103  }
   104  
   105  # Random password is used as an example to simplify the deployment and improve the security of the remote VM.
   106  # This is not as a production recommendation as the password is stored in the Terraform state file.
   107  resource "random_password" "main" {
   108    length           = 16
   109    override_special = "-_%@"
   110    min_upper        = "1"
   111    min_lower        = "1"
   112    min_numeric      = "1"
   113    min_special      = "1"
   114  }