Building, deploying and hosting a REST API using .NET Core & AWS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] a few words about myself i've been a dotnet developer for uh eight years now i'm yulia dormenko in case some people don't know me um i've been working with aws for the past year and i've really grown to enjoy it let's say and today i'm hoping to give you sort of a nice little sort of overview of how to build deploy and host rest apis in the aws environment using of course.net core okay let's get started then i think we're all pretty comfortable with the idea of having dot net core applications and dot net in general running on azure of course it's under the same big umbrella of microsoft and it it just seems right you know but maybe uh you wouldn't think that it's actually very possible and it can work very well to have.net core applications running under aws they actually have a lot of tools and support for that including an sdk a cdk and all sorts of other great tools and today we're going to be exploring one of the ways in which these two tech giants can work together basically okay a little disclaimer i'm i'm gonna be talking and going through quite a few different aws components tonight uh it's not sponsored but unfortunately i'm not going to be able to go into very much depth into all of them in just one hour my hope is that i'm going to be able to give you enough context and enough details to sort of get you started and you will have a repo with both the demo and the slides available at the end of the presentation but yeah forgive me if i'm not going to be able to go into so much detail with each of the aws services we're mentioning tonight i was chatting to a colleague yesterday and we counted there were like seven different services that we're going to be touching so an hour is just a little bit too little time for that okay uh first things first we want to build a rest api how would we do that you know first thing we would like to achieve is okay let's have a new project how does one create a new project that's gonna be runnable on aws easily well aws were kind enough to create a toolkit for visual studio 2017 and 2019 there are also versions for older um sort of versions of visual studio available i think it goes back to about 2013 and potentially even earlier than that and what this toolkit provides is a series of project templates that make it very easy to set up a rest api that will then be easily deployed on aws infrastructure but also sort of some explorers to help you see various resources in your aws account uh and also the ability to actually deploy um to your particular aws account sort of in a more play project sort of my developer project sort of thing and we'll actually see that in the demo later on okay so we have a new project um cool let's call it aws redness demo and we're gonna start to notice something's not quite what i expected it to be in sort of a usual rest api project that you would create just in dotnet core so what are all these weird files here well let's uh have a bit of an overview shall we you can see that there are two different entry points a lambda entry point and a local entry point and these will be the files that we use to actually run the api in two different environments locally and of course on aws that's the whole point we also have the serverless template which is where we specify the resources that will need to be created for our rest api to work and also a lambda tools default which is used by the aws toolkit to save parameters using the in the deployment process and we'll look at that in a bit more detail later on okay the uh big difference and sort of the most important thing that you need to have in a rest api project that you want deployed on aws is basically the lambda entry point this helps you bootstrap the asp.net core framework so it will actually run on aws and as you can see um it there's not a whole lot of coding that you need to do up front the important thing to notice here is that the lambda entry point actually actually derives from an api gateway proxy function and this base class if you drill down enough in the hierarchy actually um contains the function handler that will actually be called it will run your code eventually as we shall see but out of the gate there's not a whole lot of things that you need to do to get this running in the first place and also in that function handler you do have the opportunity to extend it and override some of the basic functionality there if you want something like custom logging or um if you want to sort of plug into other life cycles of the lambda function stuff like that but yeah initially there's not a whole lot that you need to do here and of course it uses just the same old startup that you would use in a regular asp.net core application okay moving on to the other local entry point we can see here i've put them side by side with the regular program program class that you saw was completely missing from our project but they are essentially identical and the point of having the local entry point is to be able to uh run your function locally and your api locally in kestrel like you would any other api basically okay uh in terms of serverless template um the serverless template that comes with the basic project is a sam template a bit of background here there are sort of at least two ways of writing templates for aws resources the basic and quite verbose one is aws cloud formation um and the sort of friendlier more developer oriented one is aws sam serverless application model and what sam does is it both allows you to write templates that are a little less verbose so it would allow you to not specify every single thing that you would need in the template but it also gives you a set of tools to deploy and get your package ready and uploaded in the aws environment as we'll see later on um in the project as we'll see this is in json format and during the deployment process all of it gets deployed gets transformed to yaml luckily jason in my point of view is a little bit friendlier to you so i'm happy that it's jason personal preference here okay let's look at sort of the basics of a sam template okay so as you can see here i've minimized some of the resources so you can see an overview but basically the elements here are the transform that says that this is a transform over a cloud formation template of course description stuff there is an area where we can specify parameters that we would like to give to the serverless template and these can be used in the resources and later on in the template and there's also a section for output so if you want to create a url or if you want to output certain variables or things from your stack you can do so in the output section and if we look at the resources bit in a bit more detail we can see here that for our project we only have one resource declared which is for the asp.net core function and what's interesting is it's a type serverless function which essentially is lambda basically and you can see here that the handler specifies the lambda entry point function handler async which was impressive present in the class itself but what it was present on the base class that the lambda entry point actually derives from as stated previously uh the code uri is um basically point is going to point after we deploy in package and everything it's going to point to where the deployment package resides on an s3 bucket as we'll see later on and we can also specify some memory and timeouts here as well as um access policies to sort of be able to run our our function um the interesting bit here with events is basically stating how our lambda function will be triggered a lambda function by itself doesn't do a whole lot unless it gets triggered somehow either by a schedule by an sqs message by an api gateway request and many other services basically in the aws ecosystem and in this case what these events will amount to is an api gateway trigger so whenever we get a request on our api gateway the lambda will be triggered and it will get resolved okay so some considerations uh essentially for each request that we'll get on our api we will be triggering we'll be creating a new instance of the lambda and that instance will handle the request that does mean that we need to think about sort of memory allocation and how long that function will be able to uh keep running so in terms of timeout by default the memory allocation is pretty low it's only 128 megabytes and the default timeout is three seconds but these can go up to almost three meg in 64 megabytes increment three gig sorry my bad and the maximum timeout here is 15 minutes of course maybe you would like to not go to the maximum timeout if you can avoid it but sometimes you just need that sort of um sort of leeway so that that's just something to keep in mind when you're actually building a real api in the real world okay uh so we've built our api project um say it's nothing nothing very fancy we have all the components going on let's start thinking about deploying and in order to deploy of course we're going to need to create a deployment package and what's the first thing that you would think of for deployment package well dot net publish that's what we do in dotnet core but is that going to be enough to create everything that aws needs well turns out aws needs of course the compiled assembly dependencies and everything but it also requires two additional files a project dependencies json and a project runtime config json and these don't get created by default if you just run dotnet publish you're gonna need to specify some additional arguments one of them being the generate runtime configuration file and also you would need to do to specify the runtime in that command line as well but in order to make our lives as developers easier what aws has done and we thank them for it is packed all those commands and parameters into a nice little global tool which will allow you to deploy your lambda from your local environment and you can install this from the.net cli with a simple.net tool installed and the command to deploy it is just.net lambda deploy serverless very simple we'll look at that in just a second later on the interesting thing about the deploy lambda deploy serverless is that it in the background it actually still calls the net publish command as we'll see but it also specifies this generate runtime config the runtime and a couple more configurations there to be specified so essentially it's just a nice wrapper that we can use without having to think about and remember all the other command parameters which is nice of course you don't always want to deploy only from your local machine sometimes you have a ci cd pipeline like team city or jenkins maybe even azure devops you can actually deploy stuff to aws from azure devops which is cool and in that case after you've created a deployment package you can use sam commands to actually package and deploy that particular package or just use the simple sam deploy command with to the same effect it's been recently upgraded those are sort of the big options uh and also as stated you can use the toolkit from visual studio to deploy but again that's more of a developer option so to speak so that being said let's exit this nice little presentation and start deploying our api so um this is the rest api that we've been looking at so far um you'll see that the functionality in the api itself is very dumb it's just the temp regular template for users to prove that it works because the point of the presentation wasn't necessarily about that functionality so we have this already set up and what i'm going to do now is actually run the deploy command and let's see what happens to it deploy serverless so what you'll see here is the first thing it asks for is the stack name for cloud formation essentially cloud formation stack is a way of grouping your resources so they know you know that they all go together and if you need sort of delete or update it you're going to know you're hitting the appropriate resources you don't have to spend time figuring out okay i have 300 lambdas in my aws account which is the appropriate one for this application and you'll also see that we also get prompted for an s3 bucket uh which i'm going to find right now and the purpose of the s3 bucket is to have a place where to upload our package and from there aws and cloudformation respectively are going to use our package to create the stack create the resources that we need as you can see the publish command has started we're zipping the deployment package and we're actually uploading to s3 and where we started creating the uh cloud formation stack and i can actually show you so this is my personal account in the ireland region because it's rather close to romania more or less and you can see that my stack has begun creating we can see here the events as they start to come in we're gonna get nice feedback and this this is the point in time where you start praying that the demo gods will hear you and everything will go smoothly even though you've rehearsed it like 500 times but you know it helps it helps to pray a little bit okey dokey so our stack has created let's take a look over the resources that we have here remember that in the template we only had one resource specified the function but when we take a look here we see that there's actually a whole bunch of other things that have been generated for us so of course the first one is the lambda function that we specified there's some permissions and a function rule that gets created for us but there's also a api gateway deployment for that gateway and a particular stage for that gateway i'm not going to go into too much detail it's about that one but that's kind of what it is um so let's go and look here at our api gateway so we can see here it's generated with a route and a proxy and if we go to sort of explore what what's on this sorry my headphones have uh the ability to self-close if they don't hear sound for a little while so bear with me here i might have to do that a couple times during the presentation okay uh so what you can see here is if we explore in the api gateway div that we've created you can see that we have sort of the gateway itself we see that we can have a method request and essentially uh the gateway is simply a proxy to our lambda in this case all the requests will flow through to the lambda that we've created inside of the stack which is exactly the same one and the response from the lambda will get outputted to the api gateway as is so there's also also the option of sort of setting up transformations if you're just doing an http or a rest api gateway with regular methods and endpoints but how the lambda functions and api get into a function together in the case of net chorus apis is that they just act as a proxy sort of because the lambda in itself is just a function running somewhere inside of uh aws it's not really exposed in any way to the internet so you would need some sort of way to get your clients to have an endpoint that they can call and that's exactly what the api gateway does and i also wanted to show you on the api and on the stack essentially that we output this lovely url which is generated by aws which actually points to the api gateway that we have here if you'll notice it starts kind of with the same letters twk something or yeah and let's go access our lambda our api sorry you can see that it's up and running we can give it a little test even though it's not very smart api well we can see okay everything's up and running working nicely great um before we move it on from this though i would like to also show you in s3 sort of what our uh where our package actually goes because it is quite interesting so we can also see here the template has it's been transformed and i mentioned previously the code uri property on the function resource and previously it was just an empty string but as you can see during the deployment process what happened is that the deployment package gets uploaded to s3 on this url and the template is actually transformed to point to this particular code ura if you're doing deployment sort of in the more ci cd fashion you would actually specify here the name of the zip files that you output from your build essentially okay and if i go to s3 and show you the actual bucket you can see that there's a whole lot of paths here and i believe sort of the path was asp.net core code uri something something can just search for it directly so you can see that it's uploaded here and we just previously use it it's just so you can get a feel for where our deployment package goes in this whole shebang okay so we've looked at um sort of what happens to our um our packages we've seen the resources generated everything's working fine and dandy but now we get this very ugly url that gets generated for the api gateway and this is of course because we didn't necessarily tell the api gateway to generate anything nicer but also because yeah we would probably not want to use this particular url to give it to our clients and to customers because this has the disadvantage of potentially changing if we do certain modifications to our serverless template so it it would be kind of painful if we have to go and tell clients hey you know that the api endpoint i gave you um you're going to have to change it now so we would maybe like to output a nicer url than this and we can go into the details of how that will happen reaching the point of hosting okay as mentioned not not the greatest url there how about if we could maybe expose it in a custom domain given that i might already have a domain that i've purchased or that we want to purchase one from aws itself because it can actually do that so api gateway actually gives you the opportunity to set up custom domains for an api that you have and you can also set up different base path mappings sort of if you wanted to go not necessarily to a root sort of like example.com but you would like it to go to example.com api or slash i don't know values in the case of this particular api if we want to map it that way you can actually do that in custom domain but of course you need to have a registered domain name in order to do that and you can do this um in uh in aws itself by route 53 else we'll see or you can just purchase a regular domain name and use that and the idea is that okay you set up the custom domain on your api but the custom domain is not known by any dns server so if a client goes and inputs that url in your browser they're not going to be able to find it and this is where route 53 comes in which is essentially exactly a dns server and you can use this with variable various services either inside or even things outside of aws so you can just point it to various ips you can have cnc name record setup in there whatever you really need and the cool thing about route 53 is that being a part of the aws ecosystem of course it's meant to work with other aws services and there's a really nice way of setting up basically an alias to other resources like api gateways or load balancers or even other stuff by just setting up a record in or record or multiple ones in your route 53 in your hosted zone uh cool thing about route 53 is that you can have just simple routing just one to one i'm going to route this custom domain to my api gateway let's say or you can do routing based on a client's geographical location this means that if they're in a certain continent they will get routed to a resource deployed in a certain zone in an area and this goes further to sort of country level so if you're in a particular country you can get routed to a particular instance of your api and for the case of the united states i believe it also goes as deep as state level you can actually also set up routing based on the latency that a user has to a particular instance but what we're gonna do today for the sake of demonstration really is just to use a simple routing okay and for that you might be asking okay but what do i need to do to do this in my template well of course we're going to have a couple prerequisites one is of course having a purchase domain and the other is to have a certificate for your particular domain and we're going to need to have one created in the same region as our api gateway i didn't really go into very much detail about regions but they're all the most all of the resources in aws i should say better are region [Music] split by region so you won't have all the lambdas that you've created say in ireland showing up in your north america region and stuff like that and it's better to have sort of um resources work together in the same region due to latency and that sort of consideration what else we'll need to add to our template is the api gateway custom domain name uh base path mapping so to say okay from the api gateway where on our custom domain do we want this to map and of course around 353 dns records so it can actually be discoverable by clients in the world okay so let's exit out of the presentation and actually do that so for this i'm just going to expand a little bit here our um i think i forgot to mention that we we actually have some intellisense support here so we don't it is kind of painful to try and write templates by yourself which is why i'm going to use my handy dandy cheat sheet and i'm going to explain what's going on here in a second but you do actually get some intellisense so if i go to properties here and trigger it i'm hoping you might be able to see there's a little intellisense window and you get to see um sort of little properties that can be set up for any particular resource there's a json schema behind this just works nicely okay so you can see here that i've added some parameters these are the domain names and the certificate that we need to have on our api gateway in order to work with the custom domain name these are things that you will need to set up previously and the hosted zone id is sort of um an indication of which domain you're pointing to in route 53 if you're setting it up that way okay in terms of resources themselves the function doesn't change at all it remains exactly the same but we will see that we're adding a couple more resources from what we had before uh first one being the domain name for the api gateway we're saying that okay i'm i'm going to have a domain name type resources and on the property we're going to give the parameter using a reference to the parameter that we have up here and on the configurations we also have to set up the endpoint type which can be original or edge optimized i'm just going to go with regional for now because i don't necessarily need the cloudfront distribute distribution and all the other things that edgepoint that edge optimized can give you i don't really really need that right now and for the certificate again a reference to the parameter that we are going to give to the template that's the custom domain now for the base path mapping of course we have an api mapping type which will depend on our two previous resources and we're basically going to say that uh the api id that we get from the function itself will have this domain name and we'll point to this um stage of the api gateway and if i'm not specifying any other um custom routes here it's just going to go to the root of my domain we'll see that in a second after we redeploy the third and final resource that we're going to add is a route 53 record set which again depends on some previous resources that we have we're setting some properties related to the hosted zone and the same domain name that we've used previously and the record type will be a in this case you can use a or c name for just regular pointing of things but uh for a type records it's actually free of charge for cname you actually have to pay something and i think you can set them up in the domain apex so you can set them up in the example.com you would have to set them up for example.com something okay um we have the target for the alias so our gateway domain name and sort of the dns name that will will have in the route 53 console and we'll look at that when we deploy it and also in the output section besides the existing api url that we already have i've also added a friendly url just to make it easier to access which simply uses the domain name and the swagger so i don't have to type that afterwards okay now that we've seen kind of what we're doing let me actually show you how you can deploy using the toolkit so i'm just going to right click on my project i'm going to go and click on publish to aws lambda i'm not sure how well this is going to show up it might be a bit small but i don't think i can zoom you in very much i'm just going to choose the same stack name that we have previously and the same as three bucket and you can see here i hope that there is an option to save settings to aws lambda tools for future deployments and we have the specified parameters that you can check and we're just gonna go publish and if we go here to our stack and refresh we can see that there's an update in progress that we've initiated in our visual studio you can also see updates right here in in visual studio as it goes on while that creates because it's going to take a little bit a while i can also just a little bit of preview on sort of the explorer that comes with the toolkit in here you can have various resources that you can access like s3 dynamo all sorts of things including lambdas just so you know that's there if you ever need it and you're gonna have to give it a little second because it's creating new things okay so the update has completed great let's see what's happened extra since we last were here okay so we've created the api mapping that we specified and we have the domain name on the api gateway and if i look here amongst all the other things that were already existing we also see a route 53 a record set which is exactly what we specified in the template that we would want to happen also in the outputs you can see it now that there is a friendly url that's uh nice and friendly i'm going to leave that to the end you know just build up the suspense to see if it will work or not i hope it will work maybe but first of all let's let's go to the api gateway and see what has changed in terms of the api itself nothing much has changed we're still going to have a proxy that goes to the lambda and comes back but the difference is in the custom domain name that we've created which previously didn't exist and you can see that this is pointing to a demo.dormanco.net this is the hosted zone for the domenco.net domain which i actually do own no you can't have it and we can also see the api mapping so i'm going to say that the demo api which is in the api gateway with the prod stage goes to sort of the root path that's what essentially path none means and if we go to route 53 i can first show you that i have a registered domain um this i've registered previously actually a while back and the reason why you wouldn't necessarily put the register domain i told you this would happen so i warned you i did warn you uh the reason why you wouldn't put the register domain as a resource in your serverless template is because this takes quite a bit of while to create and you probably wouldn't want to delete it if you delete the stack or things of that nature so that's why there are certain things that you might consider putting in a different serverless template than your api stacks or application sets or stuff like that so i already had that we can go to the hosted zone that corresponds to this domain so you can see here and we can see the records that i already had the first two records are things that aws sets up for you which are basically name servers that will respond to your domain to respond to queries the cname record you see here is a disappointing to the certificate that i had for my domain and the last but not least little thing is the record that we've set up in our in our stack that we've just added 2.2 demo.dorminco.net and you can see the writing policy is a simple one the type is a which is exactly what we wanted so it looks like everything was created okay let's go to the outputs and see if it will run so you can see i'm on demodormanco.net my connection is secure because i have my valid certificate it seems to load and it actually runs yay congratulations we have a deployed and hosted api and with a friendly url to boot cool so just a second okay uh that's kind of the whole flow that i wanted to guide you through tonight um you can have a look at the repo link there for the actual example project including the serverless template and the slides for tonight and i've also linked a bunch of resources and documentation from aws that should hopefully help you in getting started and yeah the time for a q a well we have.net 5.0 i'm assuming you mean for lambdas um currently there is uh lambda support for net core 3.1 and as far as i know there is an issue opened on the aws.net repo and they're looking uh into providing support for 5.0 even though it's not long-term support i believe net six will be long-term support but they they did agree that it's good it's going to be quite a long time to for people to wait until 2021 and probably a few months after it actually comes out so they're probably gonna work on something for net five as well julia i have a question oh god yes are there any concerns that we need to be aware of when we are deploying um in this way that instructions today basically serverless and uh using the linux environment and so forth uh sure there's a lot of things to be aware of including where you store your credentials to access your account so everything you saw there was me interacting with my personal account and that means some credentials set up on my local machine and you need to be careful what you do with them even if you have some a user that's only used for ci cd you need to make sure that those credentials are safe because if someone takes over them they could potentially have their way with your aws user and do whatever they want in your account also be mindful of runtimes and maybe think about running tests over a particular runtime if you're worried that some functionality might be affected you might be surprised sometimes there are options to run tests so to use net tests with their particular runtime that's something to keep in mind as well and also obviously keep uh thinking if the serverless platform is okay for your particular um sort of business need is it going to fit with what you need to do are there things that are very long running very costly um think about so sort of lambda is um cost per processing i believe so the more requests you process and the more time the lambda is running you need to pay for that so if your customer is not going to have money to to keep up you might need to consider that i hope that answers your question yeah yeah yeah thank you thank you you're welcome i i can go on in uh we have two more questions on the chat one from gennady so some pros for aws versus asia4.net uh pros for aws well aws being in the market for a bit longer than azure as we know might be a bit more mature in certain features although i think asia has definitely sort of got up to speed in recent times and they have a lot of the basic stuff that you would want maybe even beyond um yeah if you maybe want to have other technologies in your ecosystem of applications sort of you want to run something on node.js or java or other things or python or i don't know and you want to have something that's very non-homogeneous in terms of technology you can always go to aws and easily go for it personally one drawback that i do find with aws is their documentation is rather hard to sift through sometimes okay and we have another one uh stefan hey stefan uh do you know there is a similar library comparable to durable functions in azure that works for aws uh it's been a while since i've worked with azure actually in durable functions so i'm not sure if there is anything but i can maybe do some research and get back to you thank you so much for taking time out of your evening to be here with us and please keep an eye out on the meetups group for max code because i know there's some other presentations coming your way in the next coming months and thank you so much for being here you
Info
Channel: Maxcode
Views: 871
Rating: undefined out of 5
Keywords: learning, maxcode, dotnet, aws, dotnetcore, api, restapi, maxcodemeetup
Id: CWgF0KV8XGU
Channel Id: undefined
Length: 47min 30sec (2850 seconds)
Published: Mon Nov 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.