我在使用 Azure JavaScript SDK 创建函数应用程序时遇到一些问题。我可以创建应用程序,但没有添加配置变量,也没有设置运行时。
我正在使用@azure/arm-appservice并尝试遵循 vscode-azurefunctions 中使用的相同创建模式因为我找不到任何其他代码示例。
我的理解是你首先创建一个 WebSiteManagementClient使用一些凭据和 subscriptionId。
const subscriptionId = '<subscriptionId>'
const webSiteClient = new WebSiteManagementClient(new DefaultAzureCredential(), subscriptionId);
之后您可以使用 WebApps.beginCreateOrUpdateAndWait函数来创建 Function App 资源。但是,“siteEnvelope”参数的文档似乎与 vscode 扩展中的代码不匹配。我也不知道您将在哪里传递应用程序功能配置。这是我到目前为止所拥有的。
const resourceGroupName = "my-resource-group";
const siteName = "my-new-function";
const serverFarmPath = "<id_from_portal>";
const serverFarmId = `/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Web/serverFarms/${serverFarmPath}`;
const createAppResult = await webSiteClient.webApps.beginCreateOrUpdateAndWait(
resourceGroupName,
siteName,
{
name: siteName,
kind: 'functionapp',
location: 'East US 2',
serverFarmId: serverFarmId,
clientAffinityEnabled: false,
siteConfig: [
{
name: 'AzureWebJobsStorage',
value: '<connection_string>'
},
{
name: 'FUNCTIONS_EXTENSION_VERSION',
value: '~4'
},
{
name: "FUNCTIONS_WORKER_RUNTIME",
value: "node"
},
{
name: "WEBSITE_NODE_DEFAULT_VERSION",
value: "~14"
},
{
name: "APPINSIGHTS_INSTRUMENTATIONKEY",
value: '<key>'
},
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING',
value: '<connection_string>'
},
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING',
value: '<connection_string>'
},
{
name: 'MY_CUSTOM_ENV_VARIABLES',
value: 'some value'
}
],
reserved: true // The secret property - must be set to true to make it a Linux plan. Confirmed by the team who owns this API.
}
);
上面的代码将执行并创建资源,但生成的 Function App 不会设置任何配置变量。
据我所知,这段代码基本上与 vscode-azurefunctions 使用的结构相同。请参阅此处 commands/createFunctionApp/FunctionAppCreateStep.ts 中的相关代码示例
我在这里做错了什么?
最佳答案
我缺少 siteConfig 对象上的“appSettings”属性。添加该字段后,我的函数应用程序已创建并具有所有必要的配置字段。
const resourceGroupName = "my-resource-group";
const siteName = "my-new-function";
const serverFarmPath = "<id_from_portal>";
const serverFarmId = `/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Web/serverFarms/${serverFarmPath}`;
const createAppResult = await webSiteClient.webApps.beginCreateOrUpdateAndWait(
resourceGroupName,
siteName,
{
name: siteName,
kind: 'functionapp',
location: 'East US 2',
serverFarmId: serverFarmId,
clientAffinityEnabled: false,
siteConfig: {
appSettings: [
{
name: 'AzureWebJobsStorage',
value: '<connection_string>'
},
{
name: 'FUNCTIONS_EXTENSION_VERSION',
value: '~4'
},
{
name: "FUNCTIONS_WORKER_RUNTIME",
value: "node"
},
{
name: "WEBSITE_NODE_DEFAULT_VERSION",
value: "~14"
},
{
name: "APPINSIGHTS_INSTRUMENTATIONKEY",
value: '<key>'
},
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING',
value: '<connection_string>'
},
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING',
value: '<connection_string>'
},
{
name: 'MY_CUSTOM_ENV_VARIABLES',
value: 'some value'
}
]
},
reserved: true // The secret property - must be set to true to make it a Linux plan. Confirmed by the team who owns this API.
}
);
关于javascript - 如何使用 JavaScript SDK 创建 Azure Function App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72623501/