provider/azurerm: Add example of vm specialized vhd existing vnet (#14412)

* merge master

* added new constructs/naming for deploy scripts, etc.

* suppress az login output

* merge of CI config

* initial commit; not working yet

* initial commit - added files - draft

* troubleshooting image uri

* null_resource added

* updating simple linux with Hashicorp's master

* in progress

* small README edit

* in progress

* adding graph to README of azure-vnet-two-subnets

* added graph to azure-vm-simple-linux-managed-disk

* cleanup

* chmod on deploy.ci.sh

* in progress; changed uri variables

* adding variables to deploy scripts

* chmod deploy.mac.sh

* in progress; troubleshooting variables

* deploys locally successfully

* returning cleanup to deploy

* typo on deploy.ci.sh

* added graph to README

* changed the subnet_id variable in ci

* reverting to Hashicorp's travis.yml
This commit is contained in:
Annie Hedgpeth 2017-05-15 11:41:06 -05:00 committed by Paul Stack
parent 583c38a088
commit 9fac441ae2
11 changed files with 288 additions and 2 deletions

View File

@ -18,3 +18,5 @@ If a `terraform.tfvars` file is present in the current directory, Terraform auto
## variables.tf ## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template. The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
![graph](/examples/azure-vm-simple-linux-managed-disk/graph.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 KiB

View File

@ -0,0 +1,35 @@
# Create a specialized virtual machine in an existing virtual network [![Build Status](https://travis-ci.org/harijayms/terraform.svg?branch=topic-201-vm-specialized-vhd-existing-vnet)](https://travis-ci.org/harijayms/terraform)
This Terraform template was based on [this](https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-specialized-vhd-existing-vnet) Azure Quickstart Template. Changes to the ARM template that may have occurred since the creation of this example may not be reflected in this Terraform template.
## Prerequisites
- VHD file from which to create a VM that already exists in a storage account
- Name of the existing VNET and subnet to which the new virtual machine will connect
- Name of the Resource Group in which the VNET resides
### NOTE
This template will create an additional Standard_GRS storage account for enabling boot diagnostics each time you execute this template. To avoid running into storage account limits, it is best to delete the storage account when the VM is deleted.
This template creates a VM from a specialized VHD and lets you connect it to an existing VNET that can reside in a different Resource Group from which the virtual machine resides.
_Please note: This deployment template does not create or attach an existing Network Security Group to the virtual machine._
## main.tf
The `main.tf` file contains the actual resources that will be deployed. It also contains the Azure Resource Group definition and any defined variables.
## outputs.tf
This data is outputted when `terraform apply` is called, and can be queried using the `terraform output` command.
## provider.tf
Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file.
## terraform.tfvars
If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it.
## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
![graph](/examples/azure-vm-specialized-vhd-existing-vnet/graph.png)

View File

@ -0,0 +1,55 @@
#!/bin/bash
set -o errexit -o nounset
docker run --rm -it \
-e ARM_CLIENT_ID \
-e ARM_CLIENT_SECRET \
-e ARM_SUBSCRIPTION_ID \
-e ARM_TENANT_ID \
-v $(pwd):/data \
--workdir=/data \
--entrypoint "/bin/sh" \
hashicorp/terraform:light \
-c "/bin/terraform get; \
/bin/terraform validate; \
/bin/terraform plan -out=out.tfplan \
-var os_disk_vhd_uri=$EXISTING_IMAGE_URI \
-var hostname=$KEY \
-var resource_group=$KEY \
-var existing_vnet_resource_group=$EXISTING_RESOURCE_GROUP \
-var admin_password=$PASSWORD \
-var existing_subnet_id=/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/permanent/providers/Microsoft.Network/virtualNetworks/$EXISTING_VIRTUAL_NETWORK_NAME/subnets/$EXISTING_SUBNET_NAME \
-var existing_subnet_name=$EXISTING_SUBNET_NAME \
-var existing_virtual_network_name=$EXISTING_VIRTUAL_NETWORK_NAME \
-var existing_storage_acct=$EXISTING_STORAGE_ACCOUNT_NAME; \
/bin/terraform apply out.tfplan; \
/bin/terraform show;"
# cleanup deployed azure resources via azure-cli
docker run --rm -it \
azuresdk/azure-cli-python \
sh -c "az login --service-principal -u $ARM_CLIENT_ID -p $ARM_CLIENT_SECRET --tenant $ARM_TENANT_ID > /dev/null; \
az vm show -g $KEY -n $KEY"
# cleanup deployed azure resources via terraform
docker run --rm -it \
-e ARM_CLIENT_ID \
-e ARM_CLIENT_SECRET \
-e ARM_SUBSCRIPTION_ID \
-e ARM_TENANT_ID \
-v $(pwd):/data \
--workdir=/data \
--entrypoint "/bin/sh" \
hashicorp/terraform:light \
-c "/bin/terraform destroy -force \
-var os_disk_vhd_uri=$EXISTING_IMAGE_URI \
-var hostname=$KEY \
-var resource_group=$KEY \
-var existing_vnet_resource_group=$EXISTING_RESOURCE_GROUP \
-var admin_password=$PASSWORD \
-var existing_subnet_id=$EXISTING_SUBNET_ID \
-var existing_subnet_name=$EXISTING_SUBNET_NAME \
-var existing_virtual_network_name=$EXISTING_VIRTUAL_NETWORK_NAME \
-var existing_storage_acct=$EXISTING_STORAGE_ACCOUNT_NAME \
-target=azurerm_resource_group.rg"

View File

@ -0,0 +1,20 @@
#!/bin/bash
set -o errexit -o nounset
if docker -v; then
# generate a unique string for CI deployment
export KEY=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-z' | head -c 12)
export PASSWORD=$KEY$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'A-Z' | head -c 2)$(cat /dev/urandom | env LC_CTYPE=C tr -cd '0-9' | head -c 2)
export EXISTING_RESOURCE_GROUP=permanent
export EXISTING_IMAGE_URI=https://permanentstor.blob.core.windows.net/permanent-vhds/permanent-osdisk1.vhd
export EXISTING_STORAGE_ACCOUNT_NAME=permanentstor
export EXISTING_VIRTUAL_NETWORK_NAME=vqeeopeictwmvnet
export EXISTING_SUBNET_NAME=vqeeopeictwmsubnet
/bin/sh ./deploy.ci.sh
else
echo "Docker is used to run terraform commands, please install before run: https://docs.docker.com/docker-for-mac/install/"
fi

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 KiB

View File

@ -0,0 +1,71 @@
# 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"
# }
resource "azurerm_resource_group" "rg" {
name = "${var.resource_group}"
location = "${var.location}"
}
resource "azurerm_public_ip" "pip" {
name = "PublicIp"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
public_ip_address_allocation = "Dynamic"
domain_name_label = "${var.hostname}"
}
resource "azurerm_network_interface" "nic" {
name = "nic"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
ip_configuration {
name = "ipconfig"
subnet_id = "${var.existing_subnet_id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.pip.id}"
}
}
resource "azurerm_storage_account" "stor" {
name = "${var.hostname}"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${var.location}"
account_type = "${var.storage_account_type}"
}
resource "azurerm_virtual_machine" "vm" {
name = "${var.hostname}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
vm_size = "${var.vm_size}"
network_interface_ids = ["${azurerm_network_interface.nic.id}"]
storage_os_disk {
name = "${var.hostname}osdisk1"
image_uri = "${var.os_disk_vhd_uri}"
vhd_uri = "https://${var.existing_storage_acct}.blob.core.windows.net/${var.existing_vnet_resource_group}-vhds/${var.hostname}osdisk.vhd"
os_type = "${var.os_type}"
caching = "ReadWrite"
create_option = "FromImage"
}
os_profile {
computer_name = "${var.hostname}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
}
os_profile_linux_config {
disable_password_authentication = false
}
boot_diagnostics {
enabled = true
storage_uri = "${azurerm_storage_account.stor.primary_blob_endpoint}"
}
}

View File

@ -0,0 +1,11 @@
output "hostname" {
value = "${var.hostname}"
}
output "vm_fqdn" {
value = "${azurerm_public_ip.pip.fqdn}"
}
output "ssh_command" {
value = "ssh ${var.admin_username}@${azurerm_public_ip.pip.fqdn}"
}

View File

@ -0,0 +1,90 @@
variable "resource_group" {
description = "Name of the resource group in which to deploy your new Virtual Machine"
}
variable "existing_vnet_resource_group" {
description = "Name of the existing resource group in which the existing vnet resides"
}
variable "location" {
description = "The location/region where the virtual network resides."
default = "southcentralus"
}
variable "hostname" {
description = "This variable is used in this template to create the domain name label as well as the virtual machine name. Must be unique."
}
variable "os_type" {
description = "Type of OS on the existing vhd. Allowed values: 'windows' or 'linux'."
default = "linux"
}
variable "os_disk_vhd_uri" {
description = "Uri of the existing VHD in ARM standard or premium storage"
}
variable "existing_storage_acct" {
description = "The name of the storage account in which your existing VHD and image reside"
}
variable "existing_virtual_network_name" {
description = "The name for the existing virtual network"
}
variable "existing_subnet_name" {
description = "The name for the existing subnet in the existing virtual network"
}
variable "existing_subnet_id" {
description = "The id for the existing subnet in the existing virtual network"
}
variable "address_space" {
description = "The address space that is used by the virtual network. You can supply more than one address space. Changing this forces a new resource to be created."
default = "10.0.0.0/16"
}
variable "subnet_prefix" {
description = "The address prefix to use for the subnet."
default = "10.0.10.0/24"
}
variable "storage_account_type" {
description = "Defines the type of storage account to be created. Valid options are Standard_LRS, Standard_ZRS, Standard_GRS, Standard_RAGRS, Premium_LRS. Changing this is sometimes valid - see the Azure documentation for more information on which types of accounts can be converted into other types."
default = "Standard_GRS"
}
variable "vm_size" {
description = "Specifies the size of the virtual machine."
default = "Standard_DS1_v2"
}
variable "image_publisher" {
description = "name of the publisher of the image (az vm image list)"
default = "Canonical"
}
variable "image_offer" {
description = "the name of the offer (az vm image list)"
default = "UbuntuServer"
}
variable "image_sku" {
description = "image sku to apply (az vm image list)"
default = "16.04-LTS"
}
variable "image_version" {
description = "version of the image to apply (az vm image list)"
default = "latest"
}
variable "admin_username" {
description = "administrator user name"
default = "vmadmin"
}
variable "admin_password" {
description = "administrator password (recommended to disable password auth)"
}

View File

@ -16,3 +16,5 @@ If a `terraform.tfvars` file is present in the current directory, Terraform auto
## variables.tf ## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template. The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
![graph](/examples/azure-vnet-two-subnets/graph.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB