Creating an Azure Function
- Estimated read time: 10 min read
- Written by Chad Campbell on Jul 28th 2017
Hey there, I'm Chad Campbell from Ecofic. If you're new to this channel, please consider subscribing now. The purpose of this channel is to help you create better software and get that software to more people. One way to help you meet this goal is with Azure Functions. In this video, I want to show you how to create an Azure Function.
An Azure Function is some code hosted within an Azure Functions, plural, app. Azure Functions is a way to run your code in a serverless environment. This is a super handy way to tackle one-off tasks like image processing, data crunching, and scheduled tasks.
Creating the Azure Function App
To show you how to create an Azure Function, let's begin by creating an Azure Functions app. To do that, I'm going to visit the Azure Portal. Once there, I'm going to click the new button. This will bring up the blade that has all of the services available on Azure. To quickly find the Azure Functions service, I'm going to begin typing the word "Function" in the search field. You can see that a suggestion for "Function App" appears. I'm going to select this suggestion to bring up the Function App blade.
The Function App blade provides a brief description of what an Azure Functions app is. It provides some useful links if you want to learn more before creating the Azure Function App. Still, if you know that you want an Azure Function, you can go ahead and click the "Create" button, like this.
After you click the "Create" button, the Function App create blade will appear. This blade will prompt you for the initial details of your Function App. These details are covered by seven fields that must be set. Those fields are "App name", "Subscription", "Resource Group", "Hosting Plan", "Location", "Storage", and "Application Insights".
The "App name" field will serve as the main endpoint you'll use when you're using your Azure Functions. When choosing a name, make sure to choose one that reflects the purpose of the functions you'll be creating. For example, if you were creating some Azure Functions that were applying filters to a picture, you may want to use a name like "picture-filters". Or, if the Azure Functions represent those of a specific domain, like your company, you may want to use that. For example, I'm going to use the name "my-company-functions". While the name is a bit generic, it drives home the idea of domain-specific functions. Still, once you've chosen a valid name, you may move onto picking the subscription.
The subscription field determines where the Azure Functions App costs will get billed. To get an understanding of the costs associated with an Azure Functions App, I recommend you visit the Azure Functions Pricing page, here.
The next field you need to set is the "Resource Group" field. The resource group field is a way to govern billings and permissions for services in Azure. They also provide a way to monitor usage for a specific app or service. In other words, the Azure Functions App may be part of a larger app you're working on. For example, while you're company may have a "picture-filters" Azure Function App, the Azure Function App may be part of a larger initiative like a "Photo Library". That initiative may include something like an Azure Search service to help you search through the photos. Which, if you're interested, I've created an Azure Search course which you can learn about here. Still, the Azure Search service and the Azure Functions App may both be part of the same "Resource Group" called "Photo Library". This resource group helps you see usage across the entire photo library initiative.
Next up is the hosting plan. The hosting plan determines how resources are designated to your function app. There are two plans available at the time of this recording. The plans are the "consumption plan" and the "app service plan". When the default "consumption plan" option is used, resources are allocated on-the-fly as needed by your functions. The "app service plan" option is useful when you want to piggy-back onto an existing app service. For example, if you have an existing web app hosted on Azure, you may want your Azure Functions App to tie into those already allocated services. The full details of the hosting plans are beyond the scope of this video. To learn more, you may want to visit the details in the docs here. I'm going to choose the default "Consumption Plan" approach. Once selected, I can move on to the location field.
The "Location" field determines where the Function app will be hosted. In general, you should choose a location close to where your Azure Function app will be accessed from. Or, where other functions will access it from. I'm going to choose the "East US" location before moving on to the "Storage" field.
The "Storage" field is the name of the storage account that will be used by your Function App. In general this is where your function definitions will get stored. You can either create a new storage account or use an existing one. I'm just going to use the storage account that was automatically generated for me.
Finally, is the "Application Insights" field. By default, this field is turned off. However, if you turn it on, you'll get analytics into your Azure Function usage. You'll also gain access to a live stream that will let you see the app usage in near real-time. Since this integration is beyond the scope of this video, I'm going to leave the setting turned to "off". Then, I'll click the "Create" button to initialize the my-company-functions app.
Azure will validate the new service, and then it will actually create and deploy the Function App. As it's deploying, the Azure Portal's wait indicator will spin. Once deployed, you'll receive a success message that looks something like this. If you select the "notifications" icon, you'll see a few more details about the creation of the Azure Function App. With the app itself created, you're ready to navigate to it.
Navigating to the Azure Function App
To navigate to the Azure Function App, I'm going to visit the service menu. Towards the bottom is a "More services" option. When clicked, I'm given a list of all of the services hosted in Azure, grouped by category. While I could navigate down to the "Other" category, it's all the way down at the bottom of the list. For that reason, I'm just going to begin typing the word "function" in the service search field. Once the Function Apps option appears, I'm going to select the star next to it to mark this service as a favorite. This will make it easier for me to navigate to later.
Next, I'll select the Function Apps service itself. Once clicked, my list of available Function Apps will begin loading. Once loaded, I can select the Function App that I want to create functions in. For the sake of this video, I'm going to select the "my-company-functions" app that was created earlier.
Creating the Azure Function
To create the Azure Function, I'm going to expand the the "my-company-functions" branch of the Function Apps tree. Once expanded, you can see that you're presented with three new branches: Functions, Proxies, and Slots. Proxies and Slots are beyond the scope of this video. However, since this video is about creating Azure functions, the Functions part is of interest.
You can see that the Functions branch is already expanded and there's nothing in it. This is because we haven't actually created a Azure Function yet. We've only created the Azure Function App. To create an Azure Function, I'm going to click the blue "New" button. Since this is the first function in the app, we're presented with a "quick start" screen. The quick start screen asks you two questions.
The first question prompts you for the scenario you'll be using the function for. There are three scenarios presented. The first scenario is to use an Azure Function as a Webhook or an API. These are a great way to help you integrate with third-party systems. The second scenario is to use an Azure Function as a timer. This provides a way to run scheduled tasks. It's like having Cron jobs in the cloud! Finally, there's a quick start for data processing functions. For the sake of this video, I'm just going to choose the first scenario.
Writing and Testing the Azure Function
By default, Azure provides a template for a function written for Node.js. While I could begin writing some code, I want to show this function running before I do anything. So, I'll begin by expanding the utilities pane. This pane gives me a way to navigate the files in this Azure Function app kind of like the Explorer in Visual Studio Code. In addition, this pane has a "test" tab.
The test tab lets you run tests against your Azure Function from within the Azure portal. Since this function represents a Webhook or API, this tab lets me setup an HTTP request against the Azure Function. I'm not going to change anything. I'm simply going to click the "Run" button to test my function.
When the "Run" button is clicked, you'll see the "Logs" appear from the bottom of the screen. This provides diagnostic details about the function. If you look back to the "Test" tab in the utilities pane, you'll see the response of the Azure Function written in the "Output" field.
The "Output" field shows a greeting for "Azure". While cute, my name isn't "Azure", it's "Chad". I could do a black-box test of the Azure Function by passing my name as part of the request. I'm going to simply change the "name" property value from "Azure" to "Chad".
Now, when I click the "Run" button, you'll see that my name appears instead of Azure.
That's how you can test an Azure Function. To actually write the code itself, I prefer to do so locally. For that, I'll setup continuous deployments of Azure Functions. That will be covered in another video on this channel. For that reason, if you haven't subscribed, I encourage you to do so.
I hope this video helps you create some better software with Azure Functions. If you'd like to learn other ways to create better software, please check out some of the training courses I've created by visiting here. Finally, if this video was helpful, please click the "thumbs up" below and share this video with others. This let's me know that I'm adding value to your day. For future videos, don't forget to subscribe!
Thanks for watching and have a great day.