-1

I am trying to create Application ID URI of an application registration that is already deployed to Azure and using azapi for that as Application ID URI is going to be the app's own application_id (i.e. api://[app_id]). So I am trying to accomplish this via azapi.

However I get following error when I run terraform init -upgrade:

│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/azapi: provider registry registry.terraform.io does not have a provider  
│ named registry.terraform.io/hashicorp/azapi
│
│ Did you intend to use azure/azapi? If so, you must specify that source address in each module which requires that provider. To see which modules  
│ are currently depending on hashicorp/azapi, run the following command:
│     terraform providers

I have my provider.tf file as follows:

terraform {
  required_providers {
  azapi = {
    source = "Azure/azapi"
  }
  azuread = {
    source = "hashicorp/azuread"
  }
  azurerm = {
    source = "hashicorp/azurerm"
  }
}
backend "azurerm" {
  #..... #necessary info
  }
}

provider "azuread" {
  tenant_id       = "tenant_id"
}

provider "azapi" {
  subscription_id = "subscription_id"
  tenant_id       = "tenant_id"
}

and main.tf like so (where app_reg is already defined):

resource "azapi_update_resource" "update_app_reg" {
type = "Microsoft.Web/sites@2022-03-01"
name = "app-reg"
parent_id = azuread_service_principal.app_reg_service_principal.object_id

# Update Application ID URI
body = jsonencode({
"properties" = {
  "identifierUris" = [
    "api://${azuread_application.app_reg.application_id}"
    ]
  }
})

depends_on = [azuread_application.app_reg]
}

when I remove resource "azapi_update_resource" ......... , terraform init -upgrade runs successfully. However when I leave it defined as it is, I get the error above.

I have already defined the provider and I am not sure what I am doing wrong. Any help would be appreciated.

PS: I have checked this post, microsoft documentation about this and hashicorp documentation about this, but nothing helpful...

Serhat
  • 216
  • 2
  • 17

1 Answers1

0

The problem occurred because I didn't specify the provider in my module.

So here was my structure:

.
├── providers.tf
├── main.tf
├── modules
    ├── main.tf

and resources "azapi_update_resource" ... was in modules/main.tf and provider "azapi"... was in ./providers.tf. After following steps in this answer, I have created a new providers.tf inside the modules directory and added:

terraform {
 required_providers {
  azapi = {
   source = "Azure/azapi"
   }
  }
}

And this have resolved the issue.

After I ran the terraform providers command, I got this:

.
├── provider[registry.terraform.io/azure/azapi]
├── provider[registry.terraform.io/hashicorp/azuread]
├── provider[registry.terraform.io/hashicorp/azurerm]
├── module.application_registrations
    ├── provider[registry.terraform.io/hashicorp/azuread]
    ├── provider[registry.terraform.io/hashicorp/azurerm]
    ├── provider[registry.terraform.io/hashicorp/azapi]

So, there you can see that module.application_registrations is referring to hashicorp/azapi which is non-existent.

Long story short; adding providers.tf into module directory and adding azapi into it has resolved the issue!

Serhat
  • 216
  • 2
  • 17