github.com/ticketmaster/terraform@v0.10.0-beta2.0.20170711045249-a12daf5aba4f/examples/azure-vm-custom-image-new-storage-account/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_virtual_network" "vnet" {
    14    name                = "${var.hostname}vnet"
    15    location            = "${azurerm_resource_group.rg.location}"
    16    resource_group_name = "${azurerm_resource_group.rg.name}"
    17    address_space       = ["${var.address_space}"]
    18  }
    19  
    20  resource "azurerm_subnet" "subnet" {
    21    name                 = "${var.hostname}subnet"
    22    virtual_network_name = "${azurerm_virtual_network.vnet.name}"
    23    resource_group_name  = "${azurerm_resource_group.rg.name}"
    24    address_prefix       = "${var.subnet_prefix}"
    25  }
    26  
    27  resource "azurerm_public_ip" "transferpip" {
    28    name                         = "transferpip"
    29    location                     = "${azurerm_resource_group.rg.location}"
    30    resource_group_name          = "${azurerm_resource_group.rg.name}"
    31    public_ip_address_allocation = "Static"
    32  }
    33  
    34  resource "azurerm_network_interface" "transfernic" {
    35    name                = "transfernic"
    36    location            = "${azurerm_resource_group.rg.location}"
    37    resource_group_name = "${azurerm_resource_group.rg.name}"
    38  
    39    ip_configuration {
    40      name                          = "${azurerm_public_ip.transferpip.name}"
    41      subnet_id                     = "${azurerm_subnet.subnet.id}"
    42      private_ip_address_allocation = "Static"
    43      public_ip_address_id          = "${azurerm_public_ip.transferpip.id}"
    44      private_ip_address            = "10.0.0.5"
    45    }
    46  }
    47  
    48  resource "azurerm_public_ip" "mypip" {
    49    name                         = "mypip"
    50    location                     = "${azurerm_resource_group.rg.location}"
    51    resource_group_name          = "${azurerm_resource_group.rg.name}"
    52    public_ip_address_allocation = "Dynamic"
    53  }
    54  
    55  resource "azurerm_network_interface" "mynic" {
    56    name                = "mynic"
    57    location            = "${azurerm_resource_group.rg.location}"
    58    resource_group_name = "${azurerm_resource_group.rg.name}"
    59  
    60    ip_configuration {
    61      name                          = "${azurerm_public_ip.mypip.name}"
    62      subnet_id                     = "${azurerm_subnet.subnet.id}"
    63      private_ip_address_allocation = "Dynamic"
    64      public_ip_address_id          = "${azurerm_public_ip.mypip.id}"
    65    }
    66  }
    67  
    68  resource "azurerm_storage_account" "existing" {
    69    name                = "${var.existing_storage_acct}"
    70    resource_group_name = "${var.existing_resource_group}"
    71    location            = "${azurerm_resource_group.rg.location}"
    72    account_type        = "${var.existing_storage_acct_type}"
    73  
    74    lifecycle = {
    75      prevent_destroy = true
    76    }
    77  }
    78  
    79  resource "azurerm_storage_account" "stor" {
    80    name                = "${var.hostname}"
    81    resource_group_name = "${azurerm_resource_group.rg.name}"
    82    location            = "${azurerm_resource_group.rg.location}"
    83    account_type        = "${var.storage_account_type}"
    84  }
    85  
    86  resource "azurerm_virtual_machine" "transfer" {
    87    name                  = "${var.transfer_vm_name}"
    88    location              = "${azurerm_resource_group.rg.location}"
    89    resource_group_name   = "${azurerm_resource_group.rg.name}"
    90    vm_size               = "${var.vm_size}"
    91    network_interface_ids = ["${azurerm_network_interface.transfernic.id}"]
    92  
    93    storage_os_disk {
    94      name          = "${var.hostname}-osdisk"
    95      image_uri     = "${var.source_img_uri}"
    96      vhd_uri       = "https://${var.existing_storage_acct}.blob.core.windows.net/${var.existing_resource_group}-vhds/${var.hostname}osdisk.vhd"
    97      os_type       = "${var.os_type}"
    98      caching       = "ReadWrite"
    99      create_option = "FromImage"
   100    }
   101  
   102    os_profile {
   103      computer_name  = "${var.hostname}"
   104      admin_username = "${var.admin_username}"
   105      admin_password = "${var.admin_password}"
   106    }
   107  }
   108  
   109  resource "azurerm_virtual_machine_extension" "script" {
   110    name                 = "CustomScriptExtension"
   111    location             = "${azurerm_resource_group.rg.location}"
   112    resource_group_name  = "${azurerm_resource_group.rg.name}"
   113    virtual_machine_name = "${azurerm_virtual_machine.transfer.name}"
   114    publisher            = "Microsoft.Compute"
   115    type                 = "CustomScriptExtension"
   116    type_handler_version = "1.4"
   117    depends_on           = ["azurerm_virtual_machine.transfer"]
   118  
   119    settings = <<SETTINGS
   120      {
   121          "commandToExecute": "powershell -ExecutionPolicy Unrestricted -Command \"Invoke-WebRequest -Uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/201-vm-custom-image-new-storage-account/ImageTransfer.ps1 -OutFile C:/ImageTransfer.ps1\" "
   122      }
   123  SETTINGS
   124  }
   125  
   126  resource "azurerm_virtual_machine_extension" "execute" {
   127    name                 = "CustomScriptExtension"
   128    location             = "${azurerm_resource_group.rg.location}"
   129    resource_group_name  = "${azurerm_resource_group.rg.name}"
   130    virtual_machine_name = "${azurerm_virtual_machine.transfer.name}"
   131    publisher            = "Microsoft.Compute"
   132    type                 = "CustomScriptExtension"
   133    type_handler_version = "1.4"
   134    depends_on           = ["azurerm_virtual_machine_extension.script"]
   135  
   136    settings = <<SETTINGS
   137      {
   138          "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File C:\\ImageTransfer.ps1 -SourceImage ${var.source_img_uri} -SourceSAKey ${azurerm_storage_account.existing.primary_access_key} -DestinationURI https://${azurerm_storage_account.stor.name}.blob.core.windows.net/vhds -DestinationSAKey ${azurerm_storage_account.stor.primary_access_key}\" "
   139      }
   140  SETTINGS
   141  }
   142  
   143  resource "azurerm_virtual_machine" "myvm" {
   144    name                  = "${var.new_vm_name}"
   145    location              = "${azurerm_resource_group.rg.location}"
   146    resource_group_name   = "${azurerm_resource_group.rg.name}"
   147    vm_size               = "${var.vm_size}"
   148    network_interface_ids = ["${azurerm_network_interface.mynic.id}"]
   149    depends_on            = ["azurerm_virtual_machine_extension.execute"]
   150  
   151    storage_os_disk {
   152      name          = "${var.hostname}osdisk"
   153      image_uri     = "https://${azurerm_storage_account.stor.name}.blob.core.windows.net/vhds/${var.custom_image_name}.vhd"
   154      vhd_uri       = "https://${var.hostname}.blob.core.windows.net/${var.hostname}-vhds/${var.hostname}osdisk.vhd"
   155      os_type       = "${var.os_type}"
   156      caching       = "ReadWrite"
   157      create_option = "FromImage"
   158    }
   159  
   160    os_profile {
   161      computer_name  = "${var.hostname}"
   162      admin_username = "${var.admin_username}"
   163      admin_password = "${var.admin_password}"
   164    }
   165  }