【Azure 环境】ARM部署模板大于4MB的解决方案及Linked Template遇见存储账号防火墙无法访问

发布时间 2023-08-01 19:34:53作者: 路边两盏灯

问题一:在ADF Pipeline部署ARM Template报错“Deployment failed -- the request content size exceeds the maximum size of 4MB”

image

【解答】

4MB是一个固定限制,不可以修改其大小。 如果Template文件太大,需要把拆分成多个后,通过Linked Template的方式部署。 

在部署的时候,ARM通过main Template文件中配置的 templateLink.uri 来获取模板内容,所以这个URI必须是可以被ARM通过HTTP/HTTPS访问的地址。 如 Storage Account。

image

(Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/best-practices#template-limits )

image

(Source Link: https://learn.microsoft.com/en-us/azure/azure-resource-manager/troubleshooting/error-job-size-exceeded?tabs=json#solution-2-simplify-template )

 

问题二:采用Linked Template方案,把大于4MB的ARM Template拆分成小于4MB的文件,然后存储在Storage Account. 但是由于安全原因,Storage Account 必须启用防火墙,尽管把ARM所属于的Service Tags中的所有IPv4地址添加到白名单中,但依旧回遇见 403 错误 (SASIpAuthorization)

image

【解答】

在部署ARM时,将由ARM服务的实例从Storage Account访问Linked Template uri, 查看ARM服务的Service Tags,存在IPv4 和 IPv6, 而在目前阶段,Storage Account不允许配置IPv6地址。

如果ARM访问模板时,其执行实例恰巧使用的IPv6地址的情况下,就会遇见403错误。

image

(Source: https://www.microsoft.com/en-us/download/details.aspx?id=57062)

 

所以,在ARM中,也明确的指出了 Linked Template 不适用于 Storage Account 打开防火墙的场景。 

image

( Source : https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/linked-templates?tabs=azure-powershell#securing-an-external-template)

 

问题三:基于把Linked Template存放在Storage Account,但因为启用防火墙而无法访问的情况下,是否有其他的代替方案呢?

【解答】

有,使用 Template Spec,通过把main template和linked  Template上传到Azure Template Spec中,通过调用az deployment group create 的方式进行调用。参考链接:https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-specs-create-linked?tabs=azure-powershell

上传到Azure Template Spec中有两种方式:

a.直接上传Main Template,这个过程会直接将所有的link templates作为统一的request body调用Azure API,这时候很可能会受到文件大小4MB的限制。

b.先上传link template,然后在main template中修改templateLink属性中的id为link template 在Azure 中的resource id。之后直接通过az命令对main template进行部署即可。但是这个方式有一定的弊端:通过调用main template的方式,Azure会首先将link template中的资源进行部署,在确认他们所有部署成功后进行main template的部署,经过之前的测验该方式的部署时间在30分钟左右。

关于Template Spec的权限和费用问题:

1) 需要上传template spec的话通常需要用到RBAC权限 Template Spec Contributor.  其它权限参考: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-specs?tabs=azure-powershell#required-permissions

2) Template Spec is not a charged service in Azure. (Template Spec 是不收费的Azure服务)