概觀:使用 Azure Resource Manager 範本自動部署 Azure Logic Apps

適用於:Azure Logic Apps (使用量)

當您準備好將建立和部署邏輯應用程式自動化時,您可將邏輯應用程式的基礎工作流程定義擴充至 Azure Resource Manager 範本。 此範本會定義用於佈建和部署邏輯應用程式的基礎結構、資源、參數和其他資訊。 針對在部署時有所不同的值定義參數 (也稱為「參數化」),您可以根據不同的部署需求重複且一致地部署邏輯應用程式。

例如,如果您部署至開發、測試和生產環境,您可能針對每個環境使用不同的連接字串。 您可以宣告可接受不同連接字串的範本參數,然後將這些字串儲存在不同的 parameters 檔案中。 如此一來,您可以變更這些值,而不需更新和重新部署樣本。 在您的參數值具敏感性或必須安全 (例如密碼和秘密) 的案例中,您可以將這些值儲存在 Azure Key Vault 中,並且讓 parameters 檔案擷取這些值。 不過,在這些案例中,您會重新部署以擷取目前的值。

本概觀描述包含邏輯應用程式工作流程定義的 Resource Manager 範本中的屬性。 範本和工作流程定義均使用 JSON 語法,但有一些差異,因為工作流程定義也會遵循工作流程定義語言結構描述。 例如,範本運算式與工作流程定義運算式在其參考參數的方式和可接受的值有所不同。

秘訣

若要以最輕鬆的方式取得最方便部署的有效參數化邏輯應用程式範本,請使用 Visual Studio (免費的 Community 版本或更新版本),以及適用於 Visual Studio 的 Azure Logic Apps Tools。 然後,您可以在 Visual Studio 中建立邏輯應用程式,或從 Azure 尋找現有的邏輯應用程式並將其並下載到 Visual Studio

或者,可以使用 Azure PowerShell 搭配 LogicAppTemplate 模組來建立邏輯應用程式範本。

本主題中的範例邏輯應用程式使用會在新電子郵件送達時引發的 Office 365 Outlook 觸發程序,以及使用 Azure Blob 儲存體動作來建立電子郵件內文 Blob 並將該 Blob 上傳至 Azure 儲存體容器。 這些範例也會示範如何參數化在部署時有所不同的值。

如需 Resource Manager 範本的詳細資訊,請參閱下列主題:

如需邏輯應用程式、整合帳戶、整合帳戶成品和整合服務環境特有的範本資源資訊,請參閱 Microsoft.Logic 資源類型

如需範例邏輯應用程式範本,請參閱下列範例:

對於 Logic Apps REST API,請從 Azure Logic Apps REST API 概觀開始。

範本結構

在最上層,Resource Manager範本會遵循此結構,完全如 Azure Resource Manager範本結構和語法主題中所述:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {},
   "variables": {},
   "functions": [],
   "resources": [],
   "outputs": {}
}

對於邏輯應用程式範本,您主要使用這些範本物件:

屬性 描述
parameters 宣告範本參數,以接受在 Azure 中建立和自訂可供部署的資源時所要使用的值。 例如,這些參數接受邏輯應用程式的名稱和位置、連線,以及部署所需的其他資源值。 您可以將這些參數值儲存在 parameters 檔案中,本主題稍後會加以說明。 如需一般詳細資料,請參閱參數 - Resource Manager範本結構和語法
resources 定義資源以建立或更新並部署到 Azure 資源群組,例如邏輯應用程式、連線、Azure 儲存體帳戶等等。 如需一般詳細資料,請參閱資源 - Resource Manager 範本結構和語法

邏輯應用程式範本會使用此檔案名稱格式:

<logic-app-name>.json

重要事項

範本語法會區分大小寫,因此確定您使用一致的大小寫。

範本參數

邏輯應用程式範本有多個 parameters 存在於不同層級並執行不同函式的物件。 例如,在最上層,您可以在 Azure 中建立和部署資源時,針對要在部署時接受和使用的值宣告範本參數,例如:

  • 您的邏輯應用程式

  • 邏輯應用程式用於透過受控連接器存取其他服務和系統的連線

  • 邏輯應用程式需要用於部署的其他資源

    例如,如果邏輯應用程式在企業對企業 (B2B) 案例中使用整合帳戶 ,則範本的最上層 parameters 物件會宣告可接受該整合帳戶資源識別碼的參數。

以下是參數定義的一般結構和語法,這完全由參數 - Resource Manager 範本結構和語法所述:

"<parameter-name>": {
   "type": "<parameter-type>",
   "defaultValue": <default-parameter-value>,
   <other-parameter-attributes>,
   "metadata": {
      "description": "<parameter-description>"
   }
},

此範例只會針對用於在 Azure 中建立和部署這些資源的值,顯示範本參數:

  • 邏輯應用程式的名稱和位置
  • 要用於連結至邏輯應用程式的整合帳戶的識別碼
{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   // Template parameters
   "parameters": {
      "LogicAppName": {
         "type": "string",
         "minLength": 1,
         "maxLength": 80,
         "defaultValue": "MyLogicApp",
         "metadata": {
            "description": "The resource name for the logic app"
         }
      },
      "LogicAppLocation": {
         "type": "string",
         "minLength": 1,
         "defaultValue": "[resourceGroup().location]",
         "metadata": {
            "description": "The resource location for the logic app"
         }
      },
      "LogicAppIntegrationAccount": {
         "type":"string",
         "minLength": 1,
         "defaultValue": "/subscriptions/<Azure-subscription-ID>/resourceGroups/fabrikam-integration-account-rg/providers/Microsoft.Logic/integrationAccounts/fabrikam-integration-account",
         "metadata": {
            "description": "The ID to use for the integration account"
         }
      }
   },
   "variables": {},
   "functions": [],
   "resources": [],
   "outputs": {}
}

