Practice Exams:

AZ-303 Microsoft Azure Architect Technologies – Implement Solutions for Apps (10-15%) Part 9

  1. Azure Functions via Portal Walkthrough

Let’s go ahead and create a function. There’s actually two ways we can create functions. The simplest way is we can actually create them and even code them through the portal itself. And the second way is we can use an editor such as Visual Studio. We’ll go off the first option first and we’ll create a simple function go to to create a resource and we often have it as a quick start. Here a function app. But again, if it doesn’t appear, you can always search the marketplace for function apps. As always, we need to give it some basic information. I’m going to create a new resource group. I’m going to give it a name, I’m going to tell it to publish it as code. As we can see, we could actually run this in a docker container. I’m going to select Net Core as the runtime stack and I’m going to set my region to my local region. Next, we need to give it a storage account. As we said, all apps must use a storage account for various things. And I’m going to use the existing storage account that we created earlier.

The operating system is Windows and the plan type is Consumption. As we mentioned in the overview, we can always also have premium or app service plans. But I’m going to go for consumption because that will give us the cheapest way of running this. And in fact, when we’re not running it, or if we’re only running it a few times, it won’t actually cost us anything. Again, we can enable application insights, but again, I’m going to turn this off just for this demonstration. And again, I’m not going to bother adding any tags and we’ll just go ahead and click Create. Once that’s finished, let’s go up to the resource and we can see our functions here. At the moment we just have a function app. We don’t actually have any functions in it. So now we’ve got our function app, we need to go in and click New Function. We’ve got various options for creating our function.

As I said earlier, we can use Visual Studio or other editors. But I’m going to go for the importal option. First of all, it’s going to ask us for a basic template. The most common ones are APIs and timers. There are more templates here, but I’m going to go for a simple web hook first and this creates us our simple function. If we have a look at the code here, this is our input. Because this was a Http trigger, we’re going to be expecting a Http request to come in. We always get an Ilogger that helps us output logs to our logs console here, which helps for troubleshooting. And then in here we’ve got some basic examples. So what we’re going to do, first of all, we’re going to log some information. We’re going to get our Http request and look for a query string in there. Called name and we’re going to start that as a string.

We’re going to read that string and then finally we’re going to convert that because all this is in the request body. So what we want to do is get the name out of it. Finally we’re saying if the name isn’t null, we’re going to return an OK result with hello and the name. Otherwise we are going to return a bad request. What we can do to test this is if up here we click run, it brings up this test window here on the side. By default it comes up name Azure. And if we just scroll down, you can see the output here that in the login console, see the logs that have been spat out. If we scroll across here, we see the output. So we’ve seen Hello Azure. So what we could do is go in here now and change that, rerun the function. And now we see in the output it’s updated the output there.

Of course you wouldn’t normally run it in here. So this would be a Web API trigger. So if we click this get function URL here, we can copy the URL, paste that into a new tab. Then on the end I’m going to put and name equals grit again and press Enter. And so then again we get the output written. So that’s a very simple Http request. The other thing we want to do is maybe output its data. So this Http request is one binding. What we want to do now is add another binding. In this case we want to bind it to a storage account. So I’m going to go over here to integrate and this shows us our current triggers, inputs and outputs. So again, we’ve got a Http trigger. We’ve actually got an output of Http because that’s what writes to the screen. What I want to do is add a new output and we get a number of different bindings we can choose.

Here. I’m going to use Azure table storage. We give the parameter name. This is the name that we will refer to this binding within code. And then the table name is the actual table name this is going to create. So I’m going to create a table called Azure Portal function name log. Down here we tell it what storage connection we’re going to use. So we’re going to say azure Web Storage Connection. So that’s actually going to store that in a variable in a file called JSON. If you want to see what that actual string looks like, we can click Show value and it’s showing us here what that is going to be. And it’s going to basically go to our storage account that we created when we selected it. Once we have that we can click Save and now we can go back to our Http trigger and now we can update our code to start using this storage. Now the first thing we need to do is we need to bring in this here, this imports the namespace. So I need to tell because we’re using a storage account, we need to tell it to use the Microsoft Windows Azure Storage. And then we need to actually use issuer A using command.

So again using Microsoft Windows Azure Storage. And in specifically, we’re using Table Storage. The next we need to create a reference to the binding that we created on the integration stream. So the binding is actually going to be an eye collector type. I’m going to tell it, it’s going to use a class of name log and it was called Output table. Now we don’t actually have this class yet, so the next thing we need to do is create that class. So we’re going to create a class called Name log. And because this is going to be stored in Table Storage, we need to tell it that it’s going to inherit from a class called Table Entity. And this defines some basic fields that Table Storage needs to work. All I’m actually going to create in here is a string called name. We’ll save that. Next thing we want to do is in here we’re going to make it write our name to that table storage output table.

So first I want to make sure name isn’t null. And if it isn’t, we’re going to say new name log, enter equals new name log. We’re just going to create an object using the information from our class. So it was name equals name. So that’s going to store the name variable into that table. We next need to set something called the partition key and a row key. For the role key, I’m going to use a Guid string and again I’m going to save that. And what I’m going to do is just check down here in the logs and this will tell us if it’s compiled successfully. So if we look down the bottom there we go. Compilation succeeded. So we know that that’s gone. Okay, so we’ve forgotten something here.

So we’ve twisted our log entry. But next we need to actually add it to our table. So output table, add log entry, save that. Let’s run that again. It’s going to the portal screen here. I’m going to go into my storage account. I’m going to go to the Storage Explorer. So we’ve got our queues, check our storage and now we have our table that’s appeared with our record in there that we’ve just imported. And then let’s go back to here again where we were actually running it through the browser and hit Enter. And again, let’s go back in here and refresh that. And there’s again the separate record. Finally, what I will just show you is if we go into our function and click view files, we actually see the files that are in here. So the actual code is in this run CSX.

If we look in the function JSON, this is where we define things like the bindings and the triggers. So that Guid that we did through the integrate basically created these forwards. So we set these bindings, created a Http binding with the return type of Http and then we did another binding here, which was a table. We called the output table, that was the table name and that was the connection string there that it’s getting from the configuration that we saw earlier. So that was a very quick and easy way to create function within the portal. In the next lecture, what we’ll do is go and do the same thing again, but we’ll do it through Visual Studio and publish it.

  1. Azure Functions via Visual Studio Walkthrough

So in this lecture, we’re going to go and create and deploy another Azure function. But this time we’re going to do it using Visual Studio. Now, if you’ve not already done it, you need to make sure you’ve installed Visual Studio with the Azure Development SDK. So if you haven’t done that, run the Visual Studio installer going and enable the Azure Development SDK. With the Azure SDK installed, we can now go and create a new project from within Visual Studio. And we want to create a function project. So if we perform a search for function and we want an Azure function, we’ll give it a name as we had in the portal. We get given a number of templates that we can choose. But as you can see here, there are actually a greater number. We want the Http trigger. Again, we also have these additional options here. So we’re going to test this locally. Now, we could test it directly against a storage account, in which case we could go and browse through our online storage account.

But actually I’m going to go for the storage emulator. We then have this authorization level so we can set different levels of authorization for our apps. Function level means we can control it at the function level. Admin means you’ve got to be an admin. When we did this in the Portal, it defaulted to function level. And in the URL, when we activated the function through the URL, we actually had a code in there that gave us the access. But to make life a bit easier, I’m going to go for Anonymous and then click Create. Once that’s created, we’ll see some code that is pretty much the same as what we had before. The first thing I’m going to do is I’m just going to go ahead and actually run that. And when the first time we run this, you’ll get a Windows Defender request asking you to allow through function X. So just go ahead and allow access.

The function will start to run here and in particular, it’ll tell us what the URL is of our function. So if we take that and copy it, then in a browser window, paste that in and put question mark name equals and that will run the function and again output the details. We get a log trace here which is similar to the console log that we saw in the Azure Portal. So if we close that down and then we’ll now go ahead and hook up the storage like we did again in the portal. So the first thing we need to do is add the Windows storage. Next, again, we need to create a new class that is going to contain our item that we want to store. In the table, we will say public class name log which needs to inherit from table entity.

So we needed to add actually the table bit of the storage on there. Again, we’re just going to expose a public string called Name. Again, back up here, we’re going to check to make sure there’s some data in name and again we’re going to create a new instance. Finally, what we need to do is go back to the top. And if you remember earlier, what we had to do was put our binding in here. So we need to tell it that we’ve got a table. The table name will be vs name log. So this is going to be the name of the table that gets created in our table storage account.

Next we need to tell it where the storage account is. Now what we’re going to do is we’re going to tell it it’s Azure Web Jobs Storage. We’ll come back to that in a second. Now we want to say eyecollector name log and then log table. So we’re getting this error here and the reason is we need to bring in an additional new get package. So go to manage nugget packages, go to browse and we want Microsoft Azure Web jobs Extensions Storage. So install that new get package. Once that’s installed, go back to our function and the error will disappear. So what we’ve basically done here is in the portal, we did this through the integration tab and because we’re doing this encode and we don’t have that, we have to manually put in that reference. So now we’ve got our name log item, we can use that log table, add name log and that will add it to the table.

Now we’re going to be using the storage emulator and you’ll need to pass through it the connection string to the account. Again, this was done for us in the integrate tab. Pretty much automatically remembered it through the portal. But in here, we need to define this azure Web job storage. So if we go, we have two files here. We’ve got the host JSON and the local settings JSON. So the host JSON is what will get copied to the server. So all the configuration for where we’re two will go there. But we first of all want to test this locally risk. So we’re going to go to the local settings JSON. And you can see we’ve got this azure Web Jobs storage. And what we want to do is replace that string with our storage string. So what I’m going to do, I’m going to go to the view menu and I’m going to go to the Cloud Explorer. The Cloud Explorer allows us to connect not just to our resources that are in the cloud, but it also detects the local storage emulator that we’ve got running.

So let’s just collapse our subscription first so we can see our local and our storage accounts. There’s our emulator. You can see within the emulator we’ve got blobs queues and tables. So we want the emulator there and we want to look at the properties. And if we just expand this along here we can see this connection string here. So I want to copy that connection string and paste it into here and save that. Now let’s go ahead and run the function again. Again, let’s just copy the URL just in case it has changed. Again, we’ll put name again and hit Return. So there we go again. It’s output to our browser window. Now, if we close that down, if we go into our Table Explorer, our Cloud Explorer, and look at tables, we’ll see it’s created this table called Vs name log.

And actually I can right click on that through here and click Open. And there we see it’s created the record for us in the table. So the final step is we want to deploy this function app into Azure. So I’m going to right click the function and say Publish. Then we’ll go through and we’re going to create a new Azure function to hold this one. Again, we get to choose what kind of service plans we want. So I’m going to go for, again, the consumption plan. I could select an existing one, but actually I’m going to go and create a new one. So now it asks us to set up where this app service is going to go and give it a name. So I’m just going to change that name. I’m going to use the resource group that we created earlier, which was the RSG functions.

Again, I’m going to set the location where I want this to be built. And again, I need to choose the Azure storage where it’s actually going to store this that has now created our function. And if we go into Azure and look at function apps, we’ll see our new function app here. However, at the moment, there is no functions in it. So let’s go back to Visual Studio. And before we go and actually publish that, what I just want to do is have a look at the configuration here. If I click Manage in Cloud Explorer, it again shows us that same view through the Cloud Explorer, which is quite handy. I also want to have a look at the Edit Azure App service settings. So if we just click that and it comes up with options for changing various things. And the important one is this here, the Azure Web job storage token. So because when we run it locally, we were using our local account that’s become the connection string. However, it is defaulted now to the remote connection string of the storage account that we chose when we created it. In other words, it’s updated that connection string for us to use that storage account. If you wanted to use a different storage account, you would have to copy and paste the connection string for the table storage into there. But I’m actually quite happy with that. I wanted to use that storage.

So with that ready to go, I can go hit ahead and click Publish. There we’ve got publish succeeded. Also got this site URL here. So if we go and click that site URL, this will actually launch the app service running in our browser. What I want to do now is go and have a look at this in here. So if we just hit the refresh button here and we’ll see our function that we’ve deployed. However, note this time before where we had the CSX file, all we have here actually is a function JSON file and that’s because we published it from Visual Studio rather than send it up as text files, which is essentially what was running in the background when we created the functions portal. It’s actually compiled it and uploaded the compiled file. In other words, we cannot now edit that code within the browser.

We have to do any code edits we would need to do within Visual Studio and republish. What we want to do now, click this get function URL and copy that again. Let’s have name once again that’s written up to the browser. If we go back to our cloud service and we go to our storage accounts, go to the storage Explorer tables and now we have this new table hit name here vs. Namely and again we have our data that we’ve just published.