如何使用 ARM 模板结合 Terraform 在现有 Azure 逻辑应用程序(标准)中创建 1 个或多个工作流?

问题描述 投票:0回答:1

当我尝试在现有的逻辑应用程序(标准)中创建工作流时。我使用 1 个文件 (main.tf) 进行部署,我知道这不是最佳实践,但它用于测试目的。

这是我的main.tf

resource "azurerm_resource_group" "tftest" {
name     = "Terraform-resourcegroup-klant1"
location = "West Europe"
}

data "azurerm_resource_group" "serviceBusRG" {
name = "Service_bus_integration"
}

data "azurerm_servicebus_namespace" "customerServiceBus" {
name = "CustomerIntegrationASB"
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
}

data "azurerm_managed_api" "apitf" {
name     = "servicebus"
location = azurerm_resource_group.tftest.location
}

resource "azurerm_api_connection" "apiConncection" {
name                = "Servicebus-connection"
resource_group_name = azurerm_resource_group.tftest.name
managed_api_id      = data.azurerm_managed_api.apitf.id
display_name        = "API connection 1 test"

parameter_values = {
connectionString = data.azurerm_servicebus_namespace.customerServiceBus.default_primary_connection_string
}
}

resource "azurerm_servicebus_topic" "tenableTopic" {
name = "TenableTerraform"
namespace_id = data.azurerm_servicebus_namespace.customerServiceBus.id
max_size_in_megabytes = 1024
}

resource "azurerm_servicebus_subscription" "tenableSub" {
name = "Klant1"
topic_id = azurerm_servicebus_topic.tenableTopic.id
max_delivery_count = 1
}

resource "azurerm_servicebus_subscription_rule" "tenableFilter" {
name = "Klant1filter"
subscription_id = azurerm_servicebus_subscription.tenableSub.id
filter_type = "CorrelationFilter"

    correlation_filter {
    label = "Klant 1-naam"

}
}

resource "azurerm_storage_account" "storageAccountKlant1" {
name = "klant1storageaccount"
resource_group_name = azurerm_resource_group.tftest.name
location = azurerm_resource_group.tftest.location
account_tier = "Standard"
account_replication_type = "LRS"

}

resource "azurerm_service_plan" "appServicePlanKlant1" {
name = "AppServicePlanKlant1"
location = azurerm_resource_group.tftest.location
resource_group_name = azurerm_resource_group.tftest.name
os_type = "Windows"
sku_name = "WS1"
}

resource "azurerm_logic_app_standard" "logicAppKlant1" {
name = "LogicAppKlant1"
location = azurerm_resource_group.tftest.location
resource_group_name = azurerm_resource_group.tftest.name
app_service_plan_id = azurerm_service_plan.appServicePlanKlant1.id
storage_account_name = azurerm_storage_account.storageAccountKlant1.name
storage_account_access_key = azurerm_storage_account.storageAccountKlant1.primary_access_key

    app_settings = {
      "FUNCTIONS_WORKER_RUNTIME_VERSION" = "~4"
    }

}

resource "azurerm_resource_group_template_deployment" "workflowtje" {
name = "TemplateWorkflowKlant1"
resource_group_name = azurerm_resource_group.tftest.name
deployment_mode = "Complete"

template_content = file("/home/customer/vscode/terraform/logicappcomplete/jsons/readvulns.json")

parameters_content = jsonencode({
"logicAppName" = {
value = azurerm_logic_app_standard.logicAppKlant1.name
}
})
}

此 .tf 文件包含我的逻辑应用程序所需的必要资源,例如其资源组、现有服务总线上的唯一主题 + 订阅以及唯一的存储帐户。

如您所见,底层资源的类型为“azurerm_resource_group_template_deployment”。此资源获取 readvulns.json 文件,其中包含用于创建工作流的模板。这是该 readvulns.json 文件的内容:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "logicAppName": {
            "type": "string"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Web/sites/workflows",
            "apiVersion": "2022-03-01",
            "name": "[concat(parameters('logicAppName'), '/testworkflow1')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "definition": {
                    "$schema": "https://schema.management.azure.com/schemas/2016-06-01/workflowdefinition.json#",
                    "actions": {
                        "Response": {
                            "inputs": {
                                "statusCode": 200,
                                "body": {
                                    "message": "Hello from Logic App!"
                                }
                            },
                            "runAfter": {},
                            "type": "Response"
                        }
                    },
                    "triggers": {
                        "manualTrigger": {
                            "inputs": {},
                            "kind": "Http",
                            "type": "Request"
                        }
                    },
                    "parameters": {},
                    "outputs": {}
                }
            }
        }
    ]
}

确保 main.tf 文件找到 readvulns.json 文件后,我运行以下命令进行计划 + 应用:

**terraform plan -out "completetest"**
**terraform apply "completetest"**
  • 这是
    terraform plan -out "completetest"
  • 的输出
data.azurerm_resource_group.serviceBusRG: Reading...
azurerm_resource_group.tftest: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1\]
data.azurerm_resource_group.serviceBusRG: Read complete after 0s \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Service_bus_integration\]
azurerm_service_plan.appServicePlanKlant1: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Web/serverFarms/AppServicePlanKlant1\]
data.azurerm_managed_api.apitf: Reading...
data.azurerm_servicebus_namespace.customerServiceBus: Reading...
azurerm_storage_account.storageAccountKlant1: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Storage/storageAccounts/klant1storageaccount\]
data.azurerm_managed_api.apitf: Read complete after 0s \[id=/subscriptions/1111111111-1111111111111111111/providers/Microsoft.Web/locations/westeurope/managedApis/servicebus\]
data.azurerm_servicebus_namespace.customerServiceBus: Read complete after 1s \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Service_bus_integration/providers/Microsoft.ServiceBus/namespaces/CustomerIntegrationASB\]
azurerm_api_connection.apiConncection: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Web/connections/Servicebus-connection\]
azurerm_servicebus_topic.tenableTopic: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Service_bus_integration/providers/Microsoft.ServiceBus/namespaces/CustomerIntegrationASB/topics/TenableTerraform\]
azurerm_servicebus_subscription.tenableSub: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Service_bus_integration/providers/Microsoft.ServiceBus/namespaces/CustomerIntegrationASB/topics/TenableTerraform/subscriptions/Klant1\]
azurerm_servicebus_subscription_rule.tenableFilter: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Service_bus_integration/providers/Microsoft.ServiceBus/namespaces/CustomerIntegrationASB/topics/TenableTerraform/subscriptions/Klant1/rules/Klant1filter\]
azurerm_logic_app_standard.logicAppKlant1: Refreshing state... \[id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Web/sites/LogicAppKlant1\]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

+ create
  \-/+ destroy and then create replacement

Terraform will perform the following actions:

# azurerm_api_connection.apiConncection must be replaced

\-/+ resource "azurerm_api_connection" "apiConncection" {
\~ id                  = "/subscriptions/f2f014c7-0bad-40c3-ace9-dac60d3479b5/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Web/connections/Servicebus-connection" -\> (known after apply)
name                = "Servicebus-connection"
\~ parameter_values    = { # forces replacement
\+ "connectionString" = (sensitive value)
}
tags                = {
"Hello" = "World"
}
\# (3 unchanged attributes hidden)
}

# azurerm_resource_group_template_deployment.workflowtje will be created

+ resource "azurerm_resource_group_template_deployment" "workflowtje" {
  + deployment_mode     = "Complete"
  + id                  = (known after apply)
  + name                = "TemplateWorkflowKlant1"
  + output_content      = (known after apply)
  + parameters_content  = jsonencode(
    {
    \+ logicAppName = {
    \+ value = "LogicAppKlant1"
    }
    }
    )
  + resource_group_name = "Terraform-resourcegroup-klant1"
  + template_content    = jsonencode(
    {
    \+ "$schema"      = "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#"
    \+ contentVersion = "1.0.0.0"
    \+ parameters     = {
    \+ logicAppName = {
    \+ type = "string"
    }
    }
    \+ resources      = \[
    \+ {
    \+ apiVersion = "2022-03-01"
    \+ location   = "\[resourceGroup().location\]"
    \+ name       = "\[concat(parameters('logicAppName'), '/testworkflow1')\]"
    \+ properties = {
    \+ definition = {
    \+ "$schema"  = "https://schema.management.azure.com/schemas/2016-06-01/workflowdefinition.json#"
    \+ actions    = {
    \+ Response = {
    \+ inputs   = {
    \+ body       = {
    \+ message = "Hello from Logic App!"
    }
    \+ statusCode = 200
    }
    \+ runAfter = {}
    \+ type     = "Response"
    }
    }
    \+ outputs    = {}
    \+ parameters = {}
    \+ triggers   = {
    \+ manualTrigger = {
    \+ inputs = {}
    \+ kind   = "Http"
    \+ type   = "Request"
    }
    }
    }
    }
    \+ type       = "Microsoft.Web/sites/workflows"
    },
    \]
    }
    )
    }

Plan: 2 to add, 0 to change, 1 to destroy.\
  • 这是
    terraform plan "completetest"
    的输出:

azurerm_api_connection.apiConncection: Destroying... [id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Web/connections/Servicebus-connection] azurerm_resource_group_template_deployment.workflowtje: Creating... azurerm_api_connection.apiConncection: Destruction complete after 2s azurerm_api_connection.apiConncection: Creating... azurerm_api_connection.apiConncection: Creation complete after 2s [id=/subscriptions/1111111111-1111111111111111111/resourceGroups/Terraform-resourcegroup-klant1/providers/Microsoft.Web/connections/Servicebus-connection] azurerm_resource_group_template_deployment.workflowtje: Still creating... [10s elapsed] azurerm_resource_group_template_deployment.workflowtje: Still creating... [20s elapsed] azurerm_resource_group_template_deployment.workflowtje: Still creating... [30s elapsed] ╷ │ Error: waiting for creation of Template Deployment "TemplateWorkflowKlant1" (Resource Group "Terraform-resourcegroup-klant1"): Code="DeploymentFailed" Message="At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details." Details=[{"code":"MethodNotAllowed","message":"{\r\n  \"Message\": \"The requested resource does not support http method 'PUT'.\"\r\n}"}] │  │   with azurerm_resource_group_template_deployment.workflowtje, │   on main.tf line 99, in resource "azurerm_resource_group_template_deployment" "workflowtje": │   99: resource "azurerm_resource_group_template_deployment" "workflowtje" {

如您所见,我得到的错误是: 请求的资源不支持http方法“PUT”。除了我现有的逻辑应用程序(标准)中的工作流程之外,一切都已成功创建。我在这里做错了什么吗?

我已经尝试在 readvulns.json 中创建其他类型的资源,但没有成功。

terraform azure-rm-template azure-logic-app-standard
1个回答
0
投票

请求的资源不支持http方法'PUT'

该错误是由于

workflow.json
文件内的模板资源部署块冲突而导致的。经过验证,发现问题出在工作流的输入类型上。应该是
Microsoft.Logic/workflows
而不是
Microsoft.Web/sites/workflows

在对应API版本的Microsoft.Logic/workflows

文件中修改为
workflow.json
,如下所示。

"type": "Microsoft.Logic/workflows"
"apiVersion": "2019-05-01"

以下是修改后的

Workflow.json
文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "logicAppName": {
            "type": "string",
            "defaultValue": "xxxxx"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2019-05-01",
            "name": "[parameters('logicAppName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "definition": {
                    "$schema": "https://schema.management.azure.com/schemas/2016-06-01/workflowdefinition.json#",
                    "actions": {
                        "Response": {
                            "inputs": {
                                "statusCode": 200,
                                "body": {
                                    "message": "Hello from Logic App!"
                                }
                            },
                            "runAfter": {},
                            "type": "Response"
                        }
                    },
                    "triggers": {
                        "manualTrigger": {
                            "inputs": {},
                            "kind": "Http",
                            "type": "Request"
                        }
                    },
                    "parameters": {},
                    "outputs": {}
                }
            }
        }
    ]
}

完整的 Terraform 代码:

provider "azurerm" {
  features {}
  subscription_id = "xxxx"
}

data "azurerm_resource_group" "serviceBusRG" {
name = "postres"
}

data "azurerm_servicebus_namespace" "customerServiceBus" {
name = "newposp"
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
}

data "azurerm_managed_api" "example" {
  name     = "servicebus"
  location = data.azurerm_resource_group.serviceBusRG.location
}

resource "azurerm_api_connection" "apiConncection" {
name                = "Servicebus-connection"
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
managed_api_id      = data.azurerm_managed_api.apitf.id
display_name        = "API connection 1 test"

parameter_values = {
connectionString = data.azurerm_servicebus_namespace.customerServiceBus.default_primary_connection_string
}
}

resource "azurerm_servicebus_topic" "tenableTopic" {
name = "TenableTerraform"
namespace_id = data.azurerm_servicebus_namespace.customerServiceBus.id
max_size_in_megabytes = 1024
}

resource "azurerm_servicebus_subscription" "tenableSub" {
name = "Klant1"
topic_id = azurerm_servicebus_topic.tenableTopic.id
max_delivery_count = 1
}

resource "azurerm_servicebus_subscription_rule" "tenableFilter" {
name = "Klant1filter"
subscription_id = azurerm_servicebus_subscription.tenableSub.id
filter_type = "CorrelationFilter"

    correlation_filter {
    label = "Klant 1-naam"

}
}

resource "azurerm_storage_account" "storageAccountKlant1" {
name = "klant1storageaccount"
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
location = data.azurerm_resource_group.serviceBusRG.location
account_tier = "Standard"
account_replication_type = "LRS"

}

resource "azurerm_service_plan" "appServicePlanKlant1" {
name = "AppServicePlanKlant1"
location = data.azurerm_resource_group.serviceBusRG.location
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
os_type = "Windows"
sku_name = "WS1"
}

resource "azurerm_logic_app_standard" "logicAppKlant1" {
name = "LogicAppKlant1"
location = data.azurerm_resource_group.serviceBusRG.location
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
app_service_plan_id = azurerm_service_plan.appServicePlanKlant1.id
storage_account_name = azurerm_storage_account.storageAccountKlant1.name
storage_account_access_key = azurerm_storage_account.storageAccountKlant1.primary_access_key

    app_settings = {
      "FUNCTIONS_WORKER_RUNTIME_VERSION" = "~4"
    }

}

resource "azurerm_resource_group_template_deployment" "workflowtje" {
name = "TemplateWorkflowKlant1"
resource_group_name = data.azurerm_resource_group.serviceBusRG.name
deployment_mode = "Complete"

template_content = file("/home/madugula/workflow.json")

parameters_content = jsonencode({
"logicAppName" = {
value = azurerm_logic_app_standard.logicAppKlant1.name
}
})
}

输出

enter image description here

部署成功:

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.