如何使用azure-sdk-python创建标准计划的Azure Logic App
如何通过azure-sdk-for-python创建Standard计划的Azure Logic App?
我正尝试使用azure-sdk-for-python自动化Azure Logic App的创建流程。以下是我的Python脚本:
import schedule import time from azure.identity import DefaultAzureCredential from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.logic import LogicManagementClient def create_logic_app(iteration): subscription_id = '' # replace with your Azure subscription ID resource_group_name = '' # replace with your resource group name logic_app_name = f'Twa_{iteration}' # Unique name for each Logic App location = 'East US' # Azure region credential = DefaultAzureCredential() resource_client = ResourceManagementClient(credential, subscription_id) logic_client = LogicManagementClient(credential, subscription_id) print("Creating/Updating resource group...") resource_client.resource_groups.create_or_update(resource_group_name, {'location': location}) logic_app_parameters = { 'location': location, "properties": { "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "Compose": { "inputs": "Insert logic", "runAfter": {}, "type": "Compose" } }, "contentVersion": "1.0.0.0", "outputs": {}, "triggers": { "Recurrence": { "recurrence": { "frequency": "Minute", "interval": 3 }, "type": "Recurrence" } } }, "kind": "Stateful" } # Additional parameters here } print(f"Creating/Updating Logic App: {logic_app_name}...") logic_client.workflows.create_or_update(resource_group_name, logic_app_name, logic_app_parameters) print(f"Logic App {logic_app_name} created/updated successfully.") def job(): for i in range(10): create_logic_app(i) create_logic_app("recurrence")
当前脚本创建的是计划类型为consumption(消费)的Logic App,我希望创建计划类型为Standard(标准)的Logic App,请问该如何实现?
解决方案
Standard计划的Logic App本质是基于App Service的资源,和消费型的创建逻辑差异较大,需要对脚本做以下关键修改:
1. 安装额外依赖包
Standard逻辑应用的管理需要用到azure-mgmt-web包,执行以下命令安装:
pip install azure-mgmt-web
2. 修改后的完整脚本
import schedule import time from azure.identity import DefaultAzureCredential from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.logic import LogicManagementClient from azure.mgmt.web import WebSiteManagementClient # 新增:用于管理Standard Logic App相关资源 def create_logic_app(iteration): subscription_id = '' # 替换为你的Azure订阅ID resource_group_name = '' # 替换为你的资源组名称 logic_app_name = f'Twa_{iteration}' # Standard Logic App实例名称 app_service_plan_name = f'ASP-{logic_app_name}' # 关联的App Service计划名称 workflow_name = f'MainWorkflow_{iteration}' # 逻辑应用内的工作流名称 location = 'East US' # Azure区域 credential = DefaultAzureCredential() resource_client = ResourceManagementClient(credential, subscription_id) logic_client = LogicManagementClient(credential, subscription_id) web_client = WebSiteManagementClient(credential, subscription_id) # 初始化Web管理客户端 print("Creating/Updating resource group...") resource_client.resource_groups.create_or_update(resource_group_name, {'location': location}) # 1. 创建App Service计划(Standard逻辑应用必须依赖此计划,需选择Premium/Isolated级SKU) print(f"Creating/Updating App Service Plan: {app_service_plan_name}...") app_service_plan_params = { "location": location, "sku": { "name": "P1v3", # 可根据需求调整SKU,如P2v3、I1v1等 "tier": "PremiumV3", "size": "P1v3", "family": "Pv3" } } web_client.app_service_plans.create_or_update( resource_group_name, app_service_plan_name, app_service_plan_params ) # 2. 创建Standard Logic App实例(本质是App Service站点) print(f"Creating/Updating Standard Logic App Site: {logic_app_name}...") logic_app_site_params = { "location": location, "server_farm_id": f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Web/serverfarms/{app_service_plan_name}", "kind": "workflowapp", # 标识为Standard逻辑应用 "properties": { "siteConfig": { "appSettings": [ # 配置工作流扩展包,Standard逻辑应用必需 {"name": "AzureFunctionsJobHost__extensionBundle__id", "value": "Microsoft.Azure.Functions.ExtensionBundle.Workflows"}, {"name": "AzureFunctionsJobHost__extensionBundle__version", "value": "[1.*, 2.0.0)"} ] } } } web_client.web_apps.create_or_update( resource_group_name, logic_app_name, logic_app_site_params ) # 3. 在Standard Logic App实例下创建工作流 print(f"Creating/Updating Workflow: {workflow_name}...") workflow_params = { 'location': location, "properties": { "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2019-05-01/workflowdefinition.json#", # Standard对应的schema版本 "actions": { "Compose": { "inputs": "Insert logic", "runAfter": {}, "type": "Compose" } }, "contentVersion": "1.0.0.0", "outputs": {}, "triggers": { "Recurrence": { "recurrence": { "frequency": "Minute", "interval": 3 }, "type": "Recurrence" } }, "kind": "Stateful" # 指定工作流类型为有状态 }, "kind": "Stateful", "host": { "name": logic_app_name # 关联到已创建的Standard Logic App实例 } } } logic_client.workflows.create_or_update( resource_group_name, workflow_name, workflow_params ) print(f"Standard Logic App {logic_app_name} 及工作流 {workflow_name} 创建/更新成功。") def job(): for i in range(10): create_logic_app(i) create_logic_app("recurrence")
3. 关键修改说明
- 新增WebSiteManagementClient:用于创建Standard逻辑应用依赖的App Service计划和基础站点实例
- 创建App Service计划:Standard逻辑应用必须绑定Premium或Isolated级别的App Service计划,不能使用免费/共享/基础SKU
- 调整逻辑应用实例类型:通过
kind: workflowapp标识这是Standard逻辑应用,并配置必需的扩展包应用设置 - 更新工作流Schema:Standard逻辑应用使用2019-05-01版本的工作流定义Schema,替换原消费型的2016版本
- 关联工作流与实例:通过
host.name将工作流绑定到已创建的Standard逻辑应用站点
内容的提问来源于stack exchange,提问作者Jasneet Singh
相关产品推荐
相关产品推荐

