Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: making the golden image example more consistent #910

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/vm-from-managed-image/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Project Title

Create 3 Virtual Machines form an Azure Managed Image (Golden Image)
# Virtual Machine from a Managed Image

Creates a Virtual Machine from an Azure Managed Image (Golden Image)
147 changes: 72 additions & 75 deletions examples/vm-from-managed-image/main.tf
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
provider "azurerm" {
subscription_id = "${var.subscription_id}"
client_id = "${var.client_id}"
client_secret = "${var.client_secret}"
tenant_id = "${var.tenant_id}"
}

#take a pointer to the custom image from our subscription
# provider "azurerm" {
# subscription_id = "REPLACE-WITH-YOUR-SUBSCRIPTION-ID"
# client_id = "REPLACE-WITH-YOUR-CLIENT-ID"
# client_secret = "REPLACE-WITH-YOUR-CLIENT-SECRET"
# tenant_id = "REPLACE-WITH-YOUR-TENANT-ID"
# }

locals {
virtual_machine_name = "${var.prefix}vm"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
#name of the existing Image
name = "${var.GoldenImage}"
#name of the existing RG where the Image is - must be in the same region!!!
resource_group_name = "${var.RgOfGoldenImage}"
name = "${var.image_name}"
resource_group_name = "${var.image_resource_group}"
}

output "image_id" {
value = "${data.azurerm_image.search.id}"
}


# Create an Azure resource group
resource "azurerm_resource_group" "rg" {
name = "${var.resource_group}"
# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = "${var.location}"
}

# Create a virtual network in the resource group
resource "azurerm_virtual_network" "vNet" {
name = "${var.customer_name}"
# Create a Virtual Network within the Resource Group
resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["172.16.0.0/16"]
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
location = "${azurerm_resource_group.main.location}"
}

# Create Subnet
resource "azurerm_subnet" "Subnet" {
name = "Subnet"
virtual_network_name = "${azurerm_virtual_network.vNet.name}"
resource_group_name = "${azurerm_resource_group.rg.name}"
# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
name = "internal"
virtual_network_name = "${azurerm_virtual_network.main.name}"
resource_group_name = "${azurerm_resource_group.main.name}"
address_prefix = "172.16.1.0/24"
}

# Create a public IP resource for VMs
resource "azurerm_public_ip" "vm-pip" {
count = 3
name = "VM-PIP-${count.index}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
public_ip_address_allocation = "dynamic"
}

# Create a network secuirty group with some rules
resource "azurerm_network_security_group" "nsg" {
name = "${var.customer_name}-NSG"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
}

# Create a Public IP for the Virtual Machine
resource "azurerm_public_ip" "main" {
name = "${var.prefix}-pip"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
public_ip_address_allocation = "dynamic"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
name = "${var.prefix}-nsg"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"

security_rule {
name = "allow_SSH"
Expand All @@ -67,8 +67,8 @@ resource "azurerm_network_security_group" "nsg" {
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {

security_rule {
name = "allow_RDP"
description = "Allow RDP access"
priority = 110
Expand All @@ -83,60 +83,57 @@ resource "azurerm_network_security_group" "nsg" {
}

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "vm-Nic" {
count = 3
name = "vm-Nic-${count.index}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
network_security_group_id = "${azurerm_network_security_group.nsg.id}"

resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_security_group_id = "${azurerm_network_security_group.main.id}"

ip_configuration {
name = "Nic-config-${count.index}"
subnet_id = "${azurerm_subnet.Subnet.id}"
name = "primary"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
public_ip_address_id = "${element(azurerm_public_ip.vm-pip.*.id, count.index)}"
}
public_ip_address_id = "${azurerm_public_ip.main.id}"
}
}


##### Create new virtual machine - 3 vms
# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
count = 3
name = "VM-${count.index}"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
network_interface_ids = ["${element(azurerm_network_interface.vm-Nic.*.id, count.index)}"]
vm_size = "Standard_E32s_v3"
delete_os_disk_on_termination = true
name = "${local.virtual_machine_name}"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_F2"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true

storage_image_reference {
id = "${data.azurerm_image.search.id}"
id = "${data.azurerm_image.search.id}"
}

storage_os_disk {
name = "vm-OS-${count.index}"
name = "${local.virtual_machine_name}-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
disk_size_gb = "40"
disk_size_gb = "40"
}

storage_data_disk {
name = "vm-Data-Disk-${count.index}"
managed_disk_type = "Premium_LRS"
create_option = "Empty"
lun = 0
disk_size_gb = "1024"
}
name = "${local.virtual_machine_name}-data1"
managed_disk_type = "Premium_LRS"
create_option = "Empty"
lun = 0
disk_size_gb = "1024"
}

os_profile {
computer_name = "VM-${count.index}"
computer_name = "${local.virtual_machine_name}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
}

os_profile_linux_config {
disable_password_authentication = false
}
}
}
28 changes: 9 additions & 19 deletions examples/vm-from-managed-image/variables.tf
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
variable "subscription_id" {}
variable "client_id" {}
variable "client_secret" {}
variable "tenant_id" {}


variable "GoldenImage" {
description ="name of the existing Golden Image"
variable "image_name" {
description = "The name of the existing Golden Image"
}

variable "RgOfGoldenImage" {
description = "name of the existing RG where the Image is - must be in the same region!!!"
variable "image_resource_group" {
description = "The name of the Resource Group where the Golden Image is located."
}

variable "resource_group" {
description = "The name of the resource group in which to create the virtual network."
variable "prefix" {
description = "The prefix used for any resources used, must be an alphanumberic string"
}

variable "location" {
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
}

variable "customer_name" {
description = "The name of the customer. will be the name of the vNet."
description = "The location where the Resources will be provisioned. This needs to be the same as where the Image exists."
}

variable "admin_username" {
description = "the local user name of the VM"
description = "The username associated with the local administrator account on the Virtual Machine"
}

variable "admin_password" {
description = "the local password name of the VM"
description = "The password associated with the local administrator account on the Virtual Machine"
}