3
votes

I am trying to use Azure Function to call Ethereum using Web3.js, code seems to be working fine in the command shell but I get an error when I run it as a function.

On Azure Portal create a new Javascript Azure Function:

  1. Go to Platform Features --> Development Tools --> Advanced Tools (kudu) --> Debug Console --> CMD

    cd site cd wwwroot cd npm install web3@^0.20.0

  2. Create a new code.js file

    const Web3 = require('web3'); var web3 = new Web3(); const httpProv = new Web3.providers.HttpProvider("http://:8545"); web3.setProvider(httpProv); console.log(web3.eth.blockNumber);

  3. Execute this file in CMD shell

    node code.js

  4. Works fine, I can see a HTTP Post request

    Request:

    POST / HTTP/1.1 User-Agent:node-XMLHttpRequest Accept:/ Content-Type: application/json Host: : Content-Length:63 Connection:close

    {"jsonrpc":"2.0", "id":1,"method":"eth_blockNumber","params":[]}

Response:

HTTP/1.1 200 OK
Content-Type: application/json
Vary: Origin
Date:
Content-Length:
Connection:close

{"jsonrpc":"2.0", "id":1,"result":"bla"}

I have created a package.json file inside wwwroot directory

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "web3": "^0.20.0"        
      }
    }
  }
}

and ran

npm install

When I execute the Azure Function I get this error:

2017-10-30T08:31:44.291 Function started (Id=f28edf73-fa02-406d-868e-0f23cd41e6e0)
2017-10-30T08:31:44.338 Exception while executing function: Functions.AZURE_FUNCTION. mscorlib: Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24.
2017-10-30T08:31:44.385 Function completed (Failure, Id=f28edf73-fa02-406d-868e-0f23cd41e6e0, Duration=94ms)
2017-10-30T08:31:44.385 Exception while executing function: Functions.AZURE_FUNCTION
2017-10-30T08:31:44.385 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44.385 Function completed (Failure, Id=f28edf73-fa02-406d-868e-0f23cd41e6e0, Duration=94ms)
2017-10-30T08:31:44.385 Executed 'Functions.AZURE_FUNCTION' (Failed, Id=f28edf73-fa02-406d-868e-0f23cd41e6e0)
2017-10-30T08:31:44.385 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44.385 Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is 'f28edf73-fa02-406d-868e-0f23cd41e6e0'
2017-10-30T08:31:44.385 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44.400 {"id":"4e755d0e-a2cf-4c4a-a734-aa750555a948","requestId":"4fa42aca-ff49-487a-a543-8f82295a1e5c","statusCode":500,"errorCode":0,"message":"Exception while executing function: Functions.AZURE_FUNCTION -> Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.\n    at Object.InvalidConnection (D:\\home\\site\\wwwroot\\AZURE_FUNCTION\\node_modules\\web3\\lib\\web3\\errors.js:31:16)\n    at HttpProvider.send (D:\\home\\site\\wwwroot\\AZURE_FUNCTION\\node_modules\\web3\\lib\\web3\\httpprovider.js:84:18)\n    at RequestManager.send (D:\\home\\site\\wwwroot\\AZURE_FUNCTION\\node_modules\\web3\\lib\\web3\\requestmanager.js:58:32)\n    at Eth.get [as blockNumber] (D:\\home\\site\\wwwroot\\FUNCTION_NAME\\node_modules\\web3\\lib\\web3\\property.js:107:62)\n    at module.exports (D:\\home\\site\\wwwroot\\FUNCTION_NAME\\index.js:8:25)\n    at D:\\Program Files (x86)\\SiteExtensions\\Functions\\1.0.11296\\bin\\azurefunctions\\functions.js:99:24"}
2017-10-30T08:31:44.400 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\FUNCTION_NAME\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44  PID[10332] Information Sending response: 200.0 OK
2017-10-30T08:31:44  PID[10332] Information Sending response: 200.0 OK
1
What's this - http://IP_ADDR:8545.? is that somewhere outside the function or is that localhost? Binding to arbitrary ports on localhost won't work in a function. - evilSnobu
I just masked the actual ip address from the error log with IP_ADDR, there is no issue with the port or the IP. Web3.providers.HttpProvider("IP_ADDR:8545"); - user8867729
If it works from Kudu it should work from the function as well. A bit strange. What do you get if you do curl -v http://IP_ADDR:8545 in the Kudu console? - evilSnobu
curl works fine, connected to IP port 8545, response HTTP/1.1 200 OK - user8867729
The stack trace points to a problem above the TCP layer, maybe in the HTTP protocol or even one layer up. How are you curling that URL, are you using a secret, an API key maybe? Could it be the Function doesn't read that right? You could point your Function to something like requestb.in and have a look at the full request as it comes down the wire. Careful with the secrets when you do that. - evilSnobu

1 Answers

0
votes

Below are my steps to workaround this issue.

  1. create an Azure Function App on App Service Plan (instead of Consumption plan)
  2. Enable websocket
  3. Switch runtime version to Beta

Note that Websock and Runtime version option available only on App Service plan.