共用方式為


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

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

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

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

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

提示

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

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

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

如需 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 中建立和自定義部署資源時要使用的值。 例如,這些參數會接受邏輯應用程式名稱和位置、連線和其他部署所需資源的值。 您可以將這些參數值儲存在參數檔案,本主題稍後會加以說明。 如需一般詳細數據,請參閱 參數 - 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 屬性,但在某些情況下,預設值是空值。 要用於這些範本參數的部署值是由本主題稍後所述的範例 參數檔案 所提供。

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

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

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

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

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

最佳做法 - 範本參數

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

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

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

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

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

範本參數檔案

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

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

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

{
   "$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 資源群組專案可以包含多個範本,因此請確定您在出現提示時選取正確的參數檔案。

檢視資源定義

若要檢閱 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 Yes String 邏輯應用程式在部署時的狀態,其中 Enabled 表示邏輯應用程式是即時的,表示 Disabled 邏輯應用程式處於非使用中狀態。 例如,如果您尚未準備好讓邏輯應用程式上線,但想要部署草稿版本,您可以使用 Disabled 選項。
integrationAccount No Object 如果您的邏輯應用程式使用整合帳戶來儲存企業對企業案例的成品,此物件會包含 id 屬性,其會指定整合帳戶的標識符。
definition Yes Object 邏輯應用程式的基礎工作流程定義,其與出現在程式代碼檢視中的物件相同,而且在工作流程定義語言的架構參考主題中會完整描述。 在此工作流程定義中 parameters ,物件會宣告要在邏輯應用程式運行時間使用之值的參數。 如需詳細資訊,請參閱 工作流程定義和參數

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

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

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

工作流程定義和參數

邏輯應用程式的工作流程定義會出現在物件中 definition ,該物件會出現在 properties 邏輯應用程式資源定義內的物件中。 這個definition物件與出現在程式代碼檢視中的物件相同,而且會在工作流程定義語言的架構參考主題中完整描述。 您的工作流程定義包含內部 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 金鑰保存庫,並在部署時範本所使用的參數檔案中參考該密鑰保存庫

此範例範本示範如何在必要時定義受保護的參數來完成這些工作,以便將其值儲存在 Azure 金鑰保存庫:

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

範本

{
   "$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 資源,其本身的資源定義。 此外,如果您的連線使用內部部署數據閘道資源,此資源定義會與連接器資源定義分開存在。 如需詳細資訊,請參閱 內部部署數據閘道資源定義Microsoft.Web connectionGateways

若要檢閱連線資源定義, 請將邏輯應用程式從 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": {}
}

聯機資源定義會參考範本最上層參數的值,讓您可以使用參數檔案在部署時提供這些值。 請確定連線使用與邏輯應用程式相同的 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 金鑰保存庫。 若要將這些值傳遞至範本參數,您可以在部署時參考範本所使用的參數檔案中的該金鑰保存庫。 如需參考參數差異的詳細資訊,請參閱 本主題稍後的參數參考

    當您透過 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": {}
}

內部部署數據閘道資源定義

如果您的連線使用內部部署資料閘道資源,此資源定義會與連接器資源定義分開存在。 若要檢視數據閘道的資源定義,請遵循下列步驟:

  1. 在 Azure 入口網站,尋找並檢視內部部署數據閘道的 Azure 資源。

  2. 在資源功能表上的 [自動化] 底下,選取 [導出範本]。

    在 Azure 產生範本之後,閘道的資源定義會出現在程式碼視窗中。

如需詳細資訊,請參閱 Microsoft.Web connectionGateways

保護連線參數

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

以下是提供 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 連線

某些連線支援使用 Microsoft Entra 服務主體來授權已在 Microsoft Entra 識別符中註冊之邏輯應用程式的連線。 例如,此 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 Microsoft Entra 租用戶的目錄標識碼
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>')"

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

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

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

  2. 在您的工作流程定義中,宣告要在運行時間接受和使用之值的參數。 然後,您可以在工作流程定義中參考這些值。

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

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

完整範例範本

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

{
  "$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": {}
}

下一步