除了可處理具敏感性的值或必須為安全值的參數 (例如使用者名稱、密碼和秘密) 之外,所有參數都包含 defaultValue 屬性,不過在某些情況下,預設值是空值。 要用於這些範本參數的部署值是由本主題稍後所述的範例 parameters 檔案所提供。

如需保護範本參數的詳細資訊,請參閱下列主題:

其他範本物件通常會參考範本參數,以便使用可傳遞範本參數的值,例如:

  • 本主題稍後所述的範本資源物件會定義您想要在 Azure 中建立和部署的每個資源,例如邏輯應用程式的資源定義。 這些資源通常會使用範本參數值,例如邏輯應用程式的名稱和位置及連線資訊。

  • 在邏輯應用程式的更深層資源定義中,工作流程定義的 parameters 物件會針對要在邏輯應用程式執行階段使用的值宣告參數。 例如,您可以針對 HTTP 觸發程序用於驗證的使用者名稱和密碼,宣告工作流程定義參數。 若要指定工作流程定義參數的值,請使用在工作流程定義以外,但仍在邏輯應用程式的資源定義之內parameters 物件。 在此外部 parameters 物件中,您可以參考先前宣告的範本參數,這些參數可在部署時接受來自 parameters 檔案的值。

參考參數時,範本運算式和函式會使用不同的語法,而且行為與工作流程定義運算式和函式不同。 如需這些差異的詳細資訊,請參閱本主題稍後的參數參考

最佳做法 - 範本參數

以下是定義參數的一些最佳做法:

  • 僅針對根據您的部署需求而有所不同的值宣告參數。 請勿針對在不同部署需求之間保持相同的值宣告參數。

  • 包含 defaultValue 屬性,其可針對所有參數指定空白值,但具敏感性或必須安全的值除外。 一律使用使用者名稱、密碼和秘密的安全參數。 若要隱藏或保護敏感性參數值,請遵循下列主題中的指引:

  • 若要區分範本參數名稱與工作流程定義參數名稱,您可以使用描述性範本參數名稱,例如:TemplateFabrikamPassword

如需更多範本最佳做法,請參閱範本參數的最佳做法

範本 parameters 檔案

若要提供範本參數的值,請將這些值儲存在 parameters 檔案中。 如此一來,您可以根據部署需求使用不同的 parameters 檔案。 以下是要使用的檔案名稱格式:

  • 邏輯應用程式範本檔案名稱:<logic-app-name>.json
  • Parameters 檔案名稱:<logic-app-name>.parameters.json

以下是 parameters 檔案內的結構,其中包含使用 Azure Key Vault 傳遞安全參數值的金鑰保存庫參考:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
   "contentVersion": "1.0.0.0",
   // Template parameter values
   "parameters": {
      "<parameter-name-1>": {
         "value": "<parameter-value>"
      },
      "<parameter-name-2>": {
         "value": "<parameter-value>"
      },
      "<secured-parameter-name>": {
         "reference": {
            "keyVault": {
               "id": "/subscriptions/<Azure-subscription-ID>/resourceGroups/<Azure-resource-group-name>/Microsoft.KeyVault/vaults/<key-vault-name>"
            },
            "secretName: "<secret-name>"
         }
      },
      <other-parameter-values>
   }
}

此範例 parameters 檔案指定本主題稍早所宣告之範本參數的值:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
   "contentVersion": "1.0.0.0",
   // Template parameter values
   "parameters": {
      "LogicAppName": {
         "value": "Email-Processor-Logic-App"
      },
      "LogicAppLocation": {
         "value": "westeurope"
      }
   }
}

範本資源

您的範本具有 resources 物件,這是一個陣列,其中包含要在 Azure 中建立和部署的每個資源的定義,例如邏輯應用程式的資源定義連線資源定義,以及邏輯應用程式需要用於部署的任何其他資源。

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {<template-parameters>},
   "variables": {},
   "functions": [],
   "resources": [
      {
         <logic-app-resource-definition>
      },
      // Start connection resource definitions
      {
         <connection-resource-definition-1>
      },
      {
         <connection-resource-definition-2>
      }
   ],
   "outputs": {}
}

注意

範本可以包含多個邏輯應用程式的資源定義,因此確定所有邏輯應用程式資源都指定相同的 Azure 資源群組。 當您使用 Visual Studio 將範本部署至 Azure 資源群組時,系統會提示您輸入要開啟的邏輯應用程式。 此外,您的 Azure 資源群組專案可以包含多個範本,因此務必在出現提示時選取正確的 parameters 檔案。

檢視資源定義

若要檢閱 Azure 資源群組中所有資源的資源定義,請將邏輯應用程式從 Azure 下載至 Visual Studio,這是建立最方便部署的有效參數化邏輯應用程式範本的最簡單方式。

如需範本資源及其屬性的一般資訊,請參閱下列主題:

邏輯應用程式資源定義

