 All right, what do you guys what do you guys get ready to film? We are going to do a visual studio tool box episode I'm one of the co-hosts a visual studio toolbox to show that Robert Green started and myself and Batman Brown They're now co-hosting episodes and I have my guests here Jeremy. Yeah, I just randomly wandered into the studio I'm actually a cloud developer advocate and have been doing a ton of things with Azure functions We're gonna talk about serverless today both compute with functions And then we're gonna do another episode that focuses on cosmos DB, which is the less server version of the database I always say flip it backwards what's cloud developer advocate I advocate for developers in the cloud server less is less server It's not no server. Yeah, and in my full-time job I'm actually the product manager for visual studio team services, which is our DevOps cloud offering So I have a lot invested in Azure and Mean this guy did it up so pretty recently on that net as well So we do to Jeremy in the show. We'll do all more stuff together. I'm sure all right Well, we're ready to get started and talk about Azure functions. So Jeremy, why don't you kick it off those? What are we gonna talk about today in detail? Yeah, so one of the best ways to get familiar with the technology I think is the dog food it right and I wanted to talk about a project that I made that's my own URL link shortener So you're familiar with these we have aka dot MS here for example Who isn't know exactly and what I wanted to do was take ownership over the data So I'm very interested in when do people click through links what links generate the most Click-throughs right is it Twitter? Is it LinkedIn? Is it Facebook? Is it one of these social media sites? Yeah And then I wanted to also look at different statistics like what time of day is the most popular time of day for people to click Or day of the week so I thought what better way to leverage Functions and serverless than to build a link shortening tool awesome So why don't we just roll it back just a little bit because I think sometimes people one might be watching the video and they're like Wait, what it what are you talking about? What is this functions thing? So at like a five words or less summary What is functions and you know, we are talking about something on Azure So it's a cloud hosted thing, but what does functions give to a developer? Well, if you're gonna restrict me to five words I would say events and code right is it's functions, but but the idea when we talk about server less I like to flip it backwards and say it's less server Yes, so it's the ultimate realization of focusing on your code and not having to worry about necessarily how your code's hosted It's not even platform as a service because we're not necessarily talking about web applications Right, we're talking about some sort of event that triggers the code And then we're talking about the code that actually runs right and that code could could not be x or y Right, it's all it's all dependent what you do in the implementation. It's not specific The code doesn't care what you do with it You can write to to a script file or something you could it could generate some data It could execute a web service call it could return something to an end user, but really it's agnostic It doesn't care what codes inside of it I think that's something that even I took a while to overcome as a train of thought right that this is like You said a function in the cloud right? I mean it feels to me like the ultimate realization of the dream That's microservices if you will I hate to use an overloaded term It's hate so it's a valid term in this case I mean so some of the things that are really neat about functions are number one you can write them in different languages I'm gonna demonstrate it in dot-net through visual studio, but you can do them in Python PHP no JS You can even write bashell scripts that launch function So that's one neat thing the other cool part about the way Azure implements functions is we have a concept of triggers and bindings And these are ways we set up the environment to last interact with things like storage and cues and and it can be file storage It could be blob storage and it makes it really easy for the developer to work with those resources And I'm gonna show that in the application that I built that's awesome. Well, let's jump into it It sounds like a great demo. All right, so what I'm gonna do is is jump into the portal right now So I've already deployed this application. I'm gonna take a step back and show how this is built But I want to set the stage with the link shortener that I have I wrote a web front-end, but just to focus on the function side It's a small window, but you can test functions directly from within side the UI, which is pretty cool Yeah, so what I've loaded up here is a link that if I just paste this link in a new tab You can see it goes to some of the Azure functions documentation So if I run this through the tester, I'm just gonna click run here It will go out and call that function and I see a little completed success and I get some text back that has What is the short URL that I can use for tracking? So I'm just gonna copy that and paste that so you can see the way that experience works So JLIK, which is my first name and part of my last name dot ME Right and then BWE is the short URL when we hit this it goes through my function and redirects Now for the end user, it's a very simple experience. You just end up where you're going for me a ton of data is Generated from that that really simple event, right if we pass through collect collect quite a bit, right? It does and looking at that. I actually wrote the part that Shortens the URLs. I wrote that as a script. That's this run script. So you can see my shortener There's an encoding routine, but the the main part of this grabs a request and You know, obviously if I have no request, I'm gonna return and say basically not found. There's nothing there We get the input we check the input for null we go through and Basically, I'm tagging if it's coming from like Twitter or LinkedIn or some other things and then at the end of the day I'm really just creating a record here that maps the short URL to the long URL And I'm saving it to table storage and that's it. So when I'm putting the link in it's here's the long link And then give me something short back that I can use. Yeah now the real power of this comes from this function host that I have here and By the way, all of this code is available on GitHub So I open source this so people can work with this project. Yeah, we'll link it in the show It's definitely right. So the the redirect comes in and this is when you're hitting the short end point So you're hitting the the bwe for example So what happens here is it'll grab that short URL And then I have a keep alive that sort of pings the server make sure it's running I also get requests from robots So I tell them not to follow right because this is my link shortener Yeah, and then we come out and we go in to table storage now this little piece of code here Where you can see the start time and I'm starting a timer This is something that I love about functions because when you create the function app You can check a box and say you want to use application insights and this will automatically start building telemetry and analyzing response times and Basically giving me the feedback I need to know the health of my application Right what I wanted to do is because I'm using table storage is I wanted to measure how long it takes to read From table storage. So with this telemetry I have this operation that goes out to the table and Finds the long URL if it gets it back It says it found it gets that redirect URL and it's tracking how long that operation took And this is that's the most sense of operation. You don't want people to wait when they're being redirected That's I mean that happens all the time nowadays with public link shortness. You get to some URL and right And it's winding winding winding exactly So I wanted to test that and I wanted to just highlight how easy this was to put this custom data I literally am tracking table storage. That's the thing. I'm tracking retrieve is the action I'm taking and then I'm just passing it how much time elapsed the first two like strings or are they Did they have any bigger significance or they're just the way you decided to store? They're just the way I decide to categorize the data with application insights and then it's as simple as two two steps one is I want to do some more Operations with this data, but I don't want to slow down the redirect So I'm going to add some information to a queue that I can pick up and process later Right now the event is an HCP trigger. It came in and requested an endpoint. I throw this on the queue and then I redirect them That's it. That's the redirect code Then I have will ignore keep alive for now. I have this process queue So again, we said that serverless functions were events and code right in this case The event is there's an item in the queue and my code pulls that off and does some additional processing so I'm breaking apart the message in this code and I'm writing some custom information one of these is what I call a Custom event which is literally this came in from Twitter or this came in from LinkedIn the source Right and then the other thing so you can see I do the the track event here the other thing I'm doing and That's actually adding a document. I'll get to that in to a minute. There is a page view So with this piece of code, I'm highlighting a page view So now I know which page it actually went out to so I'm saying what was the medium? What was the page right and I'm adding that all through application insights and I want to show what that looks like and then this is a piece that we can Probably dive into at another time because this is creating a document for Cosmos DB And I added this on later I started just tracking through application insights But then I decided to create a Cosmos DB database because then I can stand up a Power BI dashboard and do some Exciting things around that and so what what is a Cosmos DB database of those did don't know yeah, it's the Actually the function is serverless compute the Cosmos DB is serverless database It's a not only sequel right no sequel document based Database that's hosted in the cloud what's really neat about Cosmos DB is that you can pick your interface We have a document DB interface that was developed for the previous incarnation document DB Well, it was called document DB when it first came out on Azure right it was you know very comparable to Mongo That's how people often talked about it that Microsoft's entry no sequel space and then more recently we renamed it to Cosmos So there's any confusion out there. That's sorry Microsoft loves to rebrand, but right the product has been very solid very you know long development cycle already forward and then quite You know the cloud scale player I think is what they often call it right this database lives in ton of data centers as a concept and use Developer of powerful flexibility to deploy your data across the world Well, you do you get to choose your consistency levels and you get to just click with replication Which is pretty powerful and the other thing I'll point out is this is using the document DB interface And the reason why I call that out is because you can use sequel syntax to query with document DB However, there are a ton of developers are used to Mongo DB as you mentioned Yeah, and you can use a Mongo DB interface and write an application around Mongo. So it's very flexible in that respect Yeah, it's very cool that you have these options and you know I'm a sequel developer most of my life and I've been toying with with no sequel I kind of like it and it's awesome to see this not not really stop me because I actually start with Mongo It was my first database that I learned, you know, just the basics around the necklace me I'm a developer yet on that space, but having sequel I didn't realize we even added that so that's awesome I'm learning something new in each episode here, and that's what I'm finding out with cosmos DB. It's so easy I'm a sequel developer myself. I worked with it for 15 years So I had that mindset transition to a document DB But after standing up actual applications using this for my analytics I'm finding out that it's not only easy to work with but it is scalable and fast Which is is everything someone wants and I don't have to hire a DB. Yeah It's literally inexpensive. I run this shortener on my own subscription through Azure and one of the huge benefits I see from Azure functions is I only pay when someone clicks through the the function Yeah, right and with the model I get a million calls per month right now I'm sure it may change at some point, but with that it's literally pennies for me a month I think last month there was a quarter for the storage and the function calls So cheap to handle and it's 2,000 requests a day Yeah, it's pretty cool that the thing about you know all of this technology in top of Azure is that you are living in the cloud Your credit card is plugged in there yours or your company's or whatever in the trial We do give you a limit So if you want to not go over the free trial limit we provided an Azure But I'm always kind of encouraging developers thinking about moving to the cloud as the first things they think about telemetry So it's awesome to see application insights built in you actually want to know what's going on with your cloud service It's not just you know consuming your resources of your service It's going to cost you money potentially depending on how much you know requests come in and people just need to think that way It's a right slightly different world, you know they have today money matters So it does and speaking of application insights What I thought I would do is show some of application insights Just so people can see and this is just the check mark out of the box when I created the function app and some of that Custom code and I want to show the reports that it gives back and then we can look at what it looks like to create a function from scratch Through visual studio. Yeah, let's take a look. So let's pop over back to the the website so this is the the function app I had running and I've got all these features that are configured one of the things I wanted to Call attention to is this is using a feature functions called proxies and that allows me to map One route to another route. So the function app itself is something like blah blah blah as your websites net slash api URL redirect question mark short URL equals totally friendly, which is is not a short URL So in my proxies and you can see this in in the code. This is part of that code base. I've mapped What is a short URL right here? So just slash and what the code is to that longer URL and that allows it to translate behind the scenes No one sees that happen and maps it on to that longer function So I have my custom domain and then I have the Azure functions running behind it cool If we come into the application insights portion The very first thing that application insights pulls up for me, which I love is the overall health of the system So we've got this live stream, which I'll show you in a second But this is really informative to me here because this has given me an overview of response times over time And you can see that I'm averaging a few hundred milliseconds, but this is across all of my services So it's not necessarily just the redirects, right? You can also see a count of requests and then up here We've got something called smart detection. This has actually alerted me several times when it finds something That's outside of the norm it will automatically email me and tell me you know what your redirect usually only takes a hundred milliseconds Suddenly it's taking two seconds. You should look into it It automatically does that and when I click on it if there was a smart detection It also helps me diagnose and shows me the sessions that were slow sessions So I can look into them and try to figure out what's going on. I'm going to scroll down here to performance And when we see performance, this will let me pick So overall you can see a hundred twenty two milliseconds. What's really important to me is the redirect experience, right? That's what the end users experiencing. So we've got a 94 millisecond average And if I want to drill into more detail, I can come up here and over in this graph I've got my percentile so I can see what the 99th percentile looks like and you can see the majority Falls even faster than that. Yeah, so and this is the least expensive Version of functions you can back it with beefier servers, but even with minimal resources It's still giving me this type of performance The other piece is because I tracked that custom application insights telemetry if I click on dependencies You're gonna see my azure table storage operation, right? And you can see the average there is 17 milliseconds, which is more than fast enough for my needs and what I'm doing here Yeah, it's cool The fact that you can drill down all the way from like what the request you're doing overall to the custom Tracking that you did but in a particular block of code I think that's super powerful and the fact that it alerts you is even more awesome I've used application insights kind of since the beginning. I haven't been included in some products So I do have dashboards, but I haven't implemented recently and it's cool to see it Even though they added that new automatic notification Yeah, it's pretty cool The other thing that I call this the the sad portion of the application Because it is sad to think that you might be sitting in a hotel room watching the live stream of your website saying is someone Clicking it is someone clicking it It's a new thing. It has its own level of news. Listen when you when you sort of create something This is your your baby one of your babies now, you know better or worse It's it's fun to watch. So this is a live set of requests and as the requests come in It's it's gonna show me those and I may actually force the issue over here But let's go ahead and do a redirect and I'm just going to the generic azure doc So that's the shortcode to land on the azure documentation. You can see it right here It showed me the request that showed me how long it took and then I have all my trace information on the side So if I want to drill into details, I'm not going to click on this now because it'll go into IP addresses and Mac and like everything You wanted to know about session, but you can drill into the detail the other piece With app insights and again, this is just out of the box without me adding anything Is this nice little tab right here for analytics if I click on this It's gonna drop me into basically everything that it stores is Corriable for insights so I can set up pie graphs and charts. I'm gonna just open up My folder here and look at mediums over the past 24 hours and run that and this will give me a pie chart That breaks down Twitter clicks LinkedIn clicks blog clicks, etc So you can see Twitter is definitely my main medium of choice for for generating links. Yeah Yeah, I remember somebody showing showed me this at ignite actually this this is like a sequel language, right? I have to get what they call the language itself But it's to me when I saw the first time it's like, oh, this is a sequel close enough I guess it allows you to query all the various parameters and this is again This is the back end of application insights data has been collected. It's not any other kind of data But I but it can I think be mixed with other kind of data sources to give you a more holistic view of your environment If you had a complex deployment at least that's how the person described it to me in the demo, right? Yeah No, it can and it's all promptable if I'm inside and I start to type it's giving me IntelliSense so it'll walk you through creating these these queries and that's exactly how I built the one you just saw And I'm gonna open Top page views and we'll just do this These roll ones you built yourself, right? It didn't yes They didn't have those and by default and it saved it for me And then I just dropped into table and I could see channel 9 It's actually one of the more popular links in the past 24 hours, right? So that's always nice to pull up on the fly. We've got Azure. This is actually interesting These are all Microsoft properties actually do tweet other links, but these are are the popular ones for 24 hours Yeah, that's cool. So that's the application insights experience Let's pop over to visual studio and just show someone what it looks like to start a new function app Yeah, that was great. I think I think it's awesome when we show how we did something and we provide the code But often forget this part, right exactly. How do you get started? Yeah, so the the prerequisite here is I'm working with at least 15.3 right so it's a preview version of visual studio 2017 right think we're up to 15.5 Any of those versions will work for functions. Yeah, if anybody has been kind of away from the visual studio versioning game for a while That's the versioning scheme we've gone to and if you like click go into about dialogue for a second under the help menu Sure, yeah, you'll be able to show so if anybody has any doubt which version they're on you can jump in there And there's a 15.4 preview two of that particular, you know preview branch and you can install it side-by-side So I have always the RTM version and the preview machine it tends to work really well. I've never had any issues asterisk Devils in the details be careful like now you said it so I care about your machine in your environment users out there But yeah, that's how people know in case they don't know which version Yeah, so once you have that install and you have to pick the cloud workload We'll go into a new project and Azure functions lights up and the way I like to talk about this we talked about events and code The function is the actual piece of code that gets executed the function app is the host for those different Events and pieces of code right so you have a host. That's what this Azure functions is It's the minimal amount of you know stuff around it to make it work as a function and the rest is up to you, right? So what this will do is it'll create an empty project for me. That's just ready to host an endpoint so then I can click on that and add a new item and Fortunately, these are sorted alphabetically so as your function comes up right on top Yeah, that's convenient and I'm just going to give this name of echo name and Click add and what will happen is it's going to create a class for me the entire experience For functions within visual studio is is attribute driven So it's very easy to add what we call bindings or connections to things like table storage and cues What I'm going to do is create an HTTP trigger This says basically you know what call this piece of code in response to an HTTP request Which is a pretty common scenario and I'm making it anonymous so anyone can test it out and access it But you can we support permissioning but for our example here, right? No big deal And I think it'd be also good just to kind of glance it we've gone to dependencies like what you get packages Is this team bringing in to to make this the default so you just got the function SDK and that's about it Right, so it's very bare bones at this point. There's no application inside. There's no you know It's not gunk if you don't need that stuff It's not right by default right and then if you want to start tracking custom telemetry You would just you know add a new get package going to the Explorer and add that that experience Yeah, I think if you right-click on dependencies, it would give you that yep at reference so what we've got is our async task and What's coming out of this is a response message because we have to respond to the request what's going into it is a trigger There's two types of parameters We're typically gonna pass a trigger is what actually causes it to get called and then a binding is something We might interact with and I'm gonna show how to do a binding in a second cool So we've got this anonymous it supports getting posts the routes. No here's the request and here's the log and by default It creates some code that expects you to put the name in the request body or in a query string And it's going to echo it back right, but what I want to show is before we even touch Azure I've downloaded the prerequisites. I've created a function app. I can put a breakpoint right here and Press debug and what will happen is it's going to run a full version of the function host right on my laptop And we just announced that we have this experience available cross-platform cool So it's not just Windows machines. This will run on Linux as well and Mac OS nice So it's a I've been waiting for that when I I gave I kept hearing it was coming And it's awesome that we we finally announced that I missed that part not not that I'm a Linux person or anything I still run a PC so I'm quite biased right. I spent a lot of time I actually was having fun running the function host in my Ubuntu on Windows 10 Yeah through the windows subsystem for Linux and then accessing it So all I'm doing is pasting the URL it gave me and you can see we hit the breakpoint And if I continue for that, it's going to slap my wrist and tell me that I needed to pass a name So we'll go ahead and give that a name name equals VS toolbox Hey, we'll brand it go and we hit our breakpoint again, and we'll go ahead and continue through that And what we'll see is it'll echo back and say hello VS toolbox. So that's a great start We've got it running locally. We were able to debug it. Let's take it one step further and load it into Azure What I'm going to do is just right-click and go into publish And the publish experiment experience will allow me to create a function app in the portal and connect to an existing one Or I can spin up a completely new application right from right here. So we're going to I love your slip there earlier the publish experiment Sometimes the world can be a public experiment We're going to try for the publish experience today. Let's see if we get that so we're going to call this my VS toolbox app use my subscription and I'm going to give it a second to spin because what's validating is making sure that is indeed a unique application name Yeah, I'll give it a new creating the cloud your resource It's assigning it to to the resource group of your choice or a new one So there's a lot a lot to do there, but you never have to eat the idea in this case, right? I'm going to pick a location will do West US since we're filming here And that's the biggest machine can we do here? Oh my goodness you would want to do this, right? So one thing I do want to call out I can pick these various cores to back my experience I can also pick the consumption plan the consumption plan is what I call the easy button for functions I don't have much control over size of the servers, but I'm allowing Azure to do all the scaling for me It will automatically look at incoming requests and do what it feels it needs to do to accommodate those awesome There's always trade-offs right if you have dedicated machines You can configure them to be always on so they're ready to start where with the consumption plan It may go to sleep and you have a new request and it takes a little bit longer to start up But again, you have flexibility of options for that I think consumption there's a reason to go at a price wise right there's some Differential there that it's a good deal if that's good enough for your production environment, right? And that's what I'm using for my redirect experience as well I just have I cheat a little bit with a keep alive that pings the server to keep it awake Yeah, well, that's valid. They know you're you're consuming it Nobody feels cheated in Azure team. I'm sure right. Yeah, no it they're keeping an eye on you But for good reasoning just for good reason that that's right. Yeah, so this is going to deploy and there's really I like to share When people are watching this spin, there's two steps to this the first step is Creating the assets to host the application So it's going to create a resource group, which if someone's not familiar with Azure that is a logical group of related assets I think of it as life cycle if something's going to be created or destroyed with something else So you have an app and a database if they're related They probably belong in the same resource group and you can track cost by resource group And you can script out and even delete entire resource groups. Yeah, it's a very powerful idea at first That was you know when research groups were introduced. I was a bit frustrated. It's like another thing to do There's a great way though. There's a lot of benefits to this other thing to do So now I don't even think about it twice. I really I'm starting to memorize resource groups. I'm getting that bad I'm like, okay, this is my my miscellaneous one if I need to just throw something up versus, okay This is a bunch of real stuff together then I group them right and it's great, too If you're giving demos or meeting meetups or user groups because you can create a resource group per That experience and go through the same demo but partition it off. So once it spins up those assets It's got the host. It's got the function app the function app has storage with it Next thing it does is builds and creates a set of assets that it then bundles up and deploys out So the first step is what is my target and then the second step is publishing and now it says publish succeeded So what we'll do is we'll just copy this end point right here. I click copy to clipboard And we'll paste it in and notice now instead of running off local hosts. I'm doing the full my vs toolbox app You selected that was the unique name. Yeah, and we called it echo name. I believe so if we hit that It's gonna spin up and it should slap me on the wrist again and tell me that it's expecting me to put a name inside the Corey string and what we're seeing right now is is that spin up, you know for the first time and We'll let that go and and I always encourage People to use the mouse trick, right if it's a clockwise mouse rotation should speed things up for us That is definitely the magic Right there when you know Jeremy's magic tricks and look at that So please pass a name or Corey string now that it's spun up if I correct this and we'll do this again name equals vs toolbox Boom immediately it comes back. Hello vs toolbox cool, and that that's the experience So basically I showed you how to create the function app from scratch from Visual Studio But then if you want to see what a mature application looks like something I'm using in the the real world Then that's gonna be on the the github site for the link shortener that I created Yeah, and the fact that you can write this thing in many languages the fact that it's all cloud globally hostable I mean, it's it's amazing what you can do nowadays. It is yeah But the only thing I don't have in production for more recent stuff is probably functions But we should some find some excuse to put a function up there, but it's cool Yeah, yeah, no It's a great experience and and it's something that I'll be looking at too is is how do we take existing? Web applications and migrate them where it makes sense just because you can't move them doesn't mean you have to but where it makes sense Because ultimately what happens is instead of one app with five endpoints that if you make one update You have to redeploy the entire thing with the function you can literally just test and redeploy that one endpoint in isolation and you know you are very familiar with DevOps and That experience and being able to continuously deploy at a microservice level is a huge benefit in my book Yeah, it's super awesome And if I you can monitor it you can really get a sense of do you need to roll something back How is it doing or if there's any underlying performance issues in your infrastructure like the table story slowdown whatever I mean Right, you have lots of flexibility nowadays to understand what is going on with my production environment and my staging environment If you put everything up in Azure you have all that same telemetry for all your environments, which is really awesome Right, I've always loved loved that part of it my dev the dev and experience is awesome right to get you going But get it up in the cloud you get a lot more sort of benefit. Absolutely. I'm digging I'm a one-man shop right with you are shorting tool, but I get all the telemetry and information I need to make sure it's running and up and healthy awesome Well, is that all you wanted to demo for this particular that was that all right Well, thank you for being on digital studio toolbox and we hope you folks enjoyed the episode We'll make sure we'll put the links to the github repo and to any other resources we mentioned in the show notes And we hope you come back again and watch another episode of visual studio toolbox So thank you Jeremy for being on and see you folks later. Thank you. See you