This Terraform code will create VM for windows or for linux's with its necessary resource
- Virtual Network
- Public IP
- Network Interface - NIC
- Subnet - Inside Virtual Network
- Provider version is 2.16
- This terraform code is capable for azurerm provider version 2.0.0+
provider "azurerm" {
version = "=2.16"
features {}
}
Set
version
as per requirement.
Windows Virtual Machine (VM) creation is very simple and easy
- required to set one variable
os_type
interraform.tfvars
os_type = "Windows"
This will ensure it will create only Windows Virtual Machine
Linux's Virtual Machine (VM) creation is very simple and easy
- required to set one variable
os_type
in terraform.tfvars
os_type = "Linux"
This will ensure it will create only Linux's Virtual Machine
List of image obtain by running azure-cli command given in following link click here for more information
Example: To obtain list of Windows image
available in West Europe
az vm image list -p MicrosoftWindowsServer -l 'West Europe' --all
In Terraform code main.tf
provide input values for following block of code to the respective variables
For Windows Image:
only when os_type = "Windows"
#Windows image
dynamic "storage_image_reference" {
for_each = local.windows
content {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
For Linux's Image:
only when os_type = "Linux"
#linux image
dynamic "storage_image_reference" {
for_each = local.linux
content {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
}
Initialize Terraform
terraform init
Review Terraform execution plan
terraform plan
Execute previous generated execution plan
terraform apply
Destroy resources created by Terraform
terraform destroy