Create a Serverless .NET Core 2.1 Web API with AWS Lambda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this video on creating a servile Astana court to appoint one web api with AWS lambda in this video we'll look at how we can create a dotnet Court open one web api then publish this to our AWS account a demo web api will be looking at creating a shopping list allowing us to add remove and get our items from a shopping list I'll also take you through some of the differences between tonic chord Web API lambda and a standard dotnet core web api will then look at how we can publish our lambda and interact with them let's first quickly talk about what a lambda is lambda is a service product that AWS offers when you hear the word service it sounds like there have been no servers when an actual fact there are behind the scenes what lambda offers is the ability to manage the service for you you push your code up and all the scaling in silver management is done for you one of the advantages to using lambda is the cost of execution you're only charged for the time your code is executing if your code is only active for 30 minutes you only pay for 30 minutes this can greatly reduce the cost of hosting and running your code there are two common lambda models creating an AWS lambda project and an AWS server list application when creating an AWS lambda project this is a c-sharp class for the function this function can be triggered by Vince or executed from another application an example function could be you create a lambda function that scans and checks the log files for student events or entries from Cloud trail or cloud watch you could then use another alias resource such as simple notification service ECNs to send out a notification if these events or entries have been found the second is the civil ascetic asian this utilizes the API gave a proxy integration the proxy fords all requests coming through to the lambda function this is then passed off to the AWS dotnet core pipeline this means you have an asp.net core middleware at your disposal when using the lambda service application it's handled slightly differently comes into the API gateway this is then passed on to the lambda and then passed on to asp.net core if you are creating a dotnet core with API and you want it as a lambda this is the one to use before we get started on demo there's three things that you need to have you need to have Visual Studio 2017 you also need an AWS account and thirdly you need the AWS toolkit for visual studio instructions and links on how to create each of these are in the description below okay so let's go ahead and create our project I'm going to hit up to file new project and under the AWS lamda project we're going to sleep the AWS civilus application we're going to call application lambda shopping list weave API sleeked okay and then Boop centered with some blueprints basically templates we're going to use the asp.net core Web API template select finish when using a template you're often given stuff that you don't actually need and it's the same in this case here we're given code to do with s3 but we're not actually dealing with this three and this project so we're going to delete some of that stuff we're going to delete the two controllers and we're going to head over to the server list template file and get rid of anything to do with is three so our civil a stop template file should end up looking like this will come out to the server list template file and discuss what it is and what it does in two seconds let's first have a look at two other files the lambda entry point this is the entry point that will be used when we have our project inside AWS then we have the local entry point file this is so that we can debug and use our application locally this is really important because we want to be able to test our application before pushing it up into AWS and back over to the server list template the lambda entry point is configured inside the server list template as a handler for all traffic coming through the API gateway the proxy plus sign in the path me this function handler is configured as a proxy resource and all traffic coming in on that path will be sent to the function handler async method this particular method exists on the API gateway proxy function base class and as part of the AWS lambda for dotnet core library you're more than welcome to go and have a look at the source code for that function but what it essentially does is that it marshals the requests coming in from the API gateway to the asp.net core pipeline from that point the normal asp.net core pipeline takes over and you are presented with all the greatness that comes with it routing moral binding etc okay so let's start by adding the code needed to create our shopping list Web API first I'm going to edit controller then I'm going to call it shopping list controller I'm going to add a route up to the top this is the way that we can interact with each of the methods inside our control class so we're going to say v1 shopping list we're going to have 3 methods we're going to have a get a post in a delete the get is going to get our shopping list post is going to allow us to add items to our shopping list in it delete is going to allow us to delete any items from our shopping list so first things first let's say get shopping list then I'll still return ok so that the project builds and now we're going to call off to a services class to get our items at the moment we don't have any of these services created so you're going to see some red squiggly lines but we'll use wishful programming which means that we are going to pretend that as there and we're going to start creating methods that we want to use and then create the bottoms and classes after the fact so get items from shopping list and we don't need to pass anything em in on Savior results from the shopping list in more PASOK result back to the user okay so shopping less you can see is read and we don't have that at the moment so let's create a service so under folders I'm going to create a folder called services and in the services folder I'm going to create a class called shopping less service and I also want to give an interface so that I can pin the injector as well so I'm going to say i shopping less service anything resharper i'm just going to allow Rishabh to create it for me and i'm also going to move it into its own file as well okay so hitting back to the class shopping list okay so let's create our first method which is going to be public we're going to have a return type of dictionary it's going to take in the string and then end the string is going to be the name of the item in n is going to be the quality quantity then we'll say get items from shopping list which matches up to our method call from our controller takes in no parameters and we simply want to return a shopping list dictionary which we don't have at the moment but let's create it in a second okay so at the top here we want to create a dictionary so we'll say private read-only dictionary of string and nth and we'll call that shopping less storage and will instantiate there great now so we have a dictionary up at the top here and we have a method our first method which will get the items from a shopping list this makes sure that we put this method signature and to our and face as well and we'll import the dictionary type to our controller now we need to depends inject this into the top of our class so we're going to say private read-only I shopping list which is the interface we created before a shopping list service and we can call the shopping list service and I'm going to let resharper instantiate the field into the constructor and also we need to dependency inject it into the startup as well so we're heading along to startup file and we'll say services dot ed singleton and we'll pass in the I shop unless service and bind that to the shopping list service class okay so that's great so now we have a way to be able to grab our items for my shopping list the only thing is that we have no items in our shopping list and we have no way to add those items so let's create the next method which is going to be the post okay so first we're going to end a decorator I'm going to say HTTP POST and now we're going to write out public a return type of I H and result M will say ed item two shopping lists this is going to take in a shopping list model there we don't currently have and we're going to receive the shopping list model by the body of the request coming in so we'll say from body this create our shopping list model before we go any further and it create a folder here called models in that folder I'll create a class called shopping list model and we're going to have two properties in here so the first is going to be the name of the item and the second is going to be the quantity getting back to our shopping list controller you know and pour that reference let's call off to our shopping list service and we'll say edie items to shock and loss and we'll pass in the shopping list there's been stored up in our parameters so we don't have an EDI tomate unless so let's create that now on our shopping list service we're going to have a void return type create that method in our shopping list service and we're gonna say shopping the storage which is our dictionary and we're going to add to it and we're going to pass in a shopping list name for the item and shopping list dot quantity perfect so when we add an item it's going to add the name as the string and the quantity as the int into our dictionary in and get to our shopping list controller we want to return okay when it's successful and finally let's create a delete so we can get rid of items if we need to so the HTTP delete is the edge cream then we'll publication result again delete items from shopping list will also pass in the shopping list model so we know what item to delete and we'll do that from the body of the requests coming in call off to our shopping list server skin and we'll say remove item and we want to pass in the name of the item that we want to remove and if they're successful were returned back and okay let's create that remove item okay so let's call out that generic in and we'll say remove pass it in the shopping list name perfect save and build them a bit to our shopping list controller everything looks to be working so now let's test this so we're going to grab the local host and head over to post mem and into the local host with our route v1 shopping list so if we try get at the moment there's no items in the shopping list but let's add an item so we're going to head over to post the first item we're going to add is milk the quantity of one sort of select send its head some breathe as well and it's a we need five loads of read so hit sinned he bit dragged get cool so now we see that we've got two items in our shopping list items have we want to remove one let's head over to delete let's say we didn't actually want five loads of read so we just need to pass in deep read and the delete and the free picture i give we see that we only have milk here okay so now it's time to deploy to AWS and create our lender the good thing about deploying to lambda from visual studio is that the AWS toolkit now provides a pretty easy way to be able to do this so is right-click on our solution and you'll see that you'll have a publish to AWS lambda make sure that you've got the AWS toolkit enabled and installed okay so first you need to create an account I have my account already set up but if I didn't I would select add another account and I would need to add an access and secret key if we here long to AWS console under I am roles and users this is where I'd add a user to be able to give me the access and secret key once you've created your user profile select region you want make sure the frameworks stopping it core 2.1 for the latest and we also need to create a new east three bucket I have a bucket set up here but if I didn't I could create a new one here saying timp 1 2 3 is three bucket select ok and what they would do would create a bucket and now AWS console so hidden back over the AWS console if we're here long to East 3 we can see the temp 1 2 3 bucket has been created here and you also need your stake name so let's call this shopping list lambda Web API and that's pretty much it please publish the application okay so that's our application uploaded into e3 and now it's going to create a stick behind the scenes including our lambda you see that we have creating progress up here we'll wait until the it says create complete okay so that should be completed let's head over to our AWS console again and head over to lambda make sure that it's been created and you'll see our shopping list lambda has been created here awesome you've got monitoring if you need to view any of your logs you've also got view logs in cloud watch as well okay so let's test to make sure that this is working you'll notice that we were given an AWS civilus URL so this is the URL that we can use to hit our service so we hit along - postman we put our URL without route that we added to our controller which is v1 shopping list Emma's using the get will hurt sinned we've got a two hundred back but nothing Nellis that's right because we haven't added anything here so let's add something now you long to post and the body we're going to add bread in a quantity of one slit we've got two hundred back great it did milk in the quantity of two that's send two hundred back now let's see if we have our shopping list awesome we have bread and milk and of course if we want to delete something but he longed to add delete in point and we can just delete milk back to get and milks now gone so we've just created a simple dot knee core tip on one application then pushed up into a double users lambda thank you for watching if you liked the video please remember to hit like and subscribe to my youtube channel to keep up with all my latest YouTube tutorials
Info
Channel: Daniel Donbavand
Views: 67,004
Rating: undefined out of 5
Keywords: NetCore2.0, ASP.NET, C#, WebAPI, .netcore, visual studio, dotnet core 2.0, dotnet core, dotnet core tutorial, dotnetcore aws, web app tutorial, AWS, Amazon Web Services, cloud computing, Lambda, AWS Lambda
Id: OhEANj3Y6ZQ
Channel Id: undefined
Length: 17min 57sec (1077 seconds)
Published: Sun Aug 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.