Create A Severless App Using Azure Functions

Introduction

 
The objective of this tutorial is to create a “Functions App” and then design “HTTP triggered function” in that app using “FaaS – Function as a Service”.
 
Let me introduce you to some basic concepts first.
 

Severless Computing

 
It is an execution model where the cloud provider (Azure, AWS, or Google) is responsible for executing a piece of code by the dynamic allocation of resources and charging only for the amount of resources used to perform this task.
 

Function Apps

 
It is basically a container that actually hosts the execution of multiple functions. This helps to logically group the functions in a unit for deployment, scaling, and sharing for the resource.
 
Create A Sever Less App Using Azure Function 
 

Road Map

  1. Basic Concepts.
  2. Ways to create a Function App
  3. Creating a function app using Azure Portal
  4. Creating a Function.
  5. Select the trigger type.
  6. Define what the function will do.
  7. Taking it for a spin.

Basic Concepts

 
Let’s get to know about the things which we will use in this tutorial.
  1. Event
    It is an action that occurs as a result of the activities done by some user or any other source.

  2. Trigger
    It defines how a function can be revoked and a function must have exactly one trigger.

  3. Function
    It is a server-less computation service that enables us to run code on-demand without explicitly managing the underlying infrastructure.

Ways to create Function Apps

 
There are actually four methods by which we can create a Functions App.
  • Azure Portal
  • Microsoft Visual Studio.
  • Visual Studio Code
  • PowerShell/Bash
In this tutorial, we’ll use the 1st method.
 

Creating a function app using Azure Portal

  • Select "Create a resource" >> Compute >> Function App.

    Create A Sever Less App Using Azure Function
  • Fill in the choices with the following information.
    1. Enter an app name: App name must be globally unique as it will serve as part of the base URL.
    2. Select a subscription: pick out one of the ones you have.
    3. Select a resource group: you can choose an existing resource group or create a new one.
    4. Select an OS: choices here are Windows or Linux; we opt for Windows.
    5. Select geography: well, select the region closest to you
    6. Runtimestack: this is the language you are going to be coding, so we select Node.js.
    7. Create a new storage account: let’s take an existing or create a new one.
Create A Sever Less App Using Azure Function 
 
Now, hit the "Create" button.
  • Select "Go to resource".

    Create A Sever Less App Using Azure Function
  • Now, click on the URL to verify that it has a public URL.

    Create A Sever Less App Using Azure Function
You will see an HTML page on that URL.
 
Create A Sever Less App Using Azure Function 
 

Creating a Function

  • Now, create a function for your Function App. Click on the ‘+ New function’.

    Create A Sever Less App Using Azure Function
  • Select the In-portal (you will write code in the Portal).

    Create A Sever Less App Using Azure Function

Select trigger type

Select trigger type, select “Webhook + API” (the function will run as soon as a certain URL is hit).
 
Create A Sever Less App Using Azure Function 
 
Now, click the “Create" button. 
  • Our coding environment looks like this.

    Create A Sever Less App Using Azure Function

Define what the function will do

 
Add the following code to the index.js file.
  1. //Text to display  
  2. var dadJokes = [   
  3.  "Did you hear about the restaurant on the moon? Great food, no atmosphere.",   
  4.  "What do you call a fake noodle? An Impasta.",   
  5.  "How many apples grow on a tree? All of them.",   
  6.  "Want to hear a joke about paper? Nevermind it's tearable.",   
  7.  "I just watched a program about beavers. It was the best dam program I've ever seen.",   
  8.  "Why did the coffee file a police report? It got mugged.",   
  9.  "How does a penguin build its house? Igloos it together."   
  10. ];  
  11.   
  12. // Image URLs  
  13. var punDogs = [   
  14.     "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQwYzSHjQje6Dr_XFDOlGv4ug2xrCJHqRkpgvyWZx-HOjL2a1kX",  
  15.     "https://cacm.acm.org/system/assets/0001/4295/123013_madlyjuicycom_laughs.large.jpg?1476779462&1388424919",  
  16.     "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSdq8KeoRbJAxaGER4LXGSAb8OE6IlM9WpovVliz_uiyNwQ8woC",  
  17.     "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSNsf6nFv99epdsEdEh-Nj_irTb75QYNJCmBnLUteagnONcT320",  
  18.     "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBDZs1Jn2SEE8kioGYhFdvnV3Vv-3qhbf5P6qpL0USC8RwDVdR",  
  19.     "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQRg6oMicA9_df9RqJlmqL6tsmRAX-a2C2IadPYrnaFF-w2uyQq"  
  20.     ]  
  21. //random number generator  
  22. var newNumber = Math.floor(Math.random() * dadJokes.length);   
  23. var dogImageUrl = Math.floor(Math.random() * punDogs.length);  
And, answer with an HTML response.
  1. context.res = {   
  2.  status: 200,   
  3.  headers: {   
  4.  "Content-Type""text/html"   
  5.  },   
  6.  body: '<h3>'+dadJokes[newNumber]+'</h3>' + '<br><img src="'+ punDogs[dogImageUrl] +'"/>' };  
Now, click "Integrate" and select the authorization level to “anonymous” because we don’t want any restriction.
 
Create A Sever Less App Using Azure Function 
 

Taking it for a spin

Now click on the “HttpTrigger1” and select “GetfunctionalURL”.
 
Create A Sever Less App Using Azure Function 
 
Click "Copy" to copy the URL and paste it into the browser.
 
Create A Sever Less App Using Azure Function 

Now, you will see the results like this.
 
Create A Sever Less App Using Azure Function
 
Create A Sever Less App Using Azure Function
 
Create A Sever Less App Using Azure Function 
 

Use cases

  1. With Azure Functions, we can write function definition in a variety of languages - C#, F#, JavaScript, Bash, PowerShell, etc.
  2. The amount of code you write in Azure function will be less.
  3. Azure Functions only charge you for the resources you actively consume.
  4. Using Function as a Service (FaaS), you only care about the code, not the underlying infrastructure and platform.
  5. Helpful only when you need to perform work in response to events.
  6. It is highly scalable when traffic to the specific URL increases.
  7. Can be used in the development of backend of the website.
  8. There is no difference between compiled and interpreted languages.


Similar Articles