The core component of any serverless architecture is the Function-as-a-Service. The idea of any Function-as-a-Service is basically this: you write your code, send it to your cloud provider, and they handle every aspect of making that available to yourself and your customers. This means developers focus purely on the business logic, and not on unrelated technical things. This also means developers can deliver value much faster, and in a modern business, this is the single-most important advantage you can have.
1.x's underlying infrastructure is written in .NET and 2.x's is written in the newer, faster, and cross-platform .NET Core. Version 2 is now in General Availability, and therefore it is advised to only use it for new projects (unless you have some strong dependency on a .NET-only library). It would also be advisable to convert any existing functions to version 2 for the performance benefits alone, but it's also likely that version 1 will eventually be deprecated. If you are looking for support for other languages, Java is the most likely next language to be fully supported by Azure Functions and there are a large number of unsupported yet reasonably functional languages. Alternatively, AWS Lambda supports Java, Go, and Python, in addition to C# and Node.js.
In this exercise, you will be learning how to create a function, which is the most important component of any serverless architecture, in C#. We will be using Azure Functions to do this. We will also see how to run this function locally and in the cloud:
If you are getting asked what environment to attach to when you click the play button, you are likely to be in a folder too high. Make sure you have opened Visual Studio Code inside the ProductsApi folder.
Let's begin by first creating our Azure function. Create a new folder in your development area called ProductsApi. Open a command-line tool inside the folder and open Visual Studio Code by typing code . or clicking on the Visual Studio Code icon:
Click on the Azure logo, which will appear on the pane on the left if you've downloaded the Azure Functions extension. Then, click on the Sign in to Azure… button and authorize Visual Studio Code with your Azure subscription. Select your free subscription if asked:
Now that your environment is ready, click the folder icon with a small lightning bolt icon (see the previous screenshot) in the Azure Functions pane on the left to create a new Azure Functions project. A message will appear in the command palette at the top of the screen. Select your current folder:
After this, you will be prompted to select a language for your project. Select C# :
The Azure Functions extension will have scaffolded you a project with no real code—just a .csproj file to define the build process and host.json and local.settings.json files, as shown in the following screenshot:
The host.json file defines the characteristics of the underlying container/runtime, such as how long the function can live for, the version of the runtime (1 or 2), or whether you want sampling in your logging. The local.settings.json file stores application settings, similar to IIS Application Settings, for when you are running the function locally. This is for release variables, passwords, and so on.
Open the Azure Functions extension again and click the lightning bolt with the small plus sign on it to add a function (see Figure 1.2). Select HttpTrigger when prompted for a function template:
Name the function GetProducts, as follows:
When prompted to set the namespace, set it to ProductsApi, as follows:
You will then be prompted to set AccessRights. Set it to Function:
Once this is done, a function will be templated for you, as follows:
When prompted, click Restore on the popup in the bottom right that states that some packages are missing. If you're not prompted, open a terminal window by clicking on the Terminal −> new Terminal button at the top of Visual Studio Code and typing dotnet restore in the ProductsApi folder:
Click on the debug button (the bug with the cross through it, which is visible on the left pane of the window) and click on the green play button to start the debug session:
We have now started our Azure Function on our local machine, so let's test it. Visit the address shown in the terminal (by azure-functions-core-tools) in your browser, and try adding a query parameter for the name, like this: http://localhost:7071/api/GetUserData?name=Bob. The following screenshot shows the output obtained for this query:
Let's get our function onto the cloud, where it belongs. Open the Azure Functions tab again and click the up arrow to upload this function to Azure (see Figure 1.2). Create a new Function App with a globally unique name (hence it cannot be specified here):
The name that's used in this exercise is AdvancedServerlessProductsApi:
When prompted to select a resource group, click on Create new resource group, as shown in the following screenshot:
The resource group that was created in this exercise is named productapi-rg:
Next, click on Create new storage account when prompted:
Enter the name of your storage account (the one used in this exercise is productapi):
Next, choose your local Azure Region when prompted, as shown in the following screenshot:
This may take a little while. You will see progress windows in the bottom right corner:
Your Function App should now have appeared on the left, as shown in the following screenshot. If you expand the functions dropdown, you can find the function you have just deployed and right-click it to get the URL:
Try out your function by copying the URL into your browser:
Congratulations, you've created and run an Azure function locally and in the cloud. You should be able to see from this just how quick and easy it is to develop and deploy Azure functions. The function listens for a simple HTTP trigger event and returns a simple message. Anything that you can do in C# can be done using this function.