邏輯應用程式在範本中的工作流程資源定義會以 properties 物件開頭,其中包含此資訊:

  • 邏輯應用程式在部署時的狀態
  • 邏輯應用程式所使用之任何整合帳戶的識別碼
  • 邏輯應用程式的工作流程定義
  • parameters 物件,其設定要在執行階段使用的值
  • 邏輯應用程式的其他資源資訊,例如名稱、類型、位置、任何執行階段組態設定等等
{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {<template-parameters>},
   "variables": {},
   "functions": [],
   "resources": [
      {
         // Start logic app resource definition
         "properties": {
            "state": "<Enabled or Disabled>",
            "integrationAccount": {
               "id": "[parameters('LogicAppIntegrationAccount')]" // Template parameter reference
            },
            "definition": {<workflow-definition>},
            "parameters": {<workflow-definition-parameter-values>},
            "accessControl": {},
            "runtimeConfiguration": {}
         },
         "name": "[parameters('LogicAppName')]", // Template parameter reference
         "type": "Microsoft.Logic/workflows",
         "location": "[parameters('LogicAppLocation')]", // Template parameter reference
         "tags": {
           "displayName": "LogicApp"
         },
         "apiVersion": "2019-05-01",
         "dependsOn": [
         ]
      }
      // End logic app resource definition
   ],
   "outputs": {}
}

以下是邏輯應用程式資源定義特有的屬性:

屬性 必要 類型 描述
state String 邏輯應用程式在部署時的狀態,其中 Enabled 表示邏輯應用程式是即時的,而 Disabled 表示邏輯應用程式處於非作用中狀態。 例如,如果您尚未準備好讓邏輯應用程式上線,但想要部署草稿版本,您可以使用 Disabled 選項。
integrationAccount 物件 如果您的邏輯應用程式使用整合帳戶,以儲存企業對企業 (B2B) 案例的成品,此物件會包含 id 屬性,以指定整合帳戶的識別碼。
definition Object 邏輯應用程式的基礎工作流程定義,這是出現在程式碼檢視中的相同物件,而且在工作流程定義語言的結構描述參考主題中有完整說明。 在此工作流程定義中,parameters 物件會針對要在邏輯應用程式執行階段使用的值宣告參數。 如需詳細資訊,請參閱工作流程定義語言

若要檢視邏輯應用程式的工作流程定義中的屬性,請從 Azure 入口網站或 Visual Studio 中的「設計檢視」切換至「程式碼檢視」,或使用 Azure 資源總管之類的工具。

parameters 物件 要在邏輯應用程式執行階段使用的工作流程定義參數值。 這些值的參數定義會出現在您的工作流程定義的 parameters 物件內。 此外,如果邏輯應用程式使用受控連接器來存取其他服務和系統,此物件會包含 $connections 物件,該物件會設定要在執行階段使用的連線值。
accessControl 物件 若要指定邏輯應用程式的安全性屬性,例如限制 IP 存取以要求觸發程序或執行歷程記錄輸入和輸出。 如需詳細資訊,請參閱保護邏輯應用程式的存取
runtimeConfiguration 物件 指定任何可控制邏輯應用程式執行階段運作方式的 operationOptions 屬性。 例如,您可以在高輸送量模式中執行邏輯應用程式。

如需這些 Logic Apps 物件的資源定義詳細資訊,請參閱 Microsoft.Logic 資源類型

工作流程定義和參數

邏輯應用程式的工作流程定義會出現在 definition 物件中,而該物件出現在邏輯應用程式的資源定義內的 properties 物件中。 此 definition 物件是出現在程式碼檢視中的相同物件,並且在工作流程定義語言的結構描述參考主題中有完整說明。 工作流程定義包含內部 parameters 宣告物件,您可以在其中為工作流程定義在執行階段使用的值,定義新的參數或編輯現有參數。 然後,您可以在工作流程中的觸發程序或動作內參考這些參數。 根據預設,除非邏輯應用程式透過parameters受控連接器建立與其他服務和系統的連線,否則此 物件是空的。

若要設定工作流程定義參數的值,請使用在工作流程定義以外,但仍在邏輯應用程式的資源定義之內parameters 物件。 在此外部 parameters 物件中,您可以接著參考先前宣告的範本參數,以接受從 parameters 檔案部署時的值。

秘訣

最佳做法是不要從工作流程定義內部直接參考在部署時評估的範本參數。 相反地,宣告工作流程定義參數,然後可以在工作流程定義以外,但仍在邏輯應用程式的資源定義之內parameters 物件中設定。 如需詳細資訊,請參閱參數參考

此語法顯示您可以在範本和工作流程定義層級宣告參數的位置,以及您可藉由參考範本和工作流程定義參數來設定這些參數值的位置:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   // Template parameters
   "parameters": {
      "<template-parameter-name>": {
         "type": "<parameter-type>",
         "defaultValue": "<parameter-default-value>",
         "metadata": {
            "description": "<parameter-description>"
         }
      }
   },
   "variables": {},
   "functions": [],
   "resources": [
      {
         // Start logic app resource definition
         "properties": {
            <other-logic-app-resource-properties>,
            "definition": {
               "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
               "actions": {<action-definitions>},
               // Workflow definition parameters
               "parameters": {
                  "<workflow-definition-parameter-name>": {
                     "type": "<parameter-type>",
                     "defaultValue": "<parameter-default-value>",
                     "metadata": {
                        "description": "<parameter-description>"
                     }
                  }
               },
               "triggers": {
                  "<trigger-name>": {
                     "type": "<trigger-type>",
                     "inputs": {
                         // Workflow definition parameter reference
                         "<attribute-name>": "@parameters('<workflow-definition-parameter-name')"
                     }
                  }
               },
               <...>
            },
            // Workflow definition parameter value
            "parameters": {
               "<workflow-definition-parameter-name>": { 
                  "value": "[parameters('<template-parameter-name>')]"
               }
            },
            "accessControl": {}
         },
         <other-logic-app-resource-definition-attributes>
      }
      // End logic app resource definition
   ],
   "outputs": {}
}

