I am trying to create a Virtual Machine Image using the Terraform azapi provider. Within the same configuration I'm also using Azurerm, which works fine.
This is how I set up my providers:
terraform {
required_version = "~>1.4.0"
backend "azurerm" {}
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.64.0"
}
# Additional providers must be added below
azapi = {
source = "Azure/azapi"
version = "=1.10.0"
}
}
}
provider "azurerm" {
subscription_id = <Subscription ID>
features {}
skip_provider_registration = "true"
}
provider "azapi" {
subscription_id = <Subscription ID>
use_cli = true
}
This is the resource that I am trying to deploy:
resource "azapi_resource" "<Reference-Name>" {
type = "Microsoft.VirtualMachineImages/imageTemplates@2022-02-14"
name = "<Resource-Name>"
location = "westeurope"
parent_id = <Resource Group id>
tags = var.tags
identity {
type = "UserAssigned"
identity_ids = [<Managed identity id>]
}
body = jsonencode({
properties = {
buildTimeoutInMinutes = 0
customize = [
{
name = <script-name>
type = "PowerShell"
runAsSystem = true
runElevated = true
scriptUri = <script URL>
}
]
distribute = [
{
artifactTags = {}
runOutputName = <output-name>
type = "SharedImage"
excludeFromLatest = true
galleryImageId = <gallery-id>
replicationRegions = [
"westeurope"
]
storageAccountType = "Blob"
}
]
validate = {
continueDistributeOnFailure = true
}
source = {
type = "PlatformImage"
publisher = "MicrosoftWindowsDesktop"
offer = "Windows-10"
sku = "22h2-avd"
version = "latest"
}
stagingResourceGroup = <staging RG name>
vmProfile = {
osDiskSizeGB = 64
vmSize = "Standard_B2ms"
vnetConfig = {
subnetId = <subnet id>
}
}
}
})
}
I am using the Azure CLI for authentication. But when I try to apply this I get the following error:
│ Error: checking for presence of existing Resource: (ResourceId "/subscriptions/<Subscription ID>/resourceGroups/<RG-name> /providers/Microsoft.VirtualMachineImages/imageTemplates/<Resource-name>" / Api Version "2022-02-14"): ChainedTokenCredential authentication failed
│ GET http://169.254.169.254/metadata/identity/oauth2/token
│ --------------------------------------------------------------------------------
│ RESPONSE 403 connecting to 169.254.169.254:80: connecting to 169.254.169.254:80: dial tcp 169.254.169.254:80: connectex: A socket operation was attempted to an unreachable network.
│ ---------------------------
│ connecting to 169.254.169.254:80: connecting to 169.254.169.254:80: dial tcp 169.254.169.254:80: connectex: A socket operation was attempted to an unreachable network.
│ --------------------------------------------------------------------------------
│
│
│ with azapi_resource.avd_image,
│ on main.tf line 80, in resource "azapi_resource" "<Resource-name>":
│ 80: resource "azapi_resource" "<Resource-name>" {
I couldn't find anything online that would explain why only azapi would fail but azurerm would not. I have also tried it for different resources with azapi, and that results in the same error. I have also tried connecting to different networks. What could be my mistake here?
The error you are encountering, "ChainedTokenCredential authentication failed", typically indicates an issue with Azure CLI authentication or Managed Identity configuration.
Since you're using Azure CLI for authentication (
use_cli = truein theazapiprovider configuration), ensure that you are logged in with the Azure CLI and have the correct permissionsRun
az loginto authenticate your Azure CLI session, and verify withaz account showthat you are logged in to the correct account with necessary permissions.For reference you can use this path to re-install the Azure CLI if youre using in local.
My demo terraform configuration:
Output: