1. Home
  2. Training Library
  3. Microsoft Azure
  4. Courses
  5. Introduction to Azure Functions

DEMO: Creating a New Function

The course is part of these learning paths

play-arrow
Start course
Overview
DifficultyIntermediate
Duration46m
Students499
Ratings
4.3/5
starstarstarstarstar-half

Description

This course is an introduction to Azure Functions. It explains how Azure Functions are little bits of your application logic that live in the cloud. The course includes how to activate—or what we call trigger—your Azure Functions, how to pass data to and from them, and also how to tie different Azure Functions together using an extension of Azure Functions called Durable Functions. 

This course provides hands-on demonstrations of how to create different kinds of Azure Functions, how to create bindings to other Azure Services from those functions, and how to create a Durable Function to manage state from one Azure Function to the next.

If you have any feedback related to this course, please contact us at support@cloudacademy.com.

Learning Objectives

  • Create Azure Functions with different types of Triggers
  • Implement input and output bindings to different types of data resources
  • Create Durable Functions to orchestrate related Azure Functions
  • Log the results with Dependency Injection

Intended Audience

Software developers who want to learn how to implement Azure Functions as a part of their cloud software design.

Prerequisites

To get the most out of this course, you should have some experience will the following:

  • Event-driven programming
  • Servers and APIs
  • Coding with C# and JSON
  • Project creation in Visual Studio

Resources

The GitHub repository for this course can be found here.

Transcript

To begin, we'll be creating the Azure Functions equivalent of Hello World. Although, in this case, we'll actually be logging to our console. So, just use your create a new project template in the typical way to create an Azure Function project. If you don't see it immediately in your list of projects, you can always use the filter control at the top by Azure, and you'll find it quickly.

We're gonna go ahead and name our function according to the primary type of trigger that we intend to use, which in this case for simplicity's sake, we'll just go ahead and make a timer. So, we'll call our project, AzureFunctionTimer. And although we will be using a couple of other types of timers throughout the project, including an HTTP trigger, which is the kind that you would want to use to access your function as an API endpoint.

As we discussed earlier, we'll also be touching on Queue trigger, but, as I mentioned for the moment for simplicity's sake, we'll start with a Timer trigger that runs on a simple interval. The interval that we'll specify here will be the default value of five minutes, and we're also going to want to be engaging our default development storage emulator provided by Visual Studio and Azure, which is the AzureWebJobsStorage account. That's going to allow us to interact with our local Blob storage, Queue storage, anything that we might need as Azure storage without having to provision any live resources into the cloud, which would incur charges and require a live Azure account.

So, for development work, we can use the free local storage emulator provided by Visual Studio. We'll go ahead and create that function, and let Visual Studio do its magic for us. And when it's finished doing so, you can see that we have a base function signature for an Azure function, and that it includes the necessary trigger attribute applied to the first argument, which in this case is the Timer trigger with the interval specified in part of its constructor argument.

That attribute applies to the TimerInfo variable, myTimer. And, in this function, all we're going to be doing is logging information that lets us know, yes, that function ran. To do so, we're going to use the dependency injection ILogger variable log. If you're not familiar with dependency injection, don't worry about it for right now. It just means that Visual Studio and Azure have some built-in functionality that you can access by using the right types of arguments in your function's signature, things like logging and configuration.

So, in this particular case, we're gonna go ahead and use logging, and then we're just gonna go ahead and run that function and see the sort of output that we get. And you'll see the first thing that happens when you run locally is that Azure Functions will pop up your console, so that you can see the logging activity that's happening inside of your function.

Here, you can see it picking up some configuration information, including the Cron schedule here that indicates that this function will be running every five minutes at five, 10, 15, 20, 25 minutes past the hour, etc. And so, as soon as we hit one of those specific timer intervals, our Timer trigger will fire, and there it is. As we see, we have successfully executed Function1. Because the timer fired, we have logged our information to the console, and we have confirmed that our function has executed successfully from end-to-end. So, congratulations, you have created your first Azure function.

Now, if you'd like to see the function.json file that goes along with that Azure function and defines all of the configuration inside the function, you can drill down by doing Show All Files inside of your Visual Studio. Then, you're gonna want to go into the bin directory, into your compilation, down inside of Function1, and there you will see your function.json file with the specified trigger binding automatically injected into it. So, your function.json file will hold all of your bindings, all the bindings that are created throughout the rest of the project, triggers, or input or output bindings, whatever they are, but they will all go into your function.json file, which was automatically created when you built the project and will automatically be deployed with your project when you deploy it into the cloud.

About the Author

David Gaynes is a software and cloud architect for .NET, NodeJS, Azure and AWS. He has been developing .NET and Cloud software for more than 20 years, with some of that time spent at Microsoft, and has authored both technical and non-technical teaching materials as books and in other formats. He enjoys physics, meditation, and experiencing the natural wonders of Hawaii.