如何使Azure ARM JSON模板中的Azure函数代码可读 [英] How to make Azure Function code readable in Azure ARM json template

查看:99
本文介绍了如何使Azure ARM JSON模板中的Azure函数代码可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure资源组,其中包含一个调用Azure函数的Azure逻辑应用程序.

I have an Azure Resource group that contains an Azure Logic App that calls into an Azure Function.

我将此资源组导出为ARM模板,因此可以将资源重新导入到另一个Azure订阅.这可以很好地工作,但是问题是,Azure功能代码(100+行c#文件)全部包含在JSON ARM模板文件的一行中.这使得很难从模板本身读取或修改Azure功能.

I exported this Resource Group as an ARM template so I can re-import the resources to another Azure Subscription. This works fine, but the problem is, the Azure Function Code (100+ line c# file) is all included on one line of the JSON ARM template file. This makes is really hard to read or modify the Azure Function from the template itself.

是否有解决此问题的简便方法?理想情况下,我的Azure函数将位于其自己的文件(run.csx)中,而Azure JSON ARM模板将仅引用该外部文件.

Is there an easy way to work around this? Ideally my Azure Function would be in it's own file (run.csx), and the Azure JSON ARM template would just reference that external file.

这是我在ARM模板中用于功能资源的JSON Blob.我关心的是包含run.csx的行,如何使此代码更具可读性,并使开发人员更容易编辑?

Here is my JSON blob for the Function Resource in the ARM template. The line that contains run.csx for a key is my concern, how can I make this code more readable and easy for devs to edit?

{
  "apiVersion": "2015-08-01",
  "name": "[concat(parameters('test_site_name'),'\/ProvisionUser')]",
  "type": "Microsoft.Web\/sites\/functions",
  "properties": {
    "config": {
      "bindings": [
        {
          "authLevel": "function",
          "name": "req",
          "type": "httpTrigger",
          "direction": "in"
        },
        {
          "name": "return",
          "direction": "out",
          "type": "http"
        }
      ]
    },
    "files": {
      "run.csx": "LOTS OF C# CODE HERE - LOTS OF C# CODE HERE FROM MY AZURE FUNCTION - LOTS OF C# CODE HERE FROM MY AZURE FUNCTION - LOTS OF C# CODE HERE FROM MY AZURE FUNCTION - LOTS OF C# CODE HERE FROM MY AZURE FUNCTION - LOTS OF C# CODE HERE FROM MY AZURE FUNCTION - LOTS OF C# CODE HERE FROM MY AZURE FUNCTION - ",
      "project.json": "{\r\n  \"frameworks\": {\r\n    \"net46\": {\r\n      \"dependencies\": {\r\n        \"Microsoft.IdentityModel.Clients.ActiveDirectory\": \"3.13.8\",\r\n        \"Newtonsoft.Json\":  \"10.0.2\",\r\n         \"Microsoft.Sdk.CoreAssemblies\" : \"8.2.0.2\"\r\n      }\r\n    }\r\n  }\r\n}"
    }
  }
}

推荐答案

您有一些选择:

  1. 快速解决您的问题:通过某些代码格式化程序运行ARM模板.如果您尝试将模板复制粘贴到Visual Studio中的json文件中,然后按CTRL-K,CTRL-D自动格式化,则可能会很幸运.我没有尝试过,但可能会起作用.您还可以使用多种在线格式化工具中的任何一种或使用Visual Studio剪切代码并对其进行格式化.

  1. Quick fix to your question: Run your ARM template through some code formatter. You may be in luck if you try copy-paste the template in to a json file in Visual Studio and then CTRL-K,CTRL-D to auto format it. I have not tried this but it may work. You can also cut the code out and format it using any one of a number of online formatting tools or using Visual Studio.

源代码控制系统.分别对待您的基础架构和代码. IE.从ARM模板创建功能PaaS服务,然后使用CI/CD流程部署代码和配置(功能).

Deploy your functions from a source control system. Treat your infrastructure and code separately. I.e. Create your functions PaaS service from your ARM templates, but then use a CI/CD process to deploy your code and configuration (the functions).

将代码包装到一个程序集中,将该程序集部署到您的函数宿主中,并在您的函数中引用它.这称为外部参考(此处的文档),并将逻辑中的代码存储在单独的程序集中,从而限制了函数中的代码量.您仍然需要解决如何通过脚本或CI/CD流程部署程序集.

Wrap your code in to an assembly, deploy the assembly to your function host and reference it in your function. This is called an external reference (documentation here) and will limit the amount of code in your function to plumbing, with your logic kept in a separate assembly. You will still need to work out how to deploy the assembly through script or your CI/CD process.

简而言之,并且与对您的问题的评论保持一致,从开发过程的角度出发,您需要更加勤奋地支持Azure函数的开发.如果您将有许多开发人员在使用您的功能,则这一点变得尤为重要.

In short, and in line with the comments on your question, you need to support your Azure function development with a little more diligence from a development process perspective. This becomes even more critical if you will have a number of developers working on your functions.

祝你好运!

这篇关于如何使Azure ARM JSON模板中的Azure函数代码可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