Skip to content

Instantly share code, notes, and snippets.

@ajomathew
Created May 24, 2020 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajomathew/e9bb445bea6382a74e0bac3decf24d0f to your computer and use it in GitHub Desktop.
Save ajomathew/e9bb445bea6382a74e0bac3decf24d0f to your computer and use it in GitHub Desktop.
To deploy Azure SQL VM using terraform - ARM Template referenced https://gist.github.com/ajomathew/a3bf1bc6ce00e9e6967af917ae20c222
# Provider Def
provider "azurerm" {
features {
virtual_machine {
delete_os_disk_on_deletion = true
}
}
version = "2.10"
}
provider "template" {
version = "~> 2.1"
}
terraform {
required_version = "~>0.12.0"
}
# Resource Group
resource "azurerm_resource_group" "sqlvm-rg" {
name = var.resourceGroupName
location = var.location
tags = {
environment = var.-prefix
}
}
# Vnet Def
resource azurerm_virtual_network "vnet" {
name = "vnet"
resource_group_name = azurerm_resource_group.sqlvm-rg.name
location = azurerm_resource_group.sqlvm-rg.location
address_space = [var.vnet-address-space]
}
# Subnet Def
resource "azurerm_subnet" "subnet" {
name = "snet"
resource_group_name = azurerm_resource_group.sqlvm-rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet-address-space]
}
#Public Ip Def
resource "azurerm_public_ip" "vm-pip" {
name = "pip"
location = azurerm_resource_group.sqlvm-rg.location
resource_group_name = azurerm_resource_group.sqlvm-rg.name
allocation_method = "Dynamic"
}
# Nic Def
resource "azurerm_network_interface" "vm-nic" {
name = "nic"
resource_group_name = azurerm_resource_group.sqlvm-rg.name
location = azurerm_resource_group.sqlvm-rg.location
enable_accelerated_networking = false
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.vm-pip.id
}
}
# Managed Disk Def
resource "azurerm_managed_disk" "vm-data-disk" {
name = "vm-DataDisk"
resource_group_name = azurerm_resource_group.sqlvm-rg.name
location = azurerm_resource_group.sqlvm-rg.location
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = "128"
}
# Attach Disk
resource "azurerm_virtual_machine_data_disk_attachment" "attach-data-disk" {
virtual_machine_id = azurerm_windows_virtual_machine.vm-vm.id
managed_disk_id = azurerm_managed_disk.vm-data-disk.id
lun = 0
caching = var.sqlDataDisk-caching
}
# Def VM -
resource "azurerm_windows_virtual_machine" "vm-vm" {
name = "vm-vm-${var.name-sequence}"
resource_group_name = azurerm_resource_group.sqlvm-rg.name
location = azurerm_resource_group.sqlvm-rg.location
size = var.size
admin_username = var.admin-username
admin_password = var.admin-password
network_interface_ids = [
azurerm_network_interface.vm-nic.id
]
os_disk {
caching = "ReadOnly"
storage_account_type = "StandardSSD_LRS"
}
source_image_reference {
publisher = "MicrosoftSQLServer"
offer = "sql2017-ws2019",
sku = "sqldev"
version = "latest"
}
}
# Read Template
data "template_file" "sqlvm" {
template = file("${path.module}/sql-windows-azure.json ")
}
# Deploy Template
resource "azurerm_template_deployment" "sqlvm" {
name = "vmdeploy-${formatdate("DD-MM-YYYY-hh-mm", timestamp())}"
resource_group_name = azurerm_resource_group.sqlvm-rg.name
deployment_mode = "Incremental"
template_body = data.template_file.sqlvm.rendered
depends_on = [azurerm_windows_virtual_machine.vm-vm]
parameters = {
"sqlVirtualMachineLocation" = azurerm_resource_group.sqlvm-rg.location
"sqlVirtualMachineName" = "vm-vm-${var.name-sequence}"
"sqlConnectivityType" = "Private"
"sqlPortNumber" = 1433
"sqlStorageWorkloadType" = "General"
"sqlStorageDisksConfigurationType" = "NEW"
"sqlAutopatchingDayOfWeek" = "Sunday"
"sqlAutopatchingStartHour" = "2"
"sqlAutopatchingWindowDuration" = "60"
"dataPath" = "F:\\data"
"logPath" = "F:\\log"
"tempDbPath" = "D:\\tempDb"
"rServicesEnabled" = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment