How to build a serverless website for 30 minutes using AWS and .Net Core

10/9/2019

You can’t imagine how easy it is to create a website these days. Definitely. Yeah, probably it is hard to choose the right technologies, or to get this choice for you – there are a heavy bunch of frameworks and technologies. But, using the right combinations of them will make our work much simpler.

In this tutorial, I want to show you how easy it is to create and deploy a serverless application in AWS using Lambda, .NET Core and the related plugins.

 

What we need

Visual Studio 2019, AWS Toolkit for Visual Studio, custom domain name, AWS account.

 

Steps

First step – domain preparation. You can buy a custom domain name from AWS or use another one with different registration but in this case, you need to create a hosted zone for it and update DNS settings in your provider.
In our case, we will register a domain with AWS, which automatically creates a hosted zone for us.

Go to Route 53, Registered domains, and click register.

 

Next step – SSL certificate.

 

Go to Certificate manager and change your region to North Virginia. Click register certificate, request a public certificate, and put your domain name.
If you choose DNS validation, go to the hosted zone and edit the TTL value for all your records.

By this way, validation will be faster. After that, return the previous values in the records.

 

In the main panel, under your domain, click Create record it Route 53.

add-record-to-route-53

It is time to create a simple application. Go to https://aws.amazon.com/visualstudio/ and download the AWS Toolkit for Visual Studio. After it is installation, create a new project in VS and type aws…

create-aws-project-in-visual-studio

Choose AWS Serverless Application with .NET Core and c# and from the blueprint examples choose .NET Core Web App

aws-visual-studio-blue-prints

Built the application and ran it. It is the default AWS Serverless start page. We have the basic application now; the customization depends on you, and now let's deploy it. Right-click on the project name and select Publish with AWS Lambda.

publish-with-aws-lambda

 

{few words for the initially AWS toolkit configurations}
It is time to publish it.

Choose related configuration, type Stack name and create a new S3 bucket for your app. Click Publish.

publish-aws-app-project-configuration

 

Copy the serverless URL and test it in the browser.

api-gateway-serverless-url

 

We are serverless!!!

we-are-serverless

 

The last step is to set our custom domain name to this Lambda function, so our site will have a much better URL.

Go to API Gateway.

set-custom-domain-in-api-gateway

 

Choose Custom Domain Names from the right and click to create a new one.

Set your domain name and certificate.

set-api-gateway-custom-domain-name

 

You need to wait a little bit.
After your custom domain is added to API Gateway, click to Edit and add Mapping

api-gateway-edit-custom-domain-mappingapi-gateway-edit-custom-domain-mapping-2

 

After several minutes, your domain will be refreshed from the DNS cache and will be available. Don't forget to return the previous TTL setting in the domain hosted zone. For NS is 2 days and for SOA is 10 minutes.

edit-hosted-zone-record-set-aws

 

We need to configure one more thing. Go to the AWS Route 53, hosted zone and choose your domain hosted zone. You should create several “A” records for the domain.

Copy the target domain name from the custom domain record.

api-gateway-custom-domain-name-target-domain-name

 

And use it as an alias in the hosted zone records. I usually create these “A” records:

  • se
  • *. se
  • motiviram.se
  • motiviram.se

add-aws-hosted-zone-recordsadd-target-domain-name-to-hosted-zone

 

Our app is live!!! 😊

our-aws-app-is-live

 

Conclusion

It is really easy to create and deploy a simple website using .NET Core and AWS. But what are the next steps? API Gateway doesn’t support binary data (images, videos) by default, so you will need to do some configurations if your app will use images – check this article - https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html.
Or you can serve it from S3 bucket and AWS Cloud Front (CDN).

See you next time.