保護工作流程定義參數

對於在執行階段處理敏感性資訊、密碼、存取金鑰或秘密的工作流程定義參數,請宣告或編輯此參數以使用 securestringsecureobject 參數類型。 您可以在整個工作流程定義內參考此參數。 在範本的最上層,宣告具有相同類型的參數,以在部署時處理此資訊。

若要設定工作流程定義參數的值,請使用在工作流程定義以外,但仍在邏輯應用程式資源定義之內parameters 物件來參考範本參數。 最後,若要在部署時將此值傳遞至範本參數,請將該值儲存在 Azure Key Vault中,並在部署時範本所使用的 parameters 檔案中參考該金鑰保存庫。

此範例範本示範如何在必要時定義安全的參數來完成這些工作,以便將其值儲存在 Azure Key Vault 中:

  • 針對用於驗證存取權的值宣告安全參數。
  • 在範本和工作流程定義層級使用這些值。
  • 使用 parameters 檔案提供這些值。

範本

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {
      <previously-defined-template-parameters>,
      // Additional template parameters for passing values to use in workflow definition
      "TemplateAuthenticationType": {
         "type": "string",
         "defaultValue": "",
         "metadata": {
            "description": "The type of authentication used for the Fabrikam portal"
         }
      },
      "TemplateFabrikamPassword": {
         "type": "securestring",
         "metadata": {
            "description": "The password for the Fabrikam portal"
         }
      },
      "TemplateFabrikamUserName": {
         "type": "securestring",
         "metadata": {
            "description": "The username for the Fabrikam portal"
         }
      }
   },
   "variables": {},
   "functions": [],
   "resources": [
      {
         // Start logic app resource definition
         "properties": {
            <other-logic-app-resource-properties>,
            // Start workflow definition
            "definition": {
               "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
               "actions": {<action-definitions>},
               // Workflow definition parameters
               "parameters": {
                  "authenticationType": {
                     "type": "string",
                     "defaultValue": "",
                     "metadata": {
                        "description": "The type of authentication used for the Fabrikam portal"
                     }
                  },
                  "fabrikamPassword": {
                     "type": "securestring",
                     "metadata": {
                        "description": "The password for the Fabrikam portal"
                     }
                  },
                  "fabrikamUserName": {
                     "type": "securestring",
                     "metadata": {
                        "description": "The username for the Fabrikam portal"
                     }
                  }
               },
               "triggers": {
                  "HTTP": {
                     "inputs": {
                        "authentication": {
                           // Reference workflow definition parameters
                           "password": "@parameters('fabrikamPassword')",
                           "type": "@parameters('authenticationType')",
                           "username": "@parameters('fabrikamUserName')"
                        }
                     },
                     "recurrence": {<...>},
                     "type": "Http"
                  }
               },
               <...>
            },
            // End workflow definition
            // Start workflow definition parameter values
            "parameters": {
               "authenticationType": {
                  "value": "[parameters('TemplateAuthenticationType')]" // Template parameter reference
               },
               "fabrikamPassword": {                  
                  "value": "[parameters('TemplateFabrikamPassword')]" // Template parameter reference
               },
               "fabrikamUserName": {
                  "value": "[parameters('TemplateFabrikamUserName')]" // Template parameter reference
               }
            },
            "accessControl": {}
         },
         <other-logic-app-resource-attributes>
      }
      // End logic app resource definition
   ],
   "outputs": {}
}

參數檔案

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
   "contentVersion": "1.0.0.0",
   // Template parameter values
   "parameters": {
      <previously-defined-template-parameter-values>,
     "TemplateAuthenticationType": {
        "value": "Basic"
     },
     "TemplateFabrikamPassword": {
        "reference": {
           "keyVault": {
              "id": "/subscriptions/<Azure-subscription-ID>/resourceGroups/<Azure-resource-group-name>/Microsoft.KeyVault/vaults/fabrikam-key-vault"
           },
           "secretName": "FabrikamPassword"
        }
     },
     "TemplateFabrikamUserName": {
        "reference": {
           "keyVault": {
              "id": "/subscriptions/<Azure-subscription-ID>/resourceGroups/<Azure-resource-group-name>/Microsoft.KeyVault/vaults/fabrikam-key-vault"
           },
           "secretName": "FabrikamUserName"
        }
     }
   }
}

最佳做法 - 工作流程定義參數

若要確定邏輯應用程式設計工具可以正確顯示工作流程定義參數,請遵循下列最佳做法:

如需工作流程定義參數的詳細資訊,請參閱參數 - 工作流程定義語言

連線資源定義

當邏輯應用程式使用受控連接器來建立和使用與其他服務和系統的連線時,範本的 resources 物件會包含這些連線的資源定義。 儘管您在邏輯應用程式內建立連線,這些連線會以自己的資源定義來分隔 Azure 資源。 若要檢閱這些連線資源定義,請將邏輯應用程式從 Azure 下載至 Visual Studio,這是建立最方便部署的有效參數化邏輯應用程式範本的最簡單方式。

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {<template-parameters>},
   "variables": {},
   "functions": [],
   "resources": [
      {
         <logic-app-resource-definition>
      },
      // Start connection resource definitions
      {
         <connection-resource-definition-1>
      },
      {
         <connection-resource-definition-2>
      }
   ],
   "outputs": {}
}

連線資源定義會參考範本的最上層參數作為其值,因此您可以使用 parameters 檔案在部署時提供這些值。 確定連線使用與邏輯應用程式相同的 Azure 資源群組和位置。

以下是 Office 365 Outlook 連線和對應範本參數的範例資源定義:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   // Template parameters
   "parameters": {
      "LogicAppName": {<parameter-definition>},
      "LogicAppLocation": {<parameter-definition>},
      "office365_1_Connection_Name": {
         "type": "string",
         "defaultValue": "office365",
         "metadata": {
            "description": "The resource name for the Office 365 Outlook connection"
         }
      },
      "office365_1_Connection_DisplayName": {
         "type": "string",
         "defaultValue": "",
         "metadata": {
            "description": "The display name for the Office 365 Outlook connection"
         }
      }
   },
   "variables": {},
   "functions": [],
   "resources": [
      {
         <logic-app-resource-definition>
      },
      // Office 365 Outlook API connection resource definition
      {
         "type": "Microsoft.Web/connections",
         "apiVersion": "2016-06-01",
         // Template parameter reference for connection name
         "name": "[parameters('office365_1_Connection_Name')]",
         // Template parameter reference for connection resource location. Must match logic app location.
         "location": "[parameters('LogicAppLocation')]",
         "properties": {
            "api": {
               // Connector ID
               "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'office365')]"
            },
            // Template parameter reference for connection display name
            "displayName": "[parameters('office365_1_Connection_DisplayName')]"
         }
      }
   ],
   "outputs": {}
}

邏輯應用程式的資源定義也會以下列方式搭配連線資源定義使用:

  • 在您的工作流程定義內,parameters 物件會針對要在邏輯應用程式執行階段使用的連線值宣告 $connections 參數。 此外,建立連線的觸發程序或動作會使用透過此 $connections 參數傳遞的對應值。

  • 在工作流程定義以外,但仍在邏輯應用程式的資源定義之內,另一個 parameters 物件會藉由參考對應的範本參數,為 $connections 參數設定要在執行階段使用的的值。 這些值會使用範本運算式來參考資源,以在邏輯應用程式中安全地儲存連線的中繼資料。

    例如,中繼資料可包含連接字串和存取權杖,您可以將其儲存在 Azure Key Vault 中。 若要將這些值傳遞至範本參數,您可參考範本在部署時所使用的 parameters 檔案中的該金鑰保存庫。 如需參考參數差異的詳細資訊,請參閱本主題稍後的參數參考

    當您透過 Azure 入口網站或 Visual Studio 在程式碼檢視中開啟邏輯應用程式的工作流程定義時,$connections 物件會出現在工作流程定義之外,但位於相同層級。 當您手動更新工作流程定義時,程式碼檢視中的這個排序可讓這些參數更容易參考:

    {
       "$connections": {<workflow-definition-parameter-connection-values-runtime},
       "definition": {<workflow-definition>}
    }
    
  • 邏輯應用程式的資源定義具有 dependsOn 物件,該物件可指定邏輯應用程式所使用連線的相依性。

您所建立的每個連線在 Azure 中都有唯一的名稱。 當您建立多個與相同服務或系統的連線時,每個連線名稱都會附加一個數字,這會隨著建立的每個新連線遞增,例如 office365office365-1 等等。

此範例顯示邏輯應用程式的資源定義與 Office 365 Outlook 的連線資源定義之間的互動:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   // Template parameters
   "parameters": {
      "LogicAppName": {<parameter-definition>},
      "LogicAppLocation": {<parameter-definition>},
      "office365_1_Connection_Name": {<parameter-definition>},
      "office365_1_Connection_DisplayName": {<parameter-definition>}
   },
   "variables": {},
   "functions": [],
   "resources": [
      {
         // Start logic app resource definition
         "properties": {
            <...>,
            "definition": {
               <...>,
               "parameters": {
                  // Workflow definition "$connections" parameter
                  "$connections": {
                     "defaultValue": {},
                     "type": "Object"
                  }
               },
               <...>
            },
            "parameters": {
               // Workflow definition "$connections" parameter values to use at runtime
               "$connections": {
                  "value": {
                     "office365": {
                        // Template parameter references
                        "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'office365')]",
                        "connectionId": "[resourceId('Microsoft.Web/connections', parameters('office365_1_Connection_Name'))]",
                        "connectionName": "[parameters('office365_1_Connection_Name')]"
                     }
                  }
               }
            }
         },
         <other-logic-app-resource-information>,
         "dependsOn": [
            "[resourceId('Microsoft.Web/connections', parameters('office365_1_Connection_Name'))]"
         ]
         // End logic app resource definition
      },
      // Office 365 Outlook API connection resource definition
      {
         "type": "Microsoft.Web/connections",
         "apiVersion": "2016-06-01",
         // Template parameter reference for connection name
         "name": "[parameters('office365_1_Connection_Name')]",
         // Template parameter reference for connection resource location. Must match logic app location.
         "location": "[parameters('LogicAppLocation')]",
         "properties": {
            "api": {
               // Connector ID
               "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'office365')]"
            },
            // Template parameter reference for connection display name
            "displayName": "[parameters('office365_1_Connection_DisplayName')]"
         }
      }
   ],
   "outputs": {}
}

保護連線參數

對於可處理敏感性資訊、密碼、存取金鑰或秘密的連線參數,連線的資源定義會包含 parameterValues 物件,該物件會以名稱/值組格式指定這些值。 若要隱藏此資訊,您可以使用 securestringsecureobject 參數類型來宣告或編輯這些值的範本參數。 然後,您可以將該資訊儲存在 Azure Key Vault 中。 若要將這些值傳遞至範本參數,您可參考範本在部署時所使用的 parameters 檔案中的該金鑰保存庫。

以下是針對 Azure Blob 儲存體連線提供帳戶名稱和存取金鑰的範例:

參數檔案

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
   "contentVersion": "1.0.0.0",
   // Template parameter values
   "parameters": {
      "LogicAppName": {
         "value": "Email-Processor-Logic-App"
      },
      "LogicAppLocation": {
         "value": "westeurope"
      },
      "azureblob_1_Connection_Name": {
         "value": "Fabrikam-Azure-Blob-Storage-Connection"
      },
      "azureblob_1_Connection_DisplayName": {
         "value": "Fabrikam-Storage"
      },
      "azureblob_1_accountName": {
         "value": "Fabrikam-Storage-Account"
      },
      "azureblob_1_accessKey": {
         "reference": {
            "keyVault": {
               "id": "/subscriptions/<Azure-subscription-ID>/resourceGroups/<Azure-resource-group-name>/Microsoft.KeyVault/vaults/fabrikam-key-vault"
            },
            "secretName": "FabrikamStorageKey"
         }
      }
   }
}

範本

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   // Template parameters
   "parameters": {
      "LogicAppName": {<parameter-definition>},
      "LogicAppLocation": {<parameter-definition>},
      "azureblob_1_Connection_Name": {<parameter-definition>},
      "azureblob_1_Connection_DisplayName": {<parameter-definition>},
      "azureblob_1_accountName": {
         "type": "string",
         "defaultValue": "",
         "metadata": {
            "description": "Name of the storage account the connector should use."
         }
      },
      "azureblob_1_accessKey": {
         "type": "secureobject",
         "metadata": {
            "description": "Specify a valid primary/secondary storage account access key."
         }
      }
   },
   "variables": {},
   "functions": [],
   "resources": [
      {
         "properties": {
            "state": "Disabled",
            "definition": {
               "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
               "actions": {
                  // Azure Blob Storage action
                  "Create_blob": {
                     "type": "ApiConnection",
                     "inputs": {
                        "host": {
                           "connection": {
                              // Workflow definition parameter reference for values to use at runtime
                              "name": "@parameters('$connections')['azureblob']['connectionId']"
                           }
                        },
                     },
                     "method": "post",
                     "body": "@triggerBody()?['Body']",
                     "path": "/datasets/default/files",
                     "queries": {
                        "folderPath": "/emails",
                        "name": "@triggerBody()?['Subject']",
                        "queryParametersSingleEncoded": true
                     },
                     "runAfter": {},
                     "runtimeConfiguration": {
                        "contentTransfer": {
                           "transferMode": "Chunked"
                        }
                     }
                  }
               },
               "parameters": {
                  // Workflow definition parameter for values to use at runtime
                  "$connections": {
                     "defaultValue": {},
                     "type": "Object"
                  }
               },
               "triggers": {<trigger-definition>},
               "contentVersion": "1.0.0.0",
               "outputs": {}
            },
            "parameters": {
               "$connections": {
                  "value": {
                     // Template parameter references for values to use at runtime
                     "azureblob": {
                        "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'azureblob')]",
                        "connectionId": "[resourceId('Microsoft.Web/connections', parameters('azureblob_1_Connection_Name'))]",
                        "connectionName": "[parameters('azureblob_1_Connection_Name')]"
                    }
                  }
               }
            },
            "name": "[parameters('LogicAppName')]",
            "type": "Microsoft.Logic/workflows",
            "location": "[parameters('LogicAppLocation')]",
            "tags": {
               "displayName": "LogicApp"
            },
            "apiVersion": "2019-05-01",
            // Template parameter reference for value to use at deployment
            "dependsOn": [
               "[resourceId('Microsoft.Web/connections', parameters('azureblob_1_Connection_Name'))]"
            ]
         }
      },
      // Azure Blob Storage API connection resource definition
      {
         "type": "Microsoft.Web/connections",
         "apiVersion": "2016-06-01",
         "name": "[parameters('azureblob_1_Connection_Name')]",
         "location": "[parameters('LogicAppLocation')]",
         "properties": {
            "api": {
               "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'azureblob')]"
            },
            "displayName": "[parameters('azureblob_1_Connection_DisplayName')]",
            // Template parameter reference for values to use at deployment
            "parameterValues": {
               "accountName": "[parameters('azureblob_1_accountName')]",
               "accessKey": "[parameters('azureblob_1_accessKey')]"
            }
         }
      }
   ],
   "outputs": {}
}

驗證連線

部署之後,邏輯應用程式就能搭配有效參數端對端運作。 不過,您仍然必須授權任何 OAuth 連線,以產生有效的存取權杖來驗證您的認證。 如需詳細資訊,請參閱授權 OAuth 連線

有些連線支援使用 Azure Active Directory (Azure AD) 服務主體,針對已在 Azure AD 中註冊的邏輯應用程式授權連線。 例如,此 Azure Data Lake 連線資源定義顯示如何參考可處理服務主體資訊的範本參數,以及範本如何宣告這些參數:

連線資源定義

{
   <other-template-objects>
   "type": "Microsoft.Web/connections",
   "apiVersion": "2016-06-01",
   "name": "[parameters('azuredatalake_1_Connection_Name')]",
   "location": "[parameters('LogicAppLocation')]",
   "properties": {
      "api": {
         "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', 'resourceGroup().location', '/managedApis/', 'azuredatalake')]"
      },
      "displayName": "[parameters('azuredatalake_1_Connection_DisplayName')]",
      "parameterValues": {
         "token:clientId": "[parameters('azuredatalake_1_token:clientId')]",
         "token:clientSecret": "[parameters('azuredatalake_1_token:clientSecret')]",
         "token:TenantId": "[parameters('azuredatalake_1_token:TenantId')]",
         "token:grantType": "[parameters('azuredatalake_1_token:grantType')]"
      }
   }
}
屬性 描述
token:clientId 與服務主體相關聯的應用程式或用戶端識別碼
token:clientSecret 與您的服務主體相關聯的金鑰值
token:TenantId 輸入 Azure AD 租用戶的目錄識別碼
token:grantType 要求的授與類型,其必須是 client_credentials。 如需詳細資訊,請參閱 Microsoft 身分識別平台與 OAuth 2.0 用戶端認證流程

範本參數定義

範本的最上層 parameters 物件會針對範例連線宣告這些參數:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {
      "azuredatalake_1_Connection_Name": {
        "type": "string",
        "defaultValue": "azuredatalake"
      },
      "azuredatalake_1_Connection_DisplayName": {
        "type": "string",
        "defaultValue": "<connection-name>"
      },
      "azuredatalake_1_token:clientId": {
        "type": "securestring",
        "metadata": {
          "description": "Client (or Application) ID of the Azure Active Directory application."
        }
      },
      "azuredatalake_1_token:clientSecret": {
        "type": "securestring",
        "metadata": {
          "description": "Client secret of the Azure Active Directory application."
        }
      },
      "azuredatalake_1_token:TenantId": {
        "type": "securestring",
        "metadata": {
          "description": "The tenant ID of for the Azure Active Directory application."
        }
      },
      "azuredatalake_1_token:resourceUri": {
        "type": "string",
        "metadata": {
          "description": "The resource you are requesting authorization to use."
        }
      },
      "azuredatalake_1_token:grantType": {
        "type": "string",
        "metadata": {
          "description": "Grant type"
        },
        "defaultValue": "client_credentials",
        "allowedValues": [
          "client_credentials"
        ]
      },
      // Other template parameters
   }
   // Other template objects
}

如需使用服務主體的詳細資訊,請參閱下列主題:

參數參考

若要參考範本參數,您可以使用範本運算式搭配在部署時評估的範本函式。 範本運算式會使用方括弧 ([]):

"<attribute-name>": "[parameters('<template-parameter-name>')]"

若要參考工作流程定義參數,您可使用在執行階段評估的工作流程定義語言運算式和函式。 您可能會注意到有些範本函式和工作流程定義函式的名稱相同。 工作流程定義運算式的開頭為 "at" 符號 (@):

"<attribute-name>": "@parameters('<workflow-definition-parameter-name>')"

您可以將範本參數值傳遞至工作流程定義,以便邏輯應用程式在執行階段使用。 不過,請避免在工作流程定義中使用範本參數、運算式和語法,因為邏輯應用程式設計工具不支援範本元素。 此外,範本語法和運算式可能因為評估運算式時的差異而使程式碼變複雜。

相反地,請遵循這些一般步驟來宣告及參考要在執行階段使用的工作流程定義參數、宣告及參考要在部署時使用的範本參數,以及使用 parameters 檔案指定要在部署時傳入的值。 如需完整詳細資料,請參閱本主題稍早的工作流程定義和參數一節。

  1. 建立您的範本,並針對要在部署時接受並使用的值宣告範本參數。

  2. 在工作流程定義中,針對要在執行階段接受和使用的值宣告參數。 然後,您可以在工作流程定義中參考這些值。

  3. 在工作流程定義以外,但仍在邏輯應用程式的資源定義之內parameters 物件中,藉由參考對應的範本參數來設定工作流程定義參數的值。 如此一來,您可以將範本參數值傳遞至工作流程定義參數。

  4. 在 parameters 檔案中,針對要在部署時使用的範本指定值。

完整範例範本

以下是本主題的範例所使用的參數化範例範本:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
   "parameters": {
      "LogicAppName": {
         "type": "string",
         "minLength": 1,
         "maxLength": 80,
         "defaultValue": "MyLogicApp",
         "metadata": {
            "description": "The resource name to use for the logic app"
         }
      },
      "LogicAppLocation": {
         "type": "string",
         "minLength": 1,
         "defaultValue": "[resourceGroup().location]",
         "metadata": {
            "description": "The resource location to use for the logic app"
         }
      },
      "office365_1_Connection_Name": {
         "type": "string",
         "defaultValue": "office365",
         "metadata": {
            "description": "The resource name to use for the Office 365 Outlook connection"
         }
      },
      "office365_1_Connection_DisplayName": {
         "type": "string",
         "defaultValue": "",
         "metadata": {
            "description": "The display name to use for the Office 365 Outlook connection"
         }
      },
      "azureblob_1_Connection_Name": {
         "type": "string",
         "defaultValue": "azureblob",
         "metadata": {
            "description": "The resource name to use for the Azure Blob storage account connection"
         }
      },
      "azureblob_1_Connection_DisplayName": {
         "type": "string",
         "defaultValue": "",
         "metadata": {
            "description": "Name of the storage account the connector should use."
         }

      },
      "azureblob_1_accountName": {
         "type": "string",
         "defaultValue": "",
         "metadata": {
            "description": "Name of the storage account the connector should use."
         }
      },
      "azureblob_1_accessKey": {
         "type": "securestring",
         "metadata": {
            "description": "Specify a valid primary/secondary storage account access key."
         }
      },
      "LogicAppIntegrationAccount": {
         "type":"string",
         "minLength": 1,
         "defaultValue": "/subscriptions/<Azure-subscription-ID>/resourceGroups/fabrikam-integration-account-rg/providers/Microsoft.Logic/integrationAccounts/fabrikam-integration-account",
         "metadata": {
            "description": "The ID to use for the integration account"
         }
      }
   },
   "variables": {},
   "resources": [
      {
         "properties": {
            "state": "Disabled",
            "integrationAccount": {
              "id": "[parameters('LogicAppIntegrationAccount')]"
            },
            "definition": {
               "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
               "actions": {
                  "Create_blob": {
                     "type": "ApiConnection",
                     "inputs": {
                        "host": {
                           "connection": {
                              "name": "@parameters('$connections')['azureblob']['connectionId']"
                           }
                        }
                     },
                     "method": "post",
                     "body": "@triggerBody()?['Body']",
                     "path": "/datasets/default/files",
                     "queries": {
                        "folderPath": "/emails",
                        "name": "@triggerBody()?['Subject']",
                        "queryParametersSingleEncoded": true
                     },
                     "runAfter": {},
                     "runtimeConfiguration": {
                        "contentTransfer": {
                           "transferMode": "Chunked"
                        }
                     }
                  }
               },
               "parameters": {
                  "$connections": {
                     "defaultValue": {},
                     "type": "Object"
                  }
               },
               "triggers": {
                  "When_a_new_email_arrives": {
                     "type": "ApiConnection",
                     "inputs": {
                        "host": {
                           "connection": {
                              "name": "@parameters('$connections')['office365']['connectionId']"
                           }
                        },
                        "method": "get",
                        "path": "/Mail/OnNewEmail",
                        "queries": {
                           "folderPath": "Inbox",
                           "importance": "Any",
                           "fetchOnlyWithAttachment": false,
                           "includeAttachments": false
                        }
                     },
                     "recurrence": {
                        "frequency": "Day",
                        "interval": 1
                     },
                     "splitOn": "@triggerBody()?['value']"
                  }
               },
               "contentVersion": "1.0.0.0",
               "outputs": {}
            },
            "parameters": {
               "$connections": {
                  "value": {
                     "azureblob": {
                        "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'azureblob')]",
                        "connectionId": "[resourceId('Microsoft.Web/connections', parameters('azureblob_1_Connection_Name'))]",
                        "connectionName": "[parameters('azureblob_1_Connection_Name')]"
                     },
                     "office365": {
                        "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'office365')]",
                        "connectionId": "[resourceId('Microsoft.Web/connections', parameters('office365_1_Connection_Name'))]",
                        "connectionName": "[parameters('office365_1_Connection_Name')]"
                     }
                  }
               }
            },
            "accessControl": {}
         },
         "name": "[parameters('LogicAppName')]",
         "type": "Microsoft.Logic/workflows",
         "location": "[parameters('LogicAppLocation')]",
         "tags": {
            "displayName": "LogicApp"
         },
         "apiVersion": "2019-05-01",
         "dependsOn": [
            "[resourceId('Microsoft.Web/connections', parameters('azureblob_1_Connection_Name'))]",
            "[resourceId('Microsoft.Web/connections', parameters('office365_1_Connection_Name'))]"
         ]
      },
      {
         "type": "Microsoft.Web/connections",
         "apiVersion": "2016-06-01",
         "name": "[parameters('office365_1_Connection_Name')]",
         "location": "[parameters('LogicAppLocation')]",
         "properties": {
            "api": {
                "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'office365')]"
            },
            "displayName": "[parameters('office365_1_Connection_DisplayName')]"
         }
      },
      {
         "type": "Microsoft.Web/connections",
         "apiVersion": "2016-06-01",
         "name": "[parameters('azureblob_1_Connection_Name')]",
         "location": "[parameters('LogicAppLocation')]",
         "properties": {
            "api": {
               "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('LogicAppLocation'), '/managedApis/', 'azureblob')]"
            },
            "displayName": "[parameters('azureblob_1_Connection_DisplayName')]",
            "parameterValues": {
               "accountName": "[parameters('azureblob_1_accountName')]",
               "accessKey": "[parameters('azureblob_1_accessKey')]"
            }
         }
      }
   ],
   "outputs": {}
}

後續步驟