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

provider/azurerm: Add example of vm from user image #14231

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
9f0d990
initial commit - 101-vm-from-user-image
anniehedgpeth Apr 18, 2017
6f577a8
changed branch name
anniehedgpeth Apr 19, 2017
611d70a
not deploying - storage problems
anniehedgpeth Apr 21, 2017
fdff23a
provisions vm but image not properly prepared
anniehedgpeth Apr 21, 2017
0dac5ec
storage not correct
anniehedgpeth Apr 21, 2017
81e2559
provisions properly
anniehedgpeth Apr 22, 2017
7dc8150
changed main.tf to azuredeploy.tf
anniehedgpeth Apr 22, 2017
5c079fa
added tfvars and info for README
anniehedgpeth Apr 22, 2017
c86ef6f
tfvars ignored and corrected file ext
anniehedgpeth Apr 22, 2017
534b236
added CI config; added sane defaults for variables; updated deploymen…
Apr 24, 2017
0cf888b
deploy.sh to be executable
Apr 24, 2017
68f0003
executable deploy files
Apr 24, 2017
c898510
added CI files; changed vars
anniehedgpeth Apr 25, 2017
9d1541c
merge upstream/master
Apr 25, 2017
5c8ecd8
prep for PR
Apr 25, 2017
0267fca
removal of old folder
Apr 25, 2017
f9bf6ba
prep for PR
Apr 25, 2017
cec0679
wrong args for travis
Apr 25, 2017
d78e8bd
more PR prep
Apr 26, 2017
6bf2df2
updated README
anniehedgpeth Apr 26, 2017
b22cff0
commented out variables in terraform.tfvars
Apr 26, 2017
09175de
Merge branch 'topic-101-vm-from-user-image' of https://github.com/10t…
anniehedgpeth Apr 26, 2017
0843f45
Topic 101 vm from user image (#2)
scottzilla Apr 26, 2017
49caea5
added new template
Apr 26, 2017
2236828
oops, left off master
Apr 26, 2017
27167b5
Merge branch 'master' of github.com:harijayms/terraform
Apr 26, 2017
5b8f7bb
merge upstream; prep for PR
Apr 26, 2017
7f8fe25
prep for PR
Apr 27, 2017
eecb2d2
correct repository for destination
Apr 27, 2017
92e341d
renamed scripts to be more intuitive; added check for docker
Apr 27, 2017
0fdf4cb
merge upstream
Apr 27, 2017
c98b1d1
consolidated deploy and after_deploy into a single script; simplified…
Apr 27, 2017
0ef07bc
added terraform show
Apr 27, 2017
5e99f81
added az cli check
Apr 28, 2017
ce1f573
merge upstream master
Apr 28, 2017
65826c1
on this branch, only build test_dir; master will aggregate all the ex…
Apr 28, 2017
3523ed3
suppress az login output
Apr 28, 2017
21837a9
forgot about line breaks
Apr 28, 2017
e1e3aa2
breaking build as an example
Apr 28, 2017
a0a7643
fixing broken build example
Apr 28, 2017
487d9cd
fixed grammar in readme
May 1, 2017
ca1db2f
prepped for Hashicorp master merge
anniehedgpeth May 5, 2017
c8f5dc1
typo fix
anniehedgpeth May 5, 2017
d1a29d4
changed password variable description
anniehedgpeth May 8, 2017
5ae00a9
added graph to README
anniehedgpeth May 10, 2017
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
26 changes: 26 additions & 0 deletions examples/azure-vm-from-user-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# [Create a Virtual Machine from a User Image](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cli-deploy-templates#create-a-custom-vm-image)

This Terraform template was based on [this](https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-from-user-image) Azure Quickstart Template. Changes to the ARM template that may have occurred since the creation of this example may not be reflected here.

> Prerequisite - The generalized image VHD should exist, as well as a Storage Account for boot diagnostics

This template allows you to create a Virtual Machine from an unmanaged User image vhd. This template also deploys a Virtual Network, Public IP addresses and a Network Interface.

## 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.

If you are committing this template to source control, please insure that you add this file to your `.gitignore` file.

## 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-from-user-image/graph.png)
45 changes: 45 additions & 0 deletions examples/azure-vm-from-user-image/deploy.ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/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 hostname=$KEY -var resource_group=$EXISTING_RESOURCE_GROUP -var admin_username=$KEY -var admin_password=$PASSWORD -var image_uri=$EXISTING_IMAGE_URI -var storage_account_name=$EXISTING_STORAGE_ACCOUNT_NAME; \
/bin/terraform apply out.tfplan; \
/bin/terraform show;"

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 --name $KEY --resource-group permanent"

# 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 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we shouldn't need to specify the individual resources as targets here, as deleting the resource group will force the deletion of any child resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tombuildsstuff , we did that because we are targeting specific resources for our CI process, because we depend on an image in that resource group, and this helps to ensure that we don't remove it in addition to other mechanisms in place.

-var hostname=$KEY \
-var resource_group=$EXISTING_RESOURCE_GROUP \
-var admin_username=$KEY \
-var admin_password=$PASSWORD \
-var image_uri=$EXISTING_IMAGE_URI \
-var storage_account_name=$EXISTING_STORAGE_ACCOUNT_NAME \
-target=azurerm_virtual_machine.vm \
-target=azurerm_network_interface.nic \
-target=azurerm_virtual_network.vnet \
-target=azurerm_public_ip.pip;"
18 changes: 18 additions & 0 deletions examples/azure-vm-from-user-image/deploy.mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/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_IMAGE_URI=https://permanentstor.blob.core.windows.net/permanent-vhds/permanent-osdisk1.vhd
export EXISTING_STORAGE_ACCOUNT_NAME=permanentstor
export EXISTING_RESOURCE_GROUP=permanent

/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 added examples/azure-vm-from-user-image/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions examples/azure-vm-from-user-image/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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_virtual_network" "vnet" {
name = "${var.hostname}vnet"
location = "${var.location}"
address_space = ["${var.address_space}"]
resource_group_name = "${azurerm_resource_group.rg.name}"
}

