0

I have an issue creating a VM on Azure using Terraform.

We have a policy restricting from creating certain vm sizes for our subscription, but we created an exemption for a specific ResourceGroup.

I can create VM with the wanted size using my ServicePrincipal and with the following command:

$ az login --service-principal -u ... -p ... --tenant ...

$ az vm create --resource-group ... --name ... --image ... --admin-username ... --generate-ssh-keys --location ... --size ...

The VM is created successfully with the wanted size.

But, when I try to create the VM using Terraform, with the same VM size, I'm getting the following error:

level=error msg=Error: creating Linux Virtual Machine "..." (Resource Group "..."): compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status= Code="SkuNotAvailable" Message="The requested size for resource '/subscriptions/.../resourceGroups/.../providers/Microsoft.Compute/virtualMachines/...' is currently not available in location '...' zones '...' for subscription '...'. Please try another size or deploy to a different location or zones. See https://aka.ms/azureskunotavailable for details."

After running az vm list-skus --location ... --size ... --all --output table

The output for the wanted size is:

restrictions
---
NotAvailableForSubscription, type: Zone, locations: ..., zones: 1,2,3

It looks like the size is unavailable, but using the CLI or Azure portal, I'm able to create VM with this size.

The terraform is running with the same service principal as the CLI command, in the same subscription, tenant and resource group.

Do you have an idea what can cause this problem creating the VM using terraform?

Thanks

5
  • Which provider version are you using? Commented Nov 15, 2022 at 13:38
  • Could you please mention which "SkuNotAvailable" size and location you are looking for? Commented Nov 15, 2022 at 13:39
  • @SwarnaAnipindi the size is Standard_NC12s_v3 and eastus region Commented Nov 15, 2022 at 13:42
  • As per my understanding, if you are able to create an NCv3 Series VM from the CLI on subscription "xxxxx," the same will work using Terraform as well. On the Terraform side, please check whether you have the right subscription or not. If not, please run this command. az account set --subscription "XXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX- If it does not work, please provide any screenshots; this will help in the replication of the problem. The zone "Standard_NC12s_v3" was unavailable on the east US region. Commented Nov 15, 2022 at 14:42
  • 1
    Replicated the scenario "VM zone with "Standard_NC12s_v3" and east US region" and provided a code base. Thank You. Commented Nov 22, 2022 at 7:42

1 Answer 1

0

Here is the terraform script to create a VM with specified configurations

location = "East US" vm_size = "Standard_NC12s_v3"

Step1: Copy the below code in "main tf" file

provider "azurerm" {
 features {}
 }
variable "prefix" {
  default = "rg_swarna"
}
resource "azurerm_resource_group" "example" {
  name     = "${var.prefix}-resources"
  location = "East US"
}
resource "azurerm_virtual_network" "main" {
  name                = "${var.prefix}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = "rg_swarna-resources"//azurerm_resource_group.example.name
  virtual_network_name = "rg_swarna-network"//azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "main" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
  }
}
resource "azurerm_virtual_machine" "main" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.main.id]
  //vm_size               = "Standard_DS1_v2"
  vm_size               = "Standard_NC12s_v3"
  # Uncomment this line to delete the OS disk automatically when deleting the VM
  # delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = {
    environment = "staging"
  }
}

Step2: run below commands

terraform plan 
terraform apply -auto-approve

Step3: Verify the results in Azure Portal enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.