Search code examples
azureterraform

Error creating resource "Azure Bot" via Terraform


I tried to create an "Azure Bot" via Terraform using these instructions: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/bot_service_azure_bot

and recieved this error message: enter image description here

To avoid any misunderstandings, this is the resource I would like to create via Terraform: enter image description here

When I create the resource "Azure Bot" via portal, the microsoft_app_id is created automatically. Why does via Terraform the microsoft_app_id have to be assigned the client_id?

How can i solve the error "The Microsoft App ID is already registered to another bot application"?


Solution

  • I also received the same error when trying to create a web bot servise with terraform.

    Two ways to resolve this conflict:

    Approach-1:

    Firstly, you can use random uuid resource as given below to generate the unique app Id while creating the bot resource.

    resource "random_uuid" "appid" {}
    

    But the above one might also fail in some environments.

    Approach-2:

    To make it work effectively, I have registered a new app under App registrations as shown below and allocated it to my Azure web bot service instead of creating it automatically with client_configblock.

    enter image description here

    Once it is done, I used the same one in my terraform code with the help of variable block as detailed below.

    main.tf:

    variable "microsoftappid" {
      type    = string
      default = "3072xxxxx8579"
    }
    
    data "azurerm_resource_group" "example" {
      name     = "Jahnavi"
    }
    
    resource "azurerm_application_insights" "example" {
      name                = "jahsights"
      location            = data.azurerm_resource_group.example.location
      resource_group_name = data.azurerm_resource_group.example.name
      application_type    = "web"
    }
    
    resource "azurerm_application_insights_api_key" "example" {
      name                    = "newapikey"
      application_insights_id = azurerm_application_insights.example.id
      read_permissions        = ["aggregate", "api", "draft", "extendqueries", "search"]
    }
    
    resource "azurerm_bot_service_azure_bot" "example" {
      name                = "jahazurebot"
      resource_group_name = data.azurerm_resource_group.example.name
      location            = "global"
      microsoft_app_id    = var.microsoftappid
      sku                 = "F0"
    
      endpoint                              = "https://example.com"
      developer_app_insights_api_key        = azurerm_application_insights_api_key.example.api_key
      developer_app_insights_application_id = azurerm_application_insights.example.app_id
    }
    

    Deployment succeeded:

    enter image description here

    enter image description here

    enter image description here

    Reference: Terraform registry template