github.com/spirius/terraform@v0.10.0-beta2.0.20170714185654-87b2c0cf8fea/examples/azure-wordpress-mysql-replication/main.tf (about)

     1  # provider "azurerm" {
     2  #   subscription_id = "${var.subscription_id}"
     3  #   client_id       = "${var.client_id}"
     4  #   client_secret   = "${var.client_secret}"
     5  #   tenant_id       = "${var.tenant_id}"
     6  # }
     7  
     8  # ********************** MYSQL REPLICATION ********************** #
     9  
    10  resource "azurerm_resource_group" "rg" {
    11    name     = "${var.resource_group}"
    12    location = "${var.location}"
    13  }
    14  
    15  # ********************** VNET / SUBNET ********************** #
    16  resource "azurerm_virtual_network" "vnet" {
    17    name                = "${var.virtual_network_name}"
    18    resource_group_name = "${azurerm_resource_group.rg.name}"
    19    location            = "${azurerm_resource_group.rg.location}"
    20    address_space       = ["${var.vnet_address_prefix}"]
    21  }
    22  
    23  resource "azurerm_subnet" "db_subnet" {
    24    name                      = "${var.db_subnet_name}"
    25    virtual_network_name      = "${azurerm_virtual_network.vnet.name}"
    26    resource_group_name       = "${azurerm_resource_group.rg.name}"
    27    network_security_group_id = "${azurerm_network_security_group.nsg.id}"
    28    address_prefix            = "${var.db_subnet_address_prefix}"
    29    depends_on                = ["azurerm_virtual_network.vnet"]
    30  }
    31  
    32  # **********************  STORAGE ACCOUNTS ********************** #
    33  resource "azurerm_storage_account" "stor" {
    34    name                = "${var.unique_prefix}${var.storage_account_name}"
    35    resource_group_name = "${azurerm_resource_group.rg.name}"
    36    location            = "${azurerm_resource_group.rg.location}"
    37    account_type        = "${var.storage_account_type}"
    38  }
    39  
    40  # **********************  NETWORK SECURITY GROUP ********************** #
    41  resource "azurerm_network_security_group" "nsg" {
    42    name                = "${var.unique_prefix}-nsg"
    43    resource_group_name = "${azurerm_resource_group.rg.name}"
    44    location            = "${azurerm_resource_group.rg.location}"
    45  
    46    security_rule {
    47      name                       = "allow-ssh"
    48      description                = "Allow SSH"
    49      priority                   = 100
    50      direction                  = "Inbound"
    51      access                     = "Allow"
    52      protocol                   = "Tcp"
    53      source_port_range          = "*"
    54      destination_port_range     = "22"
    55      source_address_prefix      = "Internet"
    56      destination_address_prefix = "*"
    57    }
    58  
    59   security_rule {
    60      name                       = "MySQL"
    61      description                = "MySQL"
    62      priority                   = 110
    63      direction                  = "Inbound"
    64      access                     = "Allow"
    65      protocol                   = "Tcp"
    66      source_port_range          = "*"
    67      destination_port_range     = "3306"
    68      source_address_prefix      = "*"
    69      destination_address_prefix = "*"
    70    }
    71  }
    72  
    73  # **********************  PUBLIC IP ADDRESSES ********************** #
    74  resource "azurerm_public_ip" "pip" {
    75    name                         = "${var.public_ip_name}"
    76    location                     = "${azurerm_resource_group.rg.location}"
    77    resource_group_name          = "${azurerm_resource_group.rg.name}"
    78    public_ip_address_allocation = "Static"
    79    domain_name_label            = "${var.dns_name}"
    80  }
    81  
    82  # **********************  AVAILABILITY SET ********************** #
    83  resource "azurerm_availability_set" "availability_set" {
    84    name                = "${var.dns_name}-set"
    85    location            = "${azurerm_resource_group.rg.location}"
    86    resource_group_name = "${azurerm_resource_group.rg.name}"
    87  }
    88  
    89  # **********************  NETWORK INTERFACES ********************** #
    90  resource "azurerm_network_interface" "nic" {
    91    name                      = "${var.nic_name}${count.index}"
    92    location                  = "${azurerm_resource_group.rg.location}"
    93    resource_group_name       = "${azurerm_resource_group.rg.name}"
    94    network_security_group_id = "${azurerm_network_security_group.nsg.id}"
    95    count                     = "${var.node_count}"
    96    depends_on                = ["azurerm_virtual_network.vnet", "azurerm_public_ip.pip", "azurerm_lb.lb"]
    97  
    98    ip_configuration {
    99      name                                    = "ipconfig${count.index}"
   100      subnet_id                               = "${azurerm_subnet.db_subnet.id}"
   101      private_ip_address_allocation           = "Static"
   102      private_ip_address                      = "10.0.1.${count.index + 4}"
   103      load_balancer_backend_address_pools_ids = ["${azurerm_lb_backend_address_pool.backend_pool.id}"]
   104  
   105      load_balancer_inbound_nat_rules_ids = [
   106        "${element(azurerm_lb_nat_rule.NatRule0.*.id, count.index)}",
   107        "${element(azurerm_lb_nat_rule.MySQLNatRule0.*.id, count.index)}",
   108        "${element(azurerm_lb_nat_rule.ProbeNatRule0.*.id, count.index)}",
   109      ]
   110    }
   111  }
   112  
   113  # **********************  LOAD BALANCER ********************** #
   114  resource "azurerm_lb" "lb" {
   115    name                = "${var.dns_name}-lb"
   116    location            = "${azurerm_resource_group.rg.location}"
   117    resource_group_name = "${azurerm_resource_group.rg.name}"
   118    depends_on          = ["azurerm_public_ip.pip"]
   119  
   120    frontend_ip_configuration {
   121      name                 = "${var.dns_name}-sshIPCfg"
   122      public_ip_address_id = "${azurerm_public_ip.pip.id}"
   123    }
   124  }
   125  
   126  resource "azurerm_lb_backend_address_pool" "backend_pool" {
   127    resource_group_name = "${azurerm_resource_group.rg.name}"
   128    loadbalancer_id     = "${azurerm_lb.lb.id}"
   129    name                = "${var.dns_name}-ilbBackendPool"
   130  }
   131  
   132  # **********************  LOAD BALANCER INBOUND NAT RULES ********************** #
   133  resource "azurerm_lb_nat_rule" "NatRule0" {
   134    name                           = "${var.dns_name}-NatRule-${count.index}"
   135    resource_group_name            = "${azurerm_resource_group.rg.name}"
   136    loadbalancer_id                = "${azurerm_lb.lb.id}"
   137    protocol                       = "tcp"
   138    frontend_port                  = "6400${count.index + 1}"
   139    backend_port                   = 22
   140    frontend_ip_configuration_name = "${var.dns_name}-sshIPCfg"
   141    count                          = "${var.node_count}"
   142    depends_on                     = ["azurerm_lb.lb"]
   143  }
   144  
   145  resource "azurerm_lb_nat_rule" "MySQLNatRule0" {
   146    name                           = "${var.dns_name}-MySQLNatRule-${count.index}"
   147    resource_group_name            = "${azurerm_resource_group.rg.name}"
   148    loadbalancer_id                = "${azurerm_lb.lb.id}"
   149    protocol                       = "tcp"
   150    frontend_port                  = "330${count.index + 6}"
   151    backend_port                   = 3306
   152    frontend_ip_configuration_name = "${var.dns_name}-sshIPCfg"
   153    count                          = "${var.node_count}"
   154    depends_on                     = ["azurerm_lb.lb"]
   155  }
   156  
   157  resource "azurerm_lb_nat_rule" "ProbeNatRule0" {
   158    name                           = "${var.dns_name}-ProbeNatRule-${count.index}"
   159    resource_group_name            = "${azurerm_resource_group.rg.name}"
   160    loadbalancer_id                = "${azurerm_lb.lb.id}"
   161    protocol                       = "tcp"
   162    frontend_port                  = "920${count.index}"
   163    backend_port                   = 9200
   164    frontend_ip_configuration_name = "${var.dns_name}-sshIPCfg"
   165    count                          = "${var.node_count}"
   166    depends_on                     = ["azurerm_lb.lb"]
   167  }
   168  
   169  # ********************** VIRTUAL MACHINES ********************** #
   170  resource "azurerm_virtual_machine" "vm" {
   171    name                  = "${var.dns_name}${count.index}"
   172    resource_group_name   = "${azurerm_resource_group.rg.name}"
   173    location              = "${azurerm_resource_group.rg.location}"
   174    vm_size               = "${var.vm_size}"
   175    network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]
   176    count                 = "${var.node_count}"
   177    availability_set_id   = "${azurerm_availability_set.availability_set.id}"
   178    depends_on            = ["azurerm_availability_set.availability_set", "azurerm_network_interface.nic", "azurerm_storage_account.stor"]
   179  
   180    storage_image_reference {
   181      publisher = "${var.image_publisher}"
   182      offer     = "${var.image_offer}"
   183      sku       = "${var.os_version}"
   184      version   = "latest"
   185    }
   186  
   187    storage_os_disk {
   188      name          = "osdisk${count.index}"
   189      vhd_uri       = "https://${azurerm_storage_account.stor.name}.blob.core.windows.net/vhds/${var.dns_name}${count.index}-osdisk.vhd"
   190      create_option = "FromImage"
   191      caching       = "ReadWrite"
   192    }
   193  
   194    os_profile {
   195      computer_name  = "${var.dns_name}${count.index}"
   196      admin_username = "${var.vm_admin_username}"
   197      admin_password = "${var.vm_admin_password}"
   198    }
   199  
   200    storage_data_disk {
   201      name          = "datadisk1"
   202      vhd_uri       = "https://${azurerm_storage_account.stor.name}.blob.core.windows.net/vhds/${var.dns_name}${count.index}-datadisk1.vhd"
   203      disk_size_gb  = "1000"
   204      create_option = "Empty"
   205      lun           = 0
   206    }
   207  
   208    storage_data_disk {
   209      name          = "datadisk2"
   210      vhd_uri       = "https://${azurerm_storage_account.stor.name}.blob.core.windows.net/vhds/${var.dns_name}${count.index}-datadisk2.vhd"
   211      disk_size_gb  = "1000"
   212      create_option = "Empty"
   213      lun           = 1
   214    }
   215  
   216    os_profile_linux_config {
   217      disable_password_authentication = false
   218    }
   219  }
   220  
   221  resource "azurerm_virtual_machine_extension" "setup_mysql" {
   222    name                       = "${var.dns_name}-${count.index}-setupMySQL"
   223    resource_group_name        = "${azurerm_resource_group.rg.name}"
   224    location                   = "${azurerm_resource_group.rg.location}"
   225    virtual_machine_name       = "${element(azurerm_virtual_machine.vm.*.name, count.index)}"
   226    publisher                  = "Microsoft.Azure.Extensions"
   227    type                       = "CustomScript"
   228    type_handler_version       = "2.0"
   229    auto_upgrade_minor_version = true
   230    count                      = "${var.node_count}"
   231    depends_on                 = ["azurerm_virtual_machine.vm", "azurerm_lb_nat_rule.ProbeNatRule0"]
   232  
   233    settings = <<SETTINGS
   234  {
   235    "fileUris": ["${var.artifacts_location}${var.azuremysql_script}"]
   236  }
   237  SETTINGS
   238  
   239    protected_settings = <<SETTINGS
   240   {
   241     "commandToExecute": "bash azuremysql.sh ${count.index + 1} 10.0.1.${count.index + 4} ${var.artifacts_location}${var.mysql_cfg_file_path} '${var.mysql_replication_password}' '${var.mysql_root_password}' '${var.mysql_probe_password}' 10.0.1.4 ${var.unique_prefix}wordpress"
   242   }
   243  SETTINGS
   244  }