Monday, 10 February 2020

Azure Function Proxy


Proxies are simple yet powerful feature of Azure functions. it provides a way to intercept incoming HTTP traffic to an Azure function and control its flow based on an application’s need.

Pros - Proxies are helpful in many different ways,
  • Call intercept – Enable intercepting incoming HTTP traffic to an azure function.
  • Call routing – Route incoming HTTP calls to a different function either within same function app or to a completely different function app.
  • Header manipulation – Manipulate HTTP header of an incoming request before routing to a different function. Similarly an azure function’s response can also be manipulated before reverting back to client.
  • CORS - Its mandatory to enable CORS for function app to enable communication with sample todo app. Proxy removes this dependency completely as the call always considered to be within same domain.
Demo - Enough details let’s see it in action. I have created a Todo application with simple CRUD (add, read and delete only) operations. The source is available at 
https://github.com/SonuK21/AzFunctionProxyThe repository contains two folder,
  1. TodoApi – An azure function with supported functionality.
  2. TodoApp – A simple angular app to consume the api.

Publish Todo API  Lets publish the API by following below steps. I am using visual studio 2019.
  • Open solution in visual studio and build
  • Once the project is build then right click on project file and hit publish,
  • Create a new publish profile by providing all the necessary details like
    • Name - Any unique name for your function app.
    • Subscription - Your azure subscription to be used to publish the app.
    • Resource group - A logical container for all resources related to the function app.
    • Location - Any closest azure data center's location to host the app.
    • Azure storage - A storage that will be used by azure to store your app files.
    • Below screens show a step by step guide to create profile.
  • Your API is ready to serve, take the highlighted app URL from previous screen. If you try to request API for a list of todos then it should return an empty as there is no record as of now.

Publish Todo Sample App - Lets publish the sample app so that we can integrate the todo API. I had already deployed the sample app in azure storage as I found it quickest way to host a static app on Azure. Please refer https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website for more details on hosting static websites in Azure storage. Once you are done then your sample app should be up and running on a different URL in azure like this,



Proxy - Now, we have two different service running in azure and each can be reached by their respective URLs only. Here comes the use case where proxy can be helpful. Lets try to understand the workflow and see that, how proxies could be helpful,
  • Configure proxy in our function app.
  • Intercept the incoming traffic targeting function app's root i.e. https://yourfunctionappname.azurewebsites.net/ and route this traffic to our sample app so that a UI can be presented.
  • Remove CORS dependency
  • User should be able to perform operations like add/read/delete todos directly by hitting the function app's URL
Configuring proxy - One easiest way is to configure proxies from azure portal directly. But if you want to understand each bit of it then proxies.json is the one and lets just try it,
  • Add a proxies.json file in your azure function project.
  • Right click on file and update property copy to output directory and set to copy if newer.
  • We have added two proxies (You can add more depending on requirements),
    • proxyHome - It will intercept all incoming calls to function app default URL i.e. https://yourfunctionappname.azurewebsites.net/ and redirect them to todo sample add index page.
    • proxyContent - Similarly this will route all incoming calls to other content like CSS, JavaScript etc to /content directory within todo sample app.
    • Few points to notice,
      • methods - An array of allowed HTTP verbs/methods supported by this proxy
      • route - The URL route against which this proxy needs to be triggered. i.e. any incoming call to aforesaid URL will automatically be served by respective proxy.
      • backendUrl - Any endpoint(Our sample app URL in this case) which will actually serve these requests.
  • Its done. If you try to hit function app's URL now then it should open the sample todo app instead just like the screen below,
  • Notice the URL, its function app (API) not the sample todo page.

Thanks for reading.

Saturday, 1 February 2020

Azure function bindings


Bindings provide a way to read/write data from/to different data services without any additional overhead of managing connection with these services. Today, we will see that how easy it is to integrate Azure functions with different services like blob, table storage, queue, cosmos, send grid etc with help of bindings.

Type of bindings –
  1. Input binding – As the name implies, the binding is to get data into an Azure function from data service like blob, table storage, cosmos etc.
  2. Output binding – The binding is to push/add data to different data service from an Azure function like blob, queue, send grid etc.

Demo – We will create a Todo API with following,
  • An azure function with Add/List Api,
  • Output binding - Data will be stored in a Table Storage.
  • Output binding – We will write the data in a text file and store it in a blob storage.
  • Input binding – Read data from table storage to display on UI.

·         I will be using a simple angular page to add/list todos but it could be a simple html page with some ajax or use any client like postman to hit the API directly.

I will be using Visual Studio but it is absolutely fine using any IDE like VS Code if want to follow along or refer my previous post getting started with azure functions

Creating API - Lets create the API first by following the steps below and then will move on to UI.
  1. Setup new azure function project with an empty template as shown below,
  2. Your solution should look like this,
  3. Add new azure function by following the steps below,
  4. Add required nuget package by running following command in package manager console,
    • Install-Package Microsoft.Azure.WebJobs.Extensions.Storage
  5. Add new class Models.cs and add following,
    • Todo POCO
    • TodoEntity to be used while working with storage table
  6. Add two endpoints CreateTodo/Todos in API to create and read data.
    • Include all necessary namespaces
    • CreateTodo endpoint to create a new todo item
    • Todos endpoint to get all todos for be displayed
  7. Now hit the API by using an angular page or java script then It should give you an error like below,
  8. And this is because we are trying to access an API of different domain from our client App. Now allow cross domain communication by enabling CORS under host in local.settings.json like this,
  9. You should now have a working azure function with added input/output bindings.

Monday, 27 January 2020

Debug Azure Function in Visual Studio Code


Prerequisite -

Visual Studio Code (VS Code) is a code editor by Microsoft. Its an open source tool available for cross platform development so that an individual can get same experience across OS like Windows, Mac OS and Linux. 

Today we will try to debug an Azure Function (Java Script) in VS Code.

Step 1:- Use below command to initialize a new Function App

func init

Select worker runtime to node and language to javascript

Step 2:- Add new function by using command,

                func  new

Select template to Http trigger and leave function name as default. Hit enter

Step 3:- Run your function app using below command,

                func start

Note – Try it by openning the URL in any browser.

Step 4:- Use below command to open your app in VS Code.

                code .

Step 5:- Follow steps shown below to configuration VS Code to enable debugging.
  1. Hit Debug and Run
  2. Find DEBUG AND RUN -> Add Configuration
  3. launch.json file should be opened in editor.
  4. Select Node.js: Attach to Process
  5. Save changes
  6. Follow step 3 to start the function app
  7. Attached process as shown below,
  8. Add break point in index.js
  9. Try the function from any browser.
  10. The debugger should hit like,

Sunday, 26 January 2020

Getting Started with Azure Functions


Azure functions are Function as a Service (FAAS) offering from Microsoft Azure. It is a serverless compute service from the provider where user need not to worry about underlying deployment infrastructure because Microsoft as a cloud provider manages it for us.

Azure functions are good choice to perform independent stateless task. It has the ability to scale automatically based on traffic/demand. The documentation covers a lot, so we will not go into more details instead lets try to create a simple function.

Triggers – An azure function needs a trigger to perform its task. There are no of inbuilt triggers supported by Azure functions like HTTP Trigger, Queue Trigger, Blog Trigger etc.

Note – An azure function must have one and only one trigger associated to it.

Bindings – Azure function framework provides a way to read data into a function and write data from function to any data service. There are two major types of bindings,
  1. Input binding – Read data into an Azure function from any data service.
  2. Output binding – Write data to any data service from an Azure function.

Function.json – A file containing configuration details like trigger, bindings with respect to an individual Azure function. Every azure function has a corresponding configuration file that means every function app will have exactly one function.json with respect to each function.

There are multiple ways to create function apps like,
·         Azure Portal
·         Visual Studio
·         CLI
·         VS Code
We will look at the CLI and see that how easy it is to create a function app using Azure Function Core Tools.

Prerequisite:-
Step 1:- After installing the Core tools as mentioned above lets first verify it. Open a new instance of any command line tool (I am using power-shell but its completely your choice) and run below command,

                func

You should be able to see details like, Core Tool’s version, Contexts, Actions etc as shown below,

Step 2:- Create a new function app. Function app is a unit of deployment or a group of logically related functions, shares common configuration and scale together. Use below command to initialize a new Function App.

func init

There are multiple options worker runtime to select from but lets start with node and JavaScript as language.

Step 3:- Add new function by using command,

                func  new

Lets start with Http trigger template and you can also update function name to one you prefer, but I am leaving it to default. Hit enter,

Step 4:- Run your function app using below command,

                func start



Step 5:- Try hitting the URL from any browser. Please note that the default function generated by CLI expects an input(name) either in query string or request body.