Exposing your Azure Functions on the internet with ngrok
As you have read in the previous paragraphs, you can use the Azure Functions runtime to host your functions, but the host process exposes your function on the localhost, so you cannot reach them from another PC than the one hosting them.
If you want to expose your functions on the internet and, therefore, solve the problem, you can use ngrok. ngrok is software (running on Windows, macOS, and Linux) that allows you to expose a local server to the internet using a secure tunnel, and it also works if your local server is behind a NAT or a firewall.
- To use ngrok, you need to register yourself on the ngrok website and create an account.
ngrok has a free plan that allows you to use the software for free with the limitation of only one process at a time with a maximum of four tunnels and a random port, but it is enough to test your functions.
- Once you download the ngrok client, you have to connect your account to the client (you need to do this operation once only) using the following command:
ngrok.exe authtoken <your_auth_token>
You can find your authentication token in the Auth section of your account dashboard:
- To expose your local function using ngrok, simply expose the local port using the http command:
C:>func host start --port 9999
C:>grok http 9999
The following screenshot shows the output of the preceding command:
The http command starts a tunnel from a random subdomain hosted by the ngrok platform (in the previous screenshot, a84984cb.eu.ngrok.io) to your PC. You can call your Azure Functions using the sub-domain instead, http://localhost; for example, if your function has the http://localhost:9999/api/books URL, you call it using the http://a84984cb.eu.ngrok.io/api/book URL.
The ngrok console shows the HTTP requests received through the tunnel, but ngrok also exposes a web GUI you can display using the http://localhost:4040 URL. The web console shows you all the requests received by the ngrok tunnel, as you can see in the following screenshot:
And it also shows you the metrics about your connection, as you can see in the following screenshot:
The web GUI allows you to inspect every single request received and gives you information about the tunnel and statistics about the requests (for example, connections and request duration).