英文:
Azure Automation and Logic Apps for scheduled auto-scaling of Function App
问题
我正在尝试创建一个Azure自动化系统,用于更改Function App的Always Ready实例数量(App Service计划为Elastic Premium One EP1):
自动化系统被创建,以便在工作日将Always Ready实例数量设置为3,并在周末设置为1。
该系统由以下两个在Azure自动化帐户下创建的运行簿组成:
- "runbook-Weekdays"
$resourceGroupName = "xxxxxxxxxxxx"
$functionApp = "xxxxxxxxxx-func"
$Resource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceName $functionApp/config/web -ResourceType Microsoft.Web/sites
$Resource.Properties.minimumElasticInstanceCount = 3
$Resource | Set-AzResource -Force
- "runbook-Weekend-days"
$resourceGroupName = "xxxxxxxxxxxx"
$functionApp = "xxxxxxxxxxxx-func"
$Resource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceName $functionApp/config/web -ResourceType Microsoft.Web/sites
$Resource.Properties.minimumElasticInstanceCount = 1
$Resource | Set-AzResource -Force
请注意,我在我的自动化帐户中使用了“System Assigned”身份。
同时,自动化逻辑是使用Azure Logic Apps实现的:
在“Condition”框下的表达式是:formatDateTime(utcNow(),'dddd')
,这只设置为工作日,如前图所示。
下图是Logic App的第二部分:
请注意,在Logic Apps中创建自动化作业时,我选择了“OAuth default”作为身份验证方法,然后只需粘贴了我的订阅的tenantID。
现在,如果我从Logic App服务中测试Logic App,它可以正常工作:
我期望Always Ready实例的数量等于3,但如果我检查Always Ready实例的数量,没有任何变化:
此外,在运行簿页面的错误页面中:
我看到以下两个错误:
Get-AzResource : Run Connect-AzAccount to login.
和
The property 'minimumElasticInstanceCount' cannot be found on this object.
你知道可能出现什么问题吗?
英文:
I'm trying to create an Azure automation system to change the number of always ready instances of a Function App (the App Service Plan is Elastic Premium One EP1):
The Automation System is created in order to set the number of Always Ready Instances to 3 during week days, and set it to 1 during weekend days.
The System is composed by the following two runbooks created under the service Azure Automation Accounts:
- "runbook-Weekdays"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$resourceGroupName = "xxxxxxxxxxxx"
$functionApp = "xxxxxxxxxx-func"
$Resource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceName $functionApp/config/web -ResourceType Microsoft.Web/sites
$Resource.Properties.minimumElasticInstanceCount = 3
$Resource | Set-AzResource -Force
<!-- end snippet -->
- "runbook-Weekend-days"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$resourceGroupName = "xxxxxxxxxxxx"
$functionApp = "xxxxxxxxxxxx-func"
$Resource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceName $functionApp/config/web -ResourceType Microsoft.Web/sites
$Resource.Properties.minimumElasticInstanceCount = 1
$Resource | Set-AzResource -Force
<!-- end snippet -->
Note that I'm using a "System Assigned" identity in my Automation Account.
While the automation logic is implemented using Azure Logic Apps:
Where the expression under the "Condition" box is: formatDateTime(utcNow(),'dddd')
, which set only for the week days as can be seen from the previous figure.
The following figure is the second part of the Logic App:
Note that, when creating the Automation Job in Logic Apps, I have selected "OAuth default" as authentication method, then I simply pasted the tenantID of my subscription.
Now, if I test the Logic App from the Logic App service, it works:
And I would expect a number of always ready instances equal to 3, but if I check the number of always ready instances, nothing changed:
Also, going to the errors page of the runbook page:
I see the following two errors:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
Get-AzResource : Run Connect-AzAccount to login. At line:4 char:13 + $Resource = Get-AzResource -ResourceGroupName $resourceGroupName -Res ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzResource], PSInvalidOperationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet
<!-- end snippet -->
and
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
The property 'minimumElasticInstanceCount' cannot be found on this object. Verify that the property exists and can be set. At line:5 char:1 + $Resource.Properties.minimumElasticInstanceCount = 3 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
<!-- end snippet -->
Do you know what could be the problem?
答案1
得分: 1
你是否已经在你的运行簿中登录?
第一个错误告诉你使用 Connect-AzAccount
进行连接,第二个错误是一个通用错误,因为 $Resource
对象不具有该属性。
- 第一个错误:
在 Automation Account 控制台中,转到“帐户设置” -> “标识”,确保已启用系统分配的标识:
然后,点击“Azure角色分配”按钮,检查你的函数应用程序资源组中是否具有“Contributor”角色,并分配给 Azure 自动化帐户:
最后,在你的脚本中添加以下代码:
# 确保你的运行簿不会继承 AzContext
Disable-AzContextAutosave -Scope Process
# 使用系统分配的托管标识连接到 Azure
$AzureContext = (Connect-AzAccount -Identity).context
# 设置并存储上下文
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
- 第二个错误:
进入函数应用程序控制台,然后在“设置”下点击“配置”,验证是否具有属性“minimumElasticInstanceCount”:
如果你没有此属性,可以通过点击“新应用程序设置”按钮来创建它。
这是一个有用的 Azure 文档链接:
英文:
Have you logged in within your runbook?
The first error tells you to connect using Connect-AzAccount
and the 2nd error is a generic error that the property doesn't exist because $Resource
object doesn't have that property.
- First error:
In the Automation Account console, under Account Settings -> Identity, be sure to have the System Assigned identity to on:
Then, click in the "Azure role assignments" button and check if you have the "Contributor" role in the resource group of your function app and assigned to the azure automation account:
Finally add this code in your scripts:
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# Set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
- Second error:
Go to the Function App console, then under Settings click on "Configuration" and verify that you have the property "minimumElasticInstanceCount":
If you don't have this property you can simply create it clicking in the "New application setting" button.
This is a useful Azure documentation:
答案2
得分: 0
错误消息"Run Connect-AzAccount to login" 表明在允许进行该调用的安全上下文中未执行该命令。
在您的情况下,是逻辑应用在进行该调用。因此,首先要检查的是您的逻辑应用的安全上下文。
英文:
The error "Run Connect-AzAccount to login" indicates that the call to run the command is not made in a security context that is allowed to make the call.
In your case it is the logic app that is making the call. So first thing to check is the security context of your logic app.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论