{"id":304486,"date":"2021-07-21T13:00:30","date_gmt":"2021-07-21T10:00:30","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway-cloudsavvy-it\/"},"modified":"2021-07-21T13:00:30","modified_gmt":"2021-07-21T10:00:30","slug":"how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway-cloudsavvy-it\/","title":{"rendered":"#How To Run An ASP.NET Web API on AWS With Lambda and API Gateway \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a26bb7c1cb2a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a26bb7c1cb2a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway-cloudsavvy-it\/#Wait_You_Can_Do_This_On_Lambda\" >Wait, You Can Do This On Lambda?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway-cloudsavvy-it\/#Setting_Up_ASPNET\" >Setting Up ASP.NET<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway-cloudsavvy-it\/#Using_Your_API\" >Using Your API<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To Run An ASP.NET Web API on AWS With Lambda and API Gateway \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-516\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/58095fd3.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/58095fd3.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/58095fd3.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"lambda logo\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>AWS Lambda is a serverless platform for running code without managing the underlying hardware. It\u2019s very flexible, and can run many different workloads, including full C# APIs using ASP.NET Core.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wait_You_Can_Do_This_On_Lambda\"><\/span>Wait, You Can Do This On Lambda?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Yep! Not only can you run functions based on .NET runtimes, you can respond to requests using all the tools provided to you from ASP.NET. You can create APIs that talk to databases, like AWS\u2019s managed RDS database, all while being perfectly scalable running on serverless functions.<\/p>\n<p>While previous versions of ASP.NET run on .NET Framework (the older, Windows-only runtime) have been known to be bulky, the new ASP.NET Core stack running on .NET Core 3.1 and the newer .NET 5 have made significant performance and memory usage improvements.<\/p>\n<p>This wouldn\u2019t normally be possible, as ASP.NET uses its own HTTP web server called Kestrel to respond to requests, which wouldn\u2019t work as that is handled by the Lambda runtime. However, AWS has provided an ingenious fix for this; traditionally, an ASP.NET setup usually involves their Kestrel web server behind IIS or NGINX. This talks to the ASP.NET framework to handle requests.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12801\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/06d33702.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"639\" height=\"234\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>AWS has created a proxy class, <code>Amazon.Lambda.AspNetCoreServer<\/code>, which takes care of everything in front of ASP.NET. This lets you reuse most of your code while bridging your API to Lambda.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12800\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/2a11f4e8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"695\" height=\"225\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This means you will need to use API Gateway, but that\u2019s not a bad thing as API Gateway is very useful for managing your API. It allows you to strictly define all the rules that make your API function; of course, you will need to have your ASP.NET <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a> configured to handle all requests from API Gateway.<\/p>\n<p>However, this isn\u2019t going to be the greatest experience in terms of startup time. Compared to lightweight scripting languages like JavaScript and Python, .NET packages have much more overhead.\u00a0 There are <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/medium.com\/slalom-build\/solving-cold-starts-on-aws-lambda-when-using-dotnet-core-51f244f08f60\">some tricks you can do<\/a> to speed up performance, and you can even <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/aws.amazon.com\/blogs\/compute\/new-for-aws-lambda-predictable-start-up-times-with-provisioned-concurrency\/?tag=reviewgeek-20\">pay for it with provisioned capacity<\/a>. Otherwise, you should be prepared for cold startup times around 1-2 seconds.<\/p>\n<p>This doesn\u2019t mean every execution is going to take 2 seconds to load the page. Once the first load happens, everything is initialized, and it\u2019s kept \u201chot\u201d in Lambda for 5 minutes. If someone else requests it, the function will handle the request like it normally would running on an actual server.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Setting_Up_ASPNET\"><\/span>Setting Up ASP.NET<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>AWS includes a generator for ASP.NET Lambda projects that is pre-configured with the boilerplate code and deployment to CloudFormation. We recommend that you start here, test things out, and then move your API code over, but if you\u2019d like to jam it into an existing project, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/aws.amazon.com\/blogs\/developer\/net-core-3-0-on-lambda-with-aws-lambdas-custom-runtime\/?tag=reviewgeek-20\">AWS has a guide for that as well<\/a>.<\/p>\n<p>You\u2019ll need the AWS Toolkit for Visual Studio extension installed, which you can manage from \u201cExtensions\u201d in the menu bar. This is what contains the project templates for AWS applications.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12823\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/229ee30b.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"292\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>From the main Visual Studio splash screen, create a new project:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12824\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/a1f7cbbc.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"375\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019ll probably want to put this in its own solution, so select \u201cBlank Solution\u201d under \u201cOther.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12825\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/209e685f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"578\" height=\"217\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, you can right click in the file pane to add a new project, and select \u201cAWS Serverless Application,\u201d or \u201cAWS Serverless Application with Tests,\u201d whichever you prefer.<\/p>\n<p>Make sure this is in C#, unless you want to use F# for some reason.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12817\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/901dcb3e.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"350\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Also keep in mind that this is a \u201cServerless Application\u201d project, which manages all the resources through AWS\u2019s infrastructure-as-code service, CloudFormation. If you just wanted to make some Lambda functions, there\u2019s a project for that too.<\/p>\n<p>You\u2019ll be taken to a sub-menu where you can choose what type of blueprint you want to build. Select \u201cASP.NET Core Web API\u201d and create the project.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12818\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/cc1fdb6b.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"324\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>For the most part, this is configured like a standard ASP.NET project. The main difference is that the traditional <code>Program.cs<\/code> is replaced by\u00a0<code>LambdaEntryPoint.cs<\/code> as the main entrypoint, and it contains the shim class that bridge\u2019s AWS\u2019s code to ASP.NET\u2019s <code>IWebHostBuilder<\/code>, which is used to start the application.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12826\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/a09bcf20.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"343\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once it\u2019s all up and running, you will need to copy over your controllers, models, and services, and replace <code>Startup.cs<\/code>\u00a0with your configuration.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Your_API\"><\/span>Using Your API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To deploy this project, AWS includes built in publish options using the AWS Toolkit Extension. Right click your project and select \u201cPublish to AWS Lambda\u2026\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12819\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/6b4fd909.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"446\" height=\"251\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019ll need to give it a bucket to upload to, and set a name for the CloudFormation deployment.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12820\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/e21e02be.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"466\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>It will take a second to upload and publish, but you\u2019ll then be able to go to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/console.aws.amazon.com\/lambda\/home?tag=reviewgeek-20\">AWS Lambda Management Console<\/a> to view your function. It will have an auto-generated name using the CloudFormation stack name as the prefix.<\/p>\n<p>Under Configuration &gt; Triggers, you can view all the API Gateway triggers that call this function, and test it out for yourself using the endpoint.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12821\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/fe6d014c.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"563\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can also view the full CloudFormation stack that is created automatically using your configuration.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12822\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/72578d7a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"253\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you want to change it, you\u2019ll need to edit the <code>serverless.template<\/code>\u00a0in your project. For more info, you can refer to our guide on using AWS CloudFormation.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>Codify Your AWS Infrastructure With CloudFormation<\/em><\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/12795\/how-to-run-an-asp-net-web-api-on-aws-with-lambda-and-api-gateway\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Run An ASP.NET Web API on AWS With Lambda and API Gateway \u2013 CloudSavvy IT&#8221; AWS Lambda is a serverless platform for running code without managing the underlying hardware. It\u2019s very flexible, and can run many different workloads, including full C# APIs using ASP.NET Core. Wait, You Can Do This On Lambda? Yep!&#8230;<\/p>\n","protected":false},"author":1,"featured_media":304487,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/58095fd3.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-304486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/304486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=304486"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/304486\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/304487"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=304486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=304486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=304486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}