Using Amazon Lightsail for ASP.NET Core

Daniel Sagita
6 min readOct 6, 2018

--

Amazon Lightsail

Amazon Lightsail is a new product from AWS that allows you to create a virtual private server for some cheap price. This is perfect for web servers, developer environments, and small database use cases. So if you are in need for bigger one, you can switch to using AWS EC2 instead.

To be precise, the product is defined as “burstable” in the FAQ. Although there is no clear specification regarding the “burstable”, it should have similar definition to the burstable instance in EC2 where this instance will accumulate a CPU credit that you can spend when the instance use its CPU power for a certain amount of time. And so this kind of instance fits perfectly for low-traffic web server that I’m trying to use.

Creating the Lightsail Instance

You will need to have an AWS account and already set up your payment information. If you don’t have your payment information set up, you will not be able to create a new Lightsail instance.

From the AWS Management Console, now exists a new navigation to the Lightsail service which has that icon that shows that it is an external link / product.

AWS Management Console

If you navigate from the Lighsail link, you will then be greeted with the Lightsail dashboard.

Amazon Lightsail Dashboard

Creating a new instance is pretty straightforward, from choosing the base image that you want to use for your instance (Windows / Linux).

Amazon Lightsail Instance Image

For this article, we’re going to stick to “OS Only” and use Ubuntu 16.04. You then need to setup your SSH Key for connecting to the instance later on. You can use the default that is provided by Amazon, or create a new one. I’m just going to use the default one for now.

You can then choose the instance size which come as low as $3.50

Amazon Lightsail Instance Plan

Connecting to the new instance

After creating the new instance, you can then use any SSH tools to connect to it. If you’re coming from Windows, like me, you can use Putty to connect.

You will then need to input the IP address.

Input IP address in putty

And then use the SSH key that you setup/downloaded earlier.

Input SSH key

The user for login will be “ubuntu”.

Connecting to Linux using PuTTY

If you use the default SSH key, it can be downloaded from your account page.

Download default SSH key

Setting up the instance

After connecting to the instance, we can setup the instance. For our purposes, we use nginx to serve the HTTP request.

sudo apt-get update
sudo apt-get install nginx

You can test if the nginx is installed successfully by browsing through the IP address that is provided from the Lightsail instance management.

Successful installation of nginx

Installing ASP.NET Core

Let’s try to install ASP.NET Core on our Ubuntu instance using this documentation.

You will need to register the Microsoft key, register the product repository, and install required dependencies. This only needs to be done once per machine.

sudo wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.debsudo dpkg -i packages-microsoft-prod.deb

Install the .NET Core Runtime

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install aspnetcore-runtime-2.1

Setting up ASP.NET Core

Let’s try setting up ASP.NET Core using the documentation.

Make sure that you have your ASP.NET Core application that has been published using dotnet publish -c Release

ASP.NET Core publish output

Next is to copy the ASP.NET Core output files to our instance. You can do this using any FTP tool that is out there. Connect using SFTP protocol on port 22 and provide the SSH key that is created earlier when creating the instance.

The default location for nginx files is in /var/www/html and we might put our files in the /var/www folder as well. These folders are owned by the root user and so we might need to create the folder using sudo in the terminal. After that you will need to change the folder owner to ubuntu as this user is what we are using on the FTP connection.

sudo mkdir /var/www/myaspnetcore
sudo chown ubuntu:ubuntu /var/www/myaspnetcore

When you have setup the target folder, you can just copy using the FTP tool of your choice.

WinSCP for FTP transfer

Configure nginx for ASP.NET Core

Microsoft recommend that we use a reverse-proxy server that will forward the request from the external client to the ASP.NET Core Kestrel. And so we need to configure nginx to point to the ASP.NET Core Kestrel that usually run on port 5000. We do this by editing the nginx config file.

sudo nano /etc/nginx/sites-available/default

And replace the content with the following :

Setup the root to the wwwroot folder that is inside of your published ASP.NET Core folder.

One thing to note is that the line proxy_set_header Host $host;in the Microsoft documentation will make the querystrings for the request that is going to be forwarded to Kestrel to be removed.

And so we must use this line instead proxy_set_header Host $http_host;

Setting up monitoring for the ASP.NET Core App

Our nginx is set up to forward requests made to http://<serveraddress>:80/ to the ASP.NET Kestrel on http://localhost:5000

And so we can use systemd to start and managing the ASP.NET Core process.

Create the service file

sudo nano /etc/systemd/system/myaspnetcore.service

Replace the content with the following :

Please note that you need to change

  • Description to be your services description
  • WorkingDirectory to be your ASP.NET Core folder destination
  • ExecStart to be your ASP.NET Core assembly file location
  • User to be “ubuntu” for default. But it should be changed to whatever user that you have setup in your instance that can execute the ASP.NET Core assembly

Enable the service and start the service. You can also verify that the service is running.

sudo systemctl enable myaspnetcore.service
sudo systemctl start myaspnetcore.service
sudo systemctl status myaspnetcore.service

You will get result similar to this.

ASP.NET Core running using systemd

Last one is to restart the nginx to pick up our newly configured ASP.NET Core and nginx

sudo service nginx restart

Verify that the ASP.NET Core process is running by browsing the IP address specified on the Lightsail instance management.

Editing your application

Let’s try to change our banner to show “My ASP.NET Core” label instead.

Start by changing things in your ASP.NET Core project in the Index.cshtml

Index.cshtml

Publish your application by running this command from the terminal in your .csproj location

dotnet publish -c Release

Transfer the publish output file to your instance and replacing the old files.

After this, the web page won’t change automatically because the ASP.NET Core is run using systemd, so you need to restart the service.

sudo systemctl restart myaspnetcore.service

If you then browse to the IP address, you will find the new home page.

Edited home page

Summary

We have successfully created a new instance using Amazon Lightsail. We have also setup ASP.NET Core to be run using nginx. Updating your ASP.NET Core application is as simple as running a command and copy paste it to the instance.

--

--