-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
stack72
merged 45 commits into
hashicorp:master
from
cloudbooster:hashicorp-101-vm-from-user-image
May 11, 2017
Merged
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 6f577a8
changed branch name
anniehedgpeth 611d70a
not deploying - storage problems
anniehedgpeth fdff23a
provisions vm but image not properly prepared
anniehedgpeth 0dac5ec
storage not correct
anniehedgpeth 81e2559
provisions properly
anniehedgpeth 7dc8150
changed main.tf to azuredeploy.tf
anniehedgpeth 5c079fa
added tfvars and info for README
anniehedgpeth c86ef6f
tfvars ignored and corrected file ext
anniehedgpeth 534b236
added CI config; added sane defaults for variables; updated deploymen…
0cf888b
deploy.sh to be executable
68f0003
executable deploy files
c898510
added CI files; changed vars
anniehedgpeth 9d1541c
merge upstream/master
5c8ecd8
prep for PR
0267fca
removal of old folder
f9bf6ba
prep for PR
cec0679
wrong args for travis
d78e8bd
more PR prep
6bf2df2
updated README
anniehedgpeth b22cff0
commented out variables in terraform.tfvars
09175de
Merge branch 'topic-101-vm-from-user-image' of https://github.com/10t…
anniehedgpeth 0843f45
Topic 101 vm from user image (#2)
scottzilla 49caea5
added new template
2236828
oops, left off master
27167b5
Merge branch 'master' of github.com:harijayms/terraform
5b8f7bb
merge upstream; prep for PR
7f8fe25
prep for PR
eecb2d2
correct repository for destination
92e341d
renamed scripts to be more intuitive; added check for docker
0fdf4cb
merge upstream
c98b1d1
consolidated deploy and after_deploy into a single script; simplified…
0ef07bc
added terraform show
5e99f81
added az cli check
ce1f573
merge upstream master
65826c1
on this branch, only build test_dir; master will aggregate all the ex…
3523ed3
suppress az login output
21837a9
forgot about line breaks
e1e3aa2
breaking build as an example
a0a7643
fixing broken build example
487d9cd
fixed grammar in readme
ca1db2f
prepped for Hashicorp master merge
anniehedgpeth c8f5dc1
typo fix
anniehedgpeth d1a29d4
changed password variable description
anniehedgpeth 5ae00a9
added graph to README
anniehedgpeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 \ | ||
-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;" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.