The example above uses the community Azure/compute/azurerm and Azure/network/azurerm Terraform modules to build one Linux VM and one Windows VM in Azure. Both share a virtual network and each gets a public DNS name. This page explains what each block does, gives a corrected and version-pinned main.tf that works with current Azure rules, and lists the errors you are most likely to hit.
What the example builds
The configuration uses public modules from the Terraform Registry, so you don’t have to write every azurerm_* resource yourself. Each module call creates a group of related resources from a few inputs.
| Block | What it creates | Key inputs |
|---|---|---|
azurerm_resource_group.example |
A resource group in West Europe | name, location |
module "network" |
A virtual network (10.0.0.0/16 by default) with one subnet, subnet1 (10.0.1.0/24) |
subnet_prefixes, subnet_names |
module "linuxservers" |
A Linux VM with its NIC, public IP, network security group (NSG) and OS disk | vm_os_simple, public_ip_dns, vnet_subnet_id |
module "windowsservers" |
A Windows VM with the same set of supporting resources | is_windows_image, admin_password, vm_hostname |
output blocks |
The fully qualified DNS name of each public IP | public_ip_dns_name |
vnet_subnet_id = module.network.vnet_subnets[0] connects the modules: both VMs get a NIC in the first subnet the network module creates. The depends_on lines matter. The modules read the resource group through a data source, and without an explicit dependency that read can run before the group exists.
Note: Microsoft no longer recommends the Azure/terraform-azurerm-compute and Azure/terraform-azurerm-network modules for new projects. It now points to Azure Verified Modules (AVM), for example Azure/avm-res-compute-virtualmachine/azurerm. The example still teaches module composition well, but for new production work, read the module repository README first and consider AVM.
Prerequisites
- Terraform 1.3 or later, and the Azure CLI (
az). - An Azure subscription where you can create resource groups, networks and VMs.
- An SSH key pair for the Linux VM. By default the module uses the admin user
azureuserand key-based login. - DNS labels that nobody else uses in the region.
linsimplevmipsandwinsimplevmipsbecome<label>.westeurope.cloudapp.azure.com, and someone has probably already taken them.
az login
az account set --subscription "<subscription-id-or-name>"
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa Code language: PHP (php)
Note: Azure Linux VMs accept RSA keys. Recent Azure versions also accept Ed25519 keys, but older module releases read ~/.ssh/id_rsa.pub by default. Check the ssh_key input of the module version you pin.
Corrected and hardened main.tf
The version below keeps the same design but fixes the problems in the example above. It pins module and provider versions, takes the Windows password out of the code, adds the network module’s required input, and uses a Standard public IP instead of a Basic one. It also chooses supported OS images explicitly instead of relying on the old vm_os_simple aliases.
terraform {
required_version = ">= 1.3"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
variable "windows_admin_password" {
type = string
sensitive = true
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
}
module "network" {
source = "Azure/network/azurerm"
version = "~> 5.0"
resource_group_name = azurerm_resource_group.example.name
use_for_each = true
address_space = ["10.0.0.0/16"]
subnet_prefixes = ["10.0.1.0/24"]
subnet_names = ["subnet1"]
depends_on = [azurerm_resource_group.example]
}
module "linuxservers" {
source = "Azure/compute/azurerm"
version = "~> 5.0"
resource_group_name = azurerm_resource_group.example.name
vm_hostname = "mylinuxvm"
vm_size = "Standard_B2s"
vm_os_publisher = "Canonical"
vm_os_offer = "0001-com-ubuntu-server-jammy"
vm_os_sku = "22_04-lts-gen2"
public_ip_dns = ["lin-demo-<unique-suffix>"]
public_ip_sku = "Standard"
allocation_method = "Static"
vnet_subnet_id = module.network.vnet_subnets[0]
depends_on = [azurerm_resource_group.example]
}
module "windowsservers" {
source = "Azure/compute/azurerm"
version = "~> 5.0"
resource_group_name = azurerm_resource_group.example.name
is_windows_image = true
vm_hostname = "mywinvm"
vm_size = "Standard_B2s"
admin_password = var.windows_admin_password
vm_os_publisher = "MicrosoftWindowsServer"
vm_os_offer = "WindowsServer"
vm_os_sku = "2022-datacenter-g2"
public_ip_dns = ["win-demo-<unique-suffix>"]
public_ip_sku = "Standard"
allocation_method = "Static"
vnet_subnet_id = module.network.vnet_subnets[0]
depends_on = [azurerm_resource_group.example]
}
output "linux_vm_public_name" {
value = module.linuxservers.public_ip_dns_name
}
output "windows_vm_public_name" {
value = module.windowsservers.public_ip_dns_name
}Code language: JavaScript (javascript)
versionon every module stops a new major release from changing your infrastructure without warning. Major versions of these modules have renamed or added required inputs.use_for_each = trueis a required input in version 5 of the network module. Without it,terraform planfails.public_ip_sku = "Standard"withallocation_method = "Static": Microsoft has retired Basic SKU public IPs, and a Standard IP must be static. Static allocation also means the IP and DNS name exist as soon asapplyfinishes.vm_os_publisher/vm_os_offer/vm_os_skureplacevm_os_simple = "UbuntuServer", which points to an old Ubuntu 18.04 LTS image. List current images withaz vm image list --publisher Canonical --all --output table.var.windows_admin_passwordkeeps the secret out of version control. Because it is markedsensitive, Terraform hides it in plan output.
Note: Input names can differ between major versions of a module. If terraform plan reports Unsupported argument, compare your code with the inputs listed for that exact version on the module’s Registry page. Run terraform init and the module’s required_providers constraint will also show you which azurerm versions it supports.
Deploy, connect and clean up
- Save the corrected file as
main.tfin an empty directory, and replace both<unique-suffix>placeholders with your own lowercase value. - Set the Windows password as an environment variable so it never goes on disk in your code:
export TF_VAR_windows_admin_password='<12+ chars, mixed case, digit, symbol>'. - Run
terraform initto download the provider and both modules. - Run
terraform plan -out tfplanand review it. Expect around 15 to 20 resources: the resource group, VNet, subnet, and for each VM a public IP, NSG, NIC, VM and related resources. - Run
terraform apply tfplan. Creating the VMs usually takes a few minutes. - Run
terraform outputto read the DNS names. - Connect to Linux with
ssh azureuser@<linux-fqdn>, and to Windows with an RDP client to<windows-fqdn>asazureuserwith the password you set. - When you are finished, run
terraform destroyso you stop paying for compute and public IPs.
$ terraform output
linux_vm_public_name = [
"lin-demo-abc123.westeurope.cloudapp.azure.com",
]
windows_vm_public_name = [
"win-demo-abc123.westeurope.cloudapp.azure.com",
]Code language: JavaScript (javascript)
Each output is a list because public_ip_dns takes one label per VM. To create several identical VMs, set nb_instances and give one DNS label per instance.
Common errors and fixes
| Error or symptom | Cause | Fix |
|---|---|---|
The argument "use_for_each" is required |
Version 5 or later of the network module is being used without the new required input | Add use_for_each = true, or pin the older major version you tested with |
DnsRecordInUse or a DNS label already in use |
Someone else already uses the label in that region | Change public_ip_dns to a unique value |
| A public IP error that mentions the Basic SKU | The module’s default public IP SKU is Basic, which Azure has retired | Set public_ip_sku = "Standard" and allocation_method = "Static" |
no file exists at ~/.ssh/id_rsa.pub |
The Linux module expects an SSH public key and can’t find one | Run ssh-keygen, or point the module’s SSH key input at your key |
InvalidParameter for adminPassword |
The Windows password doesn’t meet Azure’s length or complexity rules | Use at least 12 characters with upper case, lower case, a digit and a symbol |
SkuNotAvailable |
The VM size isn’t offered in that region for your subscription | Run az vm list-skus --location westeurope --size Standard_B --output table and choose another size or region |
Resource group not found during plan |
A data source inside the module read the group before it was created | Keep depends_on = [azurerm_resource_group.example] on each module |
Security and production practices
- Restrict inbound access. The module’s NSG opens SSH (22) or RDP (3389). By default it allows any source address. Limit it to your own IP range with the module’s source-prefix input, or remove public IPs and connect through Azure Bastion or a VPN.
- Protect state. Terraform state stores the admin password and other attributes in plain text. Use a remote backend such as an Azure Storage account with access controls and encryption, and never commit
terraform.tfstate. - Keep secrets out of code. Use
TF_VAR_environment variables, a CI secret store or Azure Key Vault. Never put credentials in.tffiles. - Commit
.terraform.lock.hclso every run uses the same provider builds. - Automate checks. Formatting, validation and security scanning catch problems before
apply. See pre-commit for Terraform. - Scale with loops, not copies. If you need many module calls, use
for_eachon the module block instead of repeating it. See Terraform loops and iteration.
Frequently asked questions
What is the terraform-azurerm-compute module?
It is a Terraform module published by the Azure GitHub organisation as Azure/compute/azurerm. It creates Azure Linux or Windows VMs together with their NICs, public IPs, NSGs and disks. Microsoft now directs new projects to Azure Verified Modules instead.
Why does my Azure VM have no public IP address after terraform apply?
With Dynamic allocation, Azure only assigns the address once the IP is attached to a running VM, so the output can be empty at first. Use a Standard SKU public IP with Static allocation and the address exists as soon as apply completes.
How do I pass the Windows admin password to Terraform securely?
Declare a variable marked sensitive = true and set it with the TF_VAR_<name> environment variable or a CI secret. Remember that it is still saved in state, so store state in a protected remote backend.
Can I create multiple VMs with one module call?
Yes. Set nb_instances to the number of VMs you want and supply one DNS label per instance in public_ip_dns. For VMs that need different settings, use for_each on the module block.
Do I need depends_on when using these Azure modules?
For these modules, yes. They read the resource group through a data source, and without an explicit dependency Terraform may try to read the group before it exists.
Related tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals
PakarPBN
A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.
In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.
The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.
Streaming Film
Nonton Film gratis
Jasa Backlink
Download Anime Batch