Azure Functions — Creating Azure functions using Node.js

Amir Mustafa
6 min readNov 8, 2022

→ Microsoft Azure provides us running serverless functions using Azure Functions service.

→ This is also called Function App.

→ This is similar to the AWS Lamda service in Amazon Web Service Cloud.

→ We will split this article into two parts:

a. Using Visual Studio Code, create Node.js code and run the function.

b. Go to the Azure portal and explore this service there as well.

A. Creating Azure Function using Visual Studio Code:

→ Open your favourite code editor:

→ Click on Sign in to Azure

→ A browser tab will open and asks for Azure login credentials

→ After successful login this page will come and will ask to close this tab.

→ In the Azure section in VS Code now, now all resources will come.

→ Click on + sign in Workspace followed by clicking Create Function

→ A pop-up comes- We must create a project first. Click Create new project button.

→ Enter Directory name:

eg. Azure Functions

→ Enter the Project name. Click Create

→ Finally it asks for the technology eg. Javascript, Typescript, Java, Python etc

→ Azure functions can be triggered in various ways — here we will choose the HTTP trigger approach (i.e. calling Azure functions hitting from the browser).

→ Enter Function Name:

Eg. Azure-API

→ When we create an HTTP trigger, it usually can be — anonymous or Admin.

→ Here we will use Anonymous. In real-time normally admin credentials are provided to us by the company.

→ Click open in the current window:

→ Finally Azure API project is created.

→ Let us overview the files:

a. AzureAPI/function.json:

This file contains configuration related to this Azure function — AzureAPI.

Type of requests it has — GET, POST, etc

{  "bindings": [   {     "authLevel": "anonymous",     "type": "httpTrigger",     "direction": "in",     "name": "req",     "methods": [       "get",       "post"     ]     },    {      "type": "http",      "direction": "out",      "name": "res"    }  ]}

b. AzureAPI/index.js:

→ This is the main file where the business logic of the function is written.

c. package.json:

{  "name": "azure-api",  "version": "1.0.0",  "description": "",  "scripts": {  "start": "func start",  // FOR RUNNING FUNCTION  "test": "echo \"No tests yet...\""},"dependencies": {},"devDependencies": {}}

local.settings.json

Executing Azure Function:

STEP 1: First we install the below package:

npm i -g azure-functions-core-tools

STEP 2: Run below command:

func start

→ We observe a URL is created in the terminal.

→ Copy this URL and run it in the browser:

http://localhost:7071/api/Azure-API
http://localhost:7071/api/Azure-API?name=Amir 

→ Now we can write business logic based on the application requirement in the index.js file.

→ Hurray, finally our Azure function is successfully triggered 😀

B. Exploring the same in Azure Portal:

→ The same thing can be achieved from the Azure cloud portal as well:

STEP 1: Search Function App in Azure Cloud.

STEP 2: On this page we can configure our function name, technology say Node.js, 16LTS

STEP 3: Click Review + create

→ Review the function configuration and click Create button.

→ Step 4: Click on Go to resource button.

→ So same as previous we get the Azure function URL here.

STEP 5: We can configure the function from Function settings as we did locally.

npm install -g azure-functions-core-tools@4 --unsafe-perm true

Repository:

Check here for the reference code.

Closing Thoughts:

In this article, we have successfully implemented Azure Functions using Node.js. It becomes very powerful using serverless in various features like CRON job, and running REST environment.

If you are interested in the AWS Lambda function, you can click here.

Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.

--

--

Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/