diff --git a/examples/azure-vm-simple-linux-managed-disk/README.md b/examples/azure-vm-simple-linux-managed-disk/README.md index 7203b037af..42201eb445 100644 --- a/examples/azure-vm-simple-linux-managed-disk/README.md +++ b/examples/azure-vm-simple-linux-managed-disk/README.md @@ -17,4 +17,6 @@ Azure requires that an application is added to Azure Active Directory to generat 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. \ No newline at end of file +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) \ No newline at end of file diff --git a/examples/azure-vm-simple-linux-managed-disk/graph.png b/examples/azure-vm-simple-linux-managed-disk/graph.png new file mode 100644 index 0000000000..ea993bfc4d Binary files /dev/null and b/examples/azure-vm-simple-linux-managed-disk/graph.png differ diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/README.md b/examples/azure-vm-specialized-vhd-existing-vnet/README.md new file mode 100644 index 0000000000..9219798466 --- /dev/null +++ b/examples/azure-vm-specialized-vhd-existing-vnet/README.md @@ -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) \ No newline at end of file diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/deploy.ci.sh b/examples/azure-vm-specialized-vhd-existing-vnet/deploy.ci.sh new file mode 100755 index 0000000000..3ffdcaa216 --- /dev/null +++ b/examples/azure-vm-specialized-vhd-existing-vnet/deploy.ci.sh @@ -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" \ No newline at end of file diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/deploy.mac.sh b/examples/azure-vm-specialized-vhd-existing-vnet/deploy.mac.sh new file mode 100755 index 0000000000..6f555e7a6b --- /dev/null +++ b/examples/azure-vm-specialized-vhd-existing-vnet/deploy.mac.sh @@ -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 \ No newline at end of file diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/graph.png b/examples/azure-vm-specialized-vhd-existing-vnet/graph.png new file mode 100644 index 0000000000..14e4779df3 Binary files /dev/null and b/examples/azure-vm-specialized-vhd-existing-vnet/graph.png differ diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/main.tf b/examples/azure-vm-specialized-vhd-existing-vnet/main.tf new file mode 100644 index 0000000000..821ee8c31c --- /dev/null +++ b/examples/azure-vm-specialized-vhd-existing-vnet/main.tf @@ -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}" + } +} diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/outputs.tf b/examples/azure-vm-specialized-vhd-existing-vnet/outputs.tf new file mode 100644 index 0000000000..13768e554d --- /dev/null +++ b/examples/azure-vm-specialized-vhd-existing-vnet/outputs.tf @@ -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}" +} diff --git a/examples/azure-vm-specialized-vhd-existing-vnet/variables.tf b/examples/azure-vm-specialized-vhd-existing-vnet/variables.tf new file mode 100644 index 0000000000..4e53919a84 --- /dev/null +++ b/examples/azure-vm-specialized-vhd-existing-vnet/variables.tf @@ -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)" +} diff --git a/examples/azure-vnet-two-subnets/README.md b/examples/azure-vnet-two-subnets/README.md index 1f47743df5..dcd42e79e5 100644 --- a/examples/azure-vnet-two-subnets/README.md +++ b/examples/azure-vnet-two-subnets/README.md @@ -15,4 +15,6 @@ Azure requires that an application is added to Azure Active Directory to generat 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. \ No newline at end of file +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) \ No newline at end of file diff --git a/examples/azure-vnet-two-subnets/graph.png b/examples/azure-vnet-two-subnets/graph.png new file mode 100644 index 0000000000..1df101babc Binary files /dev/null and b/examples/azure-vnet-two-subnets/graph.png differ