Unverified Commit bb47e770 authored by Stefan Scherer's avatar Stefan Scherer
Browse files

Add Terraform template for Packer build in Azure

parent 90f27552
<FirstLogonCommands>
<SynchronousCommand>
<CommandLine>cmd /c "copy C:\AzureData\CustomData.bin C:\provision.ps1"</CommandLine
><Description>CopyScript</Description>
<Order>11</Order>
</SynchronousCommand>
<SynchronousCommand>
<CommandLine>powershell.exe -sta -ExecutionPolicy Unrestricted -file C:\provision.ps1</CommandLine
><Description>RunScript</Description>
<Order>12</Order>
</SynchronousCommand>
</FirstLogonCommands>
# Packer builder in Azure
This is a Terraform template to spin up a VM in Azure that has nested HyperV
activated and tools like Packer, Vagrant and Docker CLI installed.
Now you are able to build Vagrant base boxes for HyperV in the Cloud with Packer.
## Install Terraform
```
brew install terraform
```
## Secrets
Get your Azure ID's and secret with `pass`
```
eval $(pass azure-terraform)
```
You will need these environment variables for terraform
```
export ARM_SUBSCRIPTION_ID="uuid"
export ARM_CLIENT_ID="uuid"
export ARM_CLIENT_SECRET="secret"
export ARM_TENANT_ID="uuid"
```
## Configure
Adjust the file `variables.tf` to your needs to choose
- location / region
- DNS prefix and suffix
- size of the VM's, default is `Standard_D2_v3`
- username and password
## Plan
```bash
terraform plan
```
## Create / Apply
```bash
terraform apply
```
## Destroy
```bash
terraform destroy
```
# Configure the Microsoft Azure Provider
provider "azurerm" {}
# Create a resource group
resource "azurerm_resource_group" "global" {
location = "${var.location}"
name = "${var.account}"
}
# Create a storage account
resource "azurerm_storage_account" "global" {
account_type = "Standard_LRS" # Only locally redundant
location = "${var.location}"
name = "${var.account}"
resource_group_name = "${azurerm_resource_group.global.name}"
}
Start-Transcript -Path C:\provision.log
function Get-HostToIP($hostname) {
$result = [system.Net.Dns]::GetHostByName($hostname)
$result.AddressList | ForEach-Object {$_.IPAddressToString }
}
Write-Host "provision.ps1"
Write-Host "HostName = $($HostName)"
Write-Host Windows Updates to manual
Cscript $env:WinDir\System32\SCregEdit.wsf /AU 1
Net stop wuauserv
Net start wuauserv
Write-Host Disable Windows Defender
Set-MpPreference -DisableRealtimeMonitoring $true
Write-Host Do not open Server Manager at logon
New-ItemProperty -Path HKCU:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value "1" -Force
Write-Host Install Chocolatey
iex (wget 'https://chocolatey.org/install.ps1' -UseBasicParsing)
Write-Host Install editors
choco install -y atom
Write-Host Install Git
choco install -y git
Write-Host Install Packer
choco install -y packer
Write-Host Install Vagrant
choco install -y vagrant
Write-Host Install Docker
choco install -y docker
Write-Host Install HyperV
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
Install-WindowsFeature Hyper-V-Tools
Install-WindowsFeature Hyper-V-PowerShell
Write-Host Disable autologon
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -PropertyType DWORD -Value "0" -Force
Write-Host Install all Windows Updates
Get-Content C:\windows\system32\en-us\WUA_SearchDownloadInstall.vbs | ForEach-Object {
$_ -replace 'confirm = msgbox.*$', 'confirm = vbNo'
} | Out-File $env:TEMP\WUA_SearchDownloadInstall.vbs
"a`na" | cscript $env:TEMP\WUA_SearchDownloadInstall.vbs
Write-Host Cleaning up
Remove-Item C:\provision.ps1
Write-Host Restarting computer
Restart-Computer
# Settings
variable "account" {
default = "pckr"
}
variable "dns_prefix" {
default = "pckr"
}
variable "location" {
// default = "northeurope"
default = "westeurope"
}
variable "azure_dns_suffix" {
description = "Azure DNS suffix for the Public IP"
default = "cloudapp.azure.com"
}
variable "admin_username" {
default = "vagrant"
}
variable "admin_password" {
default = "Password1234!"
}
variable "count" {
type = "map"
default = {
windows = "1"
}
}
variable "vm_size" {
default = "Standard_D2_v3"
}
resource "azurerm_virtual_network" "windows" {
name = "windows-virtnet"
address_space = ["10.0.0.0/16"]
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.global.name}"
}
resource "azurerm_subnet" "windows" {
name = "windows-${format("%02d", count.index + 1)}-sn"
resource_group_name = "${azurerm_resource_group.global.name}"
virtual_network_name = "${azurerm_virtual_network.windows.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "windows" {
count = "${var.count["windows"]}"
name = "windows-${format("%02d", count.index + 1)}-nic"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.global.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.windows.id}"
public_ip_address_id = "${element(azurerm_public_ip.windows.*.id, count.index)}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_public_ip" "windows" {
count = "${var.count["windows"]}"
domain_name_label = "${var.dns_prefix}-win-${format("%02d", count.index + 1)}"
idle_timeout_in_minutes = 30
location = "${var.location}"
name = "windows-${format("%02d", count.index + 1)}-publicip"
public_ip_address_allocation = "dynamic"
resource_group_name = "${azurerm_resource_group.global.name}"
}
resource "azurerm_storage_container" "windows" {
container_access_type = "private"
count = "${var.count["windows"]}"
name = "windows-${format("%02d", count.index + 1)}-storage"
resource_group_name = "${azurerm_resource_group.global.name}"
storage_account_name = "${azurerm_storage_account.global.name}"
}
resource "azurerm_virtual_machine" "windows" {
count = "${var.count["windows"]}"
name = "windows-${format("%02d", count.index + 1)}-vm"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.global.name}"
network_interface_ids = ["${element(azurerm_network_interface.windows.*.id, count.index)}"]
vm_size = "${var.vm_size}"
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter-with-Containers"
version = "latest"
}
storage_os_disk {
name = "windows-${format("%02d", count.index + 1)}-osdisk"
vhd_uri = "${azurerm_storage_account.global.primary_blob_endpoint}${element(azurerm_storage_container.windows.*.id, count.index)}/disk1.vhd"
caching = "ReadWrite"
create_option = "FromImage"
}
os_profile {
computer_name = "${var.dns_prefix}-win-${format("%02d", count.index + 1)}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
custom_data = "${base64encode("Param($HostName = \"${var.dns_prefix}-win-${format("%02d", count.index + 1)}.${var.location}.${var.azure_dns_suffix}\") ${file("./provision.ps1")}")}"
}
os_profile_windows_config {
provision_vm_agent = true
enable_automatic_upgrades = true
additional_unattend_config {
pass = "oobeSystem"
component = "Microsoft-Windows-Shell-Setup"
setting_name = "AutoLogon"
content = "<AutoLogon><Password><Value>${var.admin_password}</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${var.admin_username}</Username></AutoLogon>"
}
additional_unattend_config {
pass = "oobeSystem"
component = "Microsoft-Windows-Shell-Setup"
setting_name = "FirstLogonCommands"
content = "${file("./FirstLogonCommands.xml")}"
}
}
tags {
environment = "staging"
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment