AWS Lambda with C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Oh Oh mmm right everyone just coming my name's James and I'm gonna be talking about now before I get started it's a cloud some terminology when I said lambda functions I'm talking specifically to this AWS lambda which is a managed function as a service product from AWS it's similar to DCP cloud functions and as your part functions it's not these kinds of London functions it's different things as soon accident so songs are kidnapped by the brain styling for three sections firstly why you might be using cloud functions and lambda functions why you might be doing them in c-sharp and then I've got a brief visual studio demo I've only got 15 minutes so once over inaudible and beer right so while our functions well you've probably heard the term service computing it's really trendy in the minute it's one of these things that a lot of people going into like working without servers and just doing things small isolated pieces of logic and for good reason like I'm architecting your system into small deployable pieces of functionality that can be deployed to the cloud in isolation has a lot benefits you can architect systems the faster more scalable cheaper to run quicker to deploy and easier to scale it's also a really good way of expanding existing systems now you probably know as well as I do that life as a software developer isn't all greenfield development it isn't or new code a lot of the time you're working on someone else's code base maybe the people that wrote it don't even work at the company anymore and it's difficult it's difficult to innovate on stuff like that so with lambda functions and with cloud functions in general it gives you the opportunity to add functionality to legacy software in a kind of cool trendy serverless way and take advantage of some of these things I've just spoken about I'm going to illustrate this with an example the example I've got is an e-commerce website so this is something that you've got written in your company maybe it's out there in the wild it's written in dotnet core and the customer goes into your web app they place an order you to get sent off to your shipping company which will dispatch it in bio districts network to the client fine the problem is when you want to add some functionality to this and what we're going to add is we're going to add the ability to notify your client when the delivery is shipped so you can send them an email you can send the web notification or something that just says your orders on its way so you go to your shipping company and they say great what we can do is we can raise an event when we send the order out we'll send an HTTP POST request to your API and you can deal with updating the client and sending out the emails and occasional notifications fine so traditionally the way you might go about implementing something like this is opening up your doughnut core API and adding a new controller we've called this a callback controller this has got one method update order and it's got a bit of code in here with some dependencies to send an email out to the client and market is dispatched in your data store now that's all well and good until your shipping company come back to you and they say yeah we'll give you these order updates but the way we work is all of our lorries leave ATM in the morning and the way our systems update is you might get 5,000 Jason post requests in the space of a second that's what you call bursty behavior and bursty behavior is a real problem for these monolithic web apps is something that you know they're busy serving your website they're busy processing more orders suddenly they've got to deal with 5,000 or you know many many hundreds of order updates in one go so one solution so this is service-oriented architecture and this is where you take your callback controller and you place it in its own service and that might go along with a bunch of other services you've got and it builds out your service-oriented system and that's fine but the one problem of this is that you now you've got this services callback service the for 23 hours a day is doing absolutely nothing it's sat there eating up resources you need to have it on half-decent hardware so that you can handle all these requests coming in 8:00 in the morning and it's just a waste of money it's a waste of resources it's not very efficient perfect use case for a cloud function you can provision a cloud function from AWS or your GCP give it some resources when all these requests come in you can even batch them up into a message queue and batch process them run them run all the code and then after it's finished it gets deeper visioned goes to sleep again you don't have to worry about it for another 24 hours until tomorrow great so now you've got your shipping company raising an event sending it into a message queue for example and batching it into a lambda function brilliant there are many other examples this is just one but why write them in c-sharp which is the purpose of my talk today let's have a quick show of hands who here use a c-sharp on a day-to-day basis great keep your hands up keep your hands up if you like it as a language exactly right I'm with you c-sharp it's a great language it's fascist express service got great tooling around it it's modern it's easy to learn it's easy to pick up however when programmers and net programmers come to write lambda functions specifically lambda functions they all default to JavaScript if you go to AWS as console and creating new lambda function you'll see a little window where you can type your JavaScript directly in to the console they've only done that with JavaScript GCP have it with other languages but this is JavaScript so this lets you you know literally why your JavaScript in there you don't have to bother about source control you don't have to bother about code reviews you have to bother about testing turns bother are all that good stuff you just write JavaScript that's what everyone does and I'm here to try and convince you that if you've got a system written into on that moving it in to the server list function way you should keep it in dotnet code reuse code reuse is probably the biggest of advances of this let's go back to our callback controller and you'll see here that the actual code for sending out the email and marking it as a spatula datastore is in these two dependencies that you've injected from somewhere else in your system and that's good that's good way of architecting it but if you were students in JavaScript which many people would try and do you have to write all that again not if you keep this in c-sharp I've got an example of equivalents lambda function here for the same callback control let's go back this is the callback controller of the logic down here and the lung function basically exactly the same literally code reuse but in a service world um here we are side by side now I've got a little demo and I've done a video of the demo because I wasn't sure if things would break at the last minute so I'm going to show you that and talk you through got here okay oh boy some very cool well I'm going to talk over my own video here here we go so for this demo we're going to be using the AWS toolkit for visual studio now obviously you can do all this on the CLI and if you do this in production I do urge you to get to know the CLI tools for this but for the purposes of a visual demo and for the purposes of brevity we're just going to be doing that with the toolkit when you install a toolkit you get this AWS explorer which by itself is a cool little tool because it shows you an overview of what's going on in your cloud infrastructure and that's something that you know tooling like this is something that AWS is sorely missing anyway so that's kind of cool but back to the code once it catches up okay here's our callback controller so this is the one I showed you on the slide what we're going to do is we're going to turn this callback controller into that AWS lambda function that I showed you earlier on you go to with the talk installed you go to your solution and do new product type it's granite alias lambda point eight course so that's so it like your project template for a lambda function if you open up you'll see what it adds for you it gives you a nice you'll see in a second it gives you a nice little read me it gives you your tooling configuration and stuff and it gives you a shell template I'm choosing empty function here but depending on what you want to hook your function up to you can have different presets and things just give that a minute ago okay so here we go you've got your function CS which is going to be the callback controller and some stuff removed in case yes done now the way lambda functions work is they need a trigger it's a bit you know as your functions and GCP functions so the trigger in this example is going to be API gateway which is a bit like I think as your have something called API gateway layer or something but that's what's going to be triggered on a lambda function so there are third-party you know example or third party can send some JSON post requests we're also going to add references to our other dotnet and standard libraries so here we go okay let's go it all that that but now this is going to be triggered over an HTTP POST request I'm gonna put a little comment here it's going to be post to order statuses with an auto ID in the path parameter okay make it async I think yeah and this string input parameter will turn into an object that comes form AWS lambdas API gateway which is an API gateway proxy request there we go so I didn't want to type without life I okay so here we go so we're going to take the order ID form our path parameters which is up here and then from this point onwards we've quite everything we need we can go back into the callback controller and literally copy and paste our code it's not very much logic but it's a function and pasted into here we need to paste the dependencies in as well now dependency injection with lambda functions it becomes a little bit less necessary because you haven't got a whole system with dependencies flying about the place you've only really got one function in this example I'm just going to new these up but obviously in the real world you probably want to unit test this and you know how the dependencies coming in through the constructor which is fine but for the purposes of just getting this out there and showing you something new dependencies that's it that's all you need to do again with the toolkit you can right click on your project and do publish to AWS lambda and them away quicker than you think okay so this is the AWS lambda console no functions in it because I've chosen Paris as a region to protect oh yeah refresh that you can see it's updated my thing here now a quick note about one times AWS only currently supports dotnet core 2.1 they do end-of-life the old frame works relatively quickly so you need to make sure you're always updating these and they do always tend to be one version behind obviously one three now but still all 2.1 it's fine especially fine if you get to take advantage of all these and here's all under function we can test it from directly in the console by building an event or we can test it actually using the toolkit in visual studio which will give you some sample requests here and you can go through and select like a fair p eye gateway was what we were using okay so go and API go where then and that's it simples up you can build up a may lambda functions as you want you can order these into projects have them more dependent on each other AWS has introduced something recently called lambda layers which is a shared our memory layer so things like you've got here your ecommerce lot services and your doctor main model you could put those into a layer and then share some of your library code between multiple lambda functions so you don't have these like large bundle sizes there are a lot of people to put off with with AWS lambdas and it really is as simple as that so I'm basically done and this is my Twitter I've only just started using Twitter so follow me and I can have more than five followers thanks so much [Applause]
Info
Channel: dotnetsheff
Views: 5,668
Rating: undefined out of 5
Keywords: aws, .net, C#, lambda, dotnetsheff
Id: Km5G9sNUmV8
Channel Id: undefined
Length: 12min 37sec (757 seconds)
Published: Sat Jan 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.