我正在使用带有 Azure 的 Terraform 创建一个函数应用程序来演示一个 web 应用程序(带有 nodejs 的 Pi 计算器)。
在 Azure 门户中创建 Function App 时,它可以工作;使用如下设置。
之后,我将使用“开发环境”门户内 -> Webhook + API 添加一个新功能。它有效!
但是当我用 Terraform 创建它时它不起作用。我正在使用这段代码:
主文件
version = "=1.34.0"
}
resource "azurerm_resource_group" "rg" {
name = "rg"
location = "westeurope"
}
# Create a storage resource
resource "azurerm_storage_account" "picalcstorage" {
name = "picalcstorage"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}
# Choose a plan
resource "azurerm_app_service_plan" "picalcplan" {
name = "picalcplan"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
kind = "FunctionApp"
sku {
tier = "Dynamic"
size = "Y1"
}
}
resource "azurerm_storage_container" "storagecontainer" {
name = "storagecontainer"
resource_group_name = "${azurerm_resource_group.rg.name}"
storage_account_name = "${azurerm_storage_account.picalcstorage.name}"
container_access_type = "private"
}
resource "azurerm_function_app" "picalcfunction" {
name = "picalcfunction"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
app_service_plan_id = "${azurerm_app_service_plan.picalcplan.id}"
storage_connection_string = "${azurerm_storage_account.picalcstorage.primary_connection_string}"
app_settings = {
FUNCTIONS_WORKER_RUNTIME = "node"
}
}
index.js
// node.js application to calculate pi as a Azure Function App
// P.Burghouwt HHS 2019
// <URL>?n=<number of decimals> (default=20 if no n specified)
// returns pi and the calculation time as json data
// calculation derived from https://github.com/MikeMcl/decimal.js/issues/9 (Bailey-Borwein-Plouffe)
const Decimal = require("decimal.js");
Decimal.config({precision: 10});
function pi(n) {
var p16 = new Decimal(1);
var pi = new Decimal(0);
var precision=n;
Decimal.precision=precision;
var one = new Decimal(1);
var two = new Decimal(2);
var four = new Decimal(4);
var k8 = new Decimal(0);
for (var k = new Decimal(0); k.lte(precision); k = k.plus(one)) {
var f = four.div(k8.plus(1))
.minus(two.div(k8.plus(4)))
.minus(one.div(k8.plus(5)))
.minus(one.div(k8.plus(6)));
pi = pi.plus(p16.times(f));
p16 = p16.div(16);
k8 = k8.plus(8);
}
return pi;
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.n || (req.body && req.body.n)) {
var n=(req.query.n || req.body.n)
}
else {
var n=20
};
var StartTime = new Date()
var calculatePi = pi(n)
var EndTime = new Date() - StartTime
context.res = {
status: 200,
body: "{\"PI\":\n {\n \"decimals\": " + (n) + ",\n \"ms\": " + JSON.stringify(EndTime) + ",\n \"value\": " + (calculatePi.toString()) + "\n }\n} "
};
};
包.json
{
"name": "picalc",
"version": "1.0.0",
"description": "Pi Calculation for node.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Pieter Burghouwt",
"license": "MIT",
"dependencies": {
"decimal.js": "^10.2.0"
}
}
这是我收到的错误消息:
我尝试了多种方法来更改异步代码,但仍然无法正常工作。
任何人都知道问题是什么以及我如何解决这个问题?提前谢谢了!