Skip to content

Instantly share code, notes, and snippets.

@rohrerb
Last active August 16, 2018 16:25
Show Gist options
  • Save rohrerb/4b449b778a6d902014f580a34e7390df to your computer and use it in GitHub Desktop.
Save rohrerb/4b449b778a6d902014f580a34e7390df to your computer and use it in GitHub Desktop.
module "x11x" {
source = "module/linux"
environment_code = "t"
deployment_code = "us1"
location_code = "e11"
instance_count = 1
data_disk_count = 1
data_disk_size = 5
vm_code = "x11x"
}
locals {
base_hostname = "${format("%s%s%s%s", var.environment_code, var.deployment_code, var.location_code, var.vm_code)}"
}
provider "azurerm" {
subscription_id = "55546ce0-6c1c-40d0-98dd-007c68adfcd6"
}
resource "azurerm_resource_group" "rg" {
name = "${format("%s%s%s-Web", upper(var.environment_code), upper(var.deployment_code), upper(var.location_code))}"
location = "eastus"
}
resource "azurerm_virtual_network" "network" {
name = "myVnet"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
}
resource "azurerm_subnet" "subnet" {
name = "mySubnet"
resource_group_name = "${azurerm_resource_group.rg.name}"
virtual_network_name = "${azurerm_virtual_network.network.name}"
address_prefix = "10.0.1.0/24"
}
resource "azurerm_network_interface" "nic" {
count = "${var.instance_count}"
name = "${format("%s%03dNetworkInterface", local.base_hostname, count.index + 1)}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
ip_configuration {
name = "myNicConfiguration"
subnet_id = "${azurerm_subnet.subnet.id}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_availability_set" "av_set" {
name = "myAvSet"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
managed = true
platform_fault_domain_count = 2
platform_update_domain_count = 20
}
resource "azurerm_managed_disk" "data_disk" {
count = "${var.instance_count == 0 ? 0 : var.instance_count * var.data_disk_count}"
name = "${format("%s%03d-datadisk", local.base_hostname, (count.index / var.data_disk_count) + 1)}${format("%1d", count.index % var.data_disk_count + 1)}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "${var.data_disk_size}"
}
resource "azurerm_virtual_machine_data_disk_attachment" "data_disk_attach" {
count = "${var.instance_count == 0 ? 0 : var.instance_count * var.data_disk_count}"
virtual_machine_id = "${element(azurerm_virtual_machine.vm.*.id, ceil((count.index + 1) * 1.0 / var.data_disk_count) - 1)}"
managed_disk_id = "${element(azurerm_managed_disk.data_disk.*.id, count.index)}"
lun = "${floor((count.index +1) / ceil((count.index + 1) * 1.0 / var.data_disk_count)) - 1}"
caching = "ReadOnly"
create_option = "Attach"
}
resource "azurerm_virtual_machine" "vm" {
count = "${var.instance_count}"
name = "${format("%s%03d", local.base_hostname, count.index + 1)}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]
availability_set_id = "${azurerm_availability_set.av_set.id}"
vm_size = "Standard_A1"
storage_os_disk {
name = "${format("%s%03d", local.base_hostname, count.index + 1)}-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
version = "latest"
}
os_profile {
computer_name = "myvm"
admin_username = "azureuser"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/azureuser/.ssh/authorized_keys"
key_data = "ssh-rsa AAAA....TGV"
}
}
}
variable "environment_code" { default = "t"}
variable "deployment_code" { default = "us1"}
variable "location_code" { default = "ea1"}
variable "instance_count" { default = 1}
variable "data_disk_count" { default = 0}
variable "data_disk_size" { default = 5}
variable "vm_code" { default = ""}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment