hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.61k stars 4.65k forks source link

Recreating API connection makes logic app stop working #17359

Open dmlundeby opened 2 years ago

dmlundeby commented 2 years ago

Is there an existing issue for this?

Community Note

Terraform Version

1.2.3

AzureRM Provider Version

3.10.0

Affected Resource(s)/Data Source(s)

azurerm_api_connection, azurerm_logic_app_workflow

Terraform Configuration Files

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.10.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "neptune-rg"
  location = "westeurope"
}

data "azurerm_client_config" "current" {}

resource "azurerm_container_group" "neptune" {
  name                = "neptune-container-group"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  os_type             = "Linux"

  container {
    name   = "neptune-container"
    image  = "alpine"
    cpu    = "1.0"
    memory = "1.0"
    ports {
      port     = 80
      protocol = "TCP"
    }
    commands = ["echo", "\"Hello world\""]
  }
}

data "azurerm_managed_api" "aci-managed-api" {
  location = azurerm_resource_group.rg.location
  name     = "aci"
}

resource "azurerm_api_connection" "aci-conn" {
  name                = "aci-connection"
  resource_group_name = azurerm_resource_group.rg.name
  managed_api_id      = data.azurerm_managed_api.aci-managed-api.id
  display_name        = "ACI connection"

  parameter_values = {
    "token:TenantId"  = data.azurerm_client_config.current.tenant_id,
    "token:grantType" = "code"
  }

  lifecycle {
    ignore_changes = [
      parameter_values
    ]
  }
}

resource "azurerm_logic_app_workflow" "run-neptune" {
  name                = "run-neptune"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  workflow_parameters = {
    "$connections" = jsonencode({
      "defaultValue" : {},
      "type" : "Object"
    })
  }

  parameters = {
    "$connections" = jsonencode({
      "aci" : {
        "connectionId" : "${azurerm_api_connection.aci-conn.id}",
        "connectionName" : "${azurerm_api_connection.aci-conn.name}",
        "id" : "${azurerm_api_connection.aci-conn.managed_api_id}"
      }
    })
  }
}

resource "azurerm_logic_app_trigger_recurrence" "run-neptune-trigger-recurrence" {
  name         = "Recurrence"
  logic_app_id = azurerm_logic_app_workflow.run-neptune.id
  frequency    = "Day"
  interval     = 1
}

resource "azurerm_logic_app_action_custom" "la-load-test-recurrence-start-container" {
  name         = "Start container"
  logic_app_id = azurerm_logic_app_workflow.run-neptune.id
  body         = <<BODY
{
  "inputs": {
    "host": {
      "connection": {
        "name": "@parameters('$connections')['aci']['connectionId']"
      }
    },
    "method": "post",
    "path": "/subscriptions/@{encodeURIComponent('${data.azurerm_client_config.current.subscription_id}')}/resourceGroups/@{encodeURIComponent('${azurerm_resource_group.rg.name}')}/providers/Microsoft.ContainerInstance/containerGroups/@{encodeURIComponent('${azurerm_container_group.neptune.name}')}/start",
    "queries": {
      "x-ms-api-version": "2019-12-01"
    }
  },
  "runAfter": {},
  "type": "ApiConnection"
}
BODY
}

Debug Output/Panic Output

{
  "status": 404,
  "source": "https://logic-apis-westeurope.token.azure-apim.net:443/tokens/logic-apis-westeurope/ACI/62fffe4f22614cd196e0b54340f6adb0/exchange",
  "message": "Error from token exchange: The connection (logic-apis-westeurope/aci/62fffe4f22614cd196e0b54340f6adb0) is not found. Please create new connection and change your application to use the new connection."
}

Expected Behaviour

The logic app is expected to run successfully.

Actual Behaviour

The logic app fails in the action "Start container", with error message mentioned above.

We can make the logic app work again if we enter designer manually, do any change and click "Save", then undo the change and click "Save" once again.

Steps to Reproduce

  1. terraform apply. The logic app now works as expected
  2. Delete the API connection (called aci-connection) manually in Azure Portal
  3. terraform apply
  4. Go to the recreated API connection in Azure Portal, then "Edit API connection" --> Authorize --> Save
  5. Start the logic app manually, by clicking "Run Trigger"

Important Factoids

No response

References

No response

chester-personaify commented 3 months ago

any update on this issue or some automated workaround?