0

I am using Terraform with Azure to create a function app to demonstrate a webapp (Pi Calculator with nodejs).

When creating a Function App within the portal of Azure it works; using the settings like below.

Function App Azure Settings

Afterwards I am adding a new function with the "development environment" In-portal -> Webhook + API. And it works!

WorkingPiCalc

But when I am creating it with Terraform it doesn't work. I am using this code:

main.tf

    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} "
    };
};

package.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"
  }
}

This is the error message I get:

Error message

I have tried multiple ways with changing the async code, but still it's not working.

Anyone has an idea what the problem is and how I could possibly solve this? Many thanks in advance!

ineshi
  • 1
  • 1
  • try change to `module.exports = async (context, req) {` without `function`. – GNOKOHEAT Oct 17 '19 at 23:33
  • Thanks, I just tried that and now I receive an error pointing to the curly brackets. – ineshi Oct 18 '19 at 10:54
  • Do you have a function app you are trying to deploy or just JS files? I believe VS creates your host.json file that specifies what version of functions you should be using, maybe that is the issue a v1 vs v2 situation. – Jamie Oct 23 '19 at 17:45

0 Answers0