resource "azurerm_subnet" "subnet" {
name = "${var.hostname}subnet"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.rg.name}"
address_prefix = "${var.subnet_prefix}"
}

resource "azurerm_network_interface" "nic" {
name = "${var.hostname}nic"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"

ip_configuration {
name = "${var.hostname}ipconfig"
subnet_id = "${azurerm_subnet.subnet.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.pip.id}"
}
}

resource "azurerm_public_ip" "pip" {
name = "${var.hostname}-ip"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
public_ip_address_allocation = "Dynamic"
domain_name_label = "${var.hostname}"
}

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.image_uri}"
vhd_uri = "https://${var.storage_account_name}.blob.core.windows.net/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
}
}
11 changes: 11 additions & 0 deletions examples/azure-vm-from-user-image/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "hostname" {
value = "${var.hostname}"
}

output "vm_fqdn" {
value = "${azurerm_public_ip.pip.fqdn}"
}

output "ssh_command" {
value = "${concat("ssh ", var.admin_username, "@", azurerm_public_ip.pip.fqdn)}"
}
55 changes: 55 additions & 0 deletions examples/azure-vm-from-user-image/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
variable "resource_group" {
description = "The name of the resource group in which the image to clone resides."
default = "myrg"
}

variable "image_uri" {
description = "Specifies the image_uri in the form publisherName:offer:skus:version. image_uri can also specify the VHD uri of a custom VM image to clone."
}

variable "os_type" {
description = "Specifies the operating system Type, valid values are windows, linux."
default = "linux"
}

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

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/24"
}

variable "subnet_prefix" {
description = "The address prefix to use for the subnet."
default = "10.0.0.0/24"
}

variable "storage_account_name" {
description = "The name of the storage account in which the image from which you are cloning resides."
}

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 = "Premium_LRS"
}

variable "vm_size" {
description = "Specifies the size of the virtual machine. This must be the same as the vm image from which you are copying."
default = "Standard_DS1_v2"
}

variable "hostname" {
description = "VM name referenced also in storage-related names. This is also used as the label for the Domain Name and to make up the FQDN. If a domain name label is specified, an A DNS record is created for the public IP in the Microsoft Azure DNS system."
}

variable "admin_username" {
description = "administrator user name"
default = "vmadmin"
}

variable "admin_password" {
description = "The Password for the account specified in the 'admin_username' field. We recommend disabling Password Authentication in a Production environment."
}
3 changes: 0 additions & 3 deletions examples/azure-vnet-two-subnets/.gitignore

This file was deleted.