Launch an AWS AMI with Ubuntu 18.04 LTS, .NET Core 3.1, Kestrel, and Nginx

Learn how you can easily launch and configure a .net core web server running kestrel and nginx, and easily configure it to work with AWS code deploy

Launch an AWS AMI with Ubuntu 18.04 LTS, .NET Core 3.1, Kestrel, and Nginx

Creating, configuring and launching a new instance to host your .net core web app can be a bit challenging. Fortunately, we have done all the hard work and made it a snap for you. Our Ubuntu AMI allows you to easily stand up an instance, and we tell you how to configure your appspec.yml file so AWS Code Deploy handles deployments for you automatically.

The Basics

Launch your JebteK Ubuntu .NET AMI. The AMI is configured to use Nginx as a reverse proxy to front the .NET Web Server Kestrel. Essentially, Kestrel listens on port 5000, and Nginx listens on port 80 and forwards all traffic to Kestrel.

Once you launch your instance, connect to it via ssh. On a mac, you can use the command:

ssh -i <keyfile.pem> ubuntu@<instance public ip>

Test your instance

Run the command ~/install-test-site.sh and then navigate to your instance's public ip address, and you should see the message JebteK AMI Test v1.0. After confirming, run ~/clean-site.sh to delete the test files and install your own code. If the site won't load, ensure you have port 80 allowed in the AWS EC2 firewall rules.

Configure Kestrel

Kestrel is setup to launch as a service, and systemctl will ensure it keeps running. All you need to do is set the name of your project dll. To do this, type:

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

Change JebteK.AMI.dll at the end of ExecStart to the name of your web app's dll file. Remember, this is case sensitive!

Then press Ctrl + O and then Ctrl + X to save and exit.

Upload your deploy package

Upload your deploy files to /var/www/dotnetapp. The easy way to do this is to zip your files, copy them to the home directory, and then run the command:

sudo unzip ~/<zipfile.zip> -d /var/www/dotnetapp.

Reload the systemctl daemon

Reload the demon by executing the following command:

sudo systemctl daemon-reload

Start Kestrel

Start Kestrel by executing the following command:

sudo systemctl start kestrel.service

Test your webapp

Navigate to your instance's public ip. You should see your app's home page!

Optional: Configure FQDN for Nginx

The FQDN is the part of the URL that has the host and the domain name. For example, for the site /, the FQDN is www.jebtek.com. The default configuration has Nginx responding to all FQDN requests, but for added security you can lock it down to just the one you will be hosting on the server. To do this, type:

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

Replace the _ after server_name with your FQDN. Then press Ctrl + O and then Ctrl + X to save and exit.

Optional: Configure your app for AWS Code Deploy

You're done configuring the instance! Next, let's setup your project so AWS Code Deploy can automatically deploy your code. To do this, you will need to add three files to your project. The first file, appspec.yml, will contain the instructions for Code Build. The second file, stop_server.sh will stop Kestrel before a deploy, and finally the third file start_server.sh will start Kestrel after the deploy is complete.

The listing for the file is provided below. The deploy file is a zip file that contains your application code inside the zip file, in the path /your-app/bin/build_output. You can adjust that path as needed. The two other files that need to be in your deploy zip file are /scripts/stop_server.sh and /scripts/start_server.sh. Those listings are also provided below.

That's it!

You're all done! When you build and deploy your project, create the zip file in the format specified, you can upload it to AWS Code Deploy and have it automatically deploy to your instance!

What's next?

In another post I'll describe how you can use create a full pipeline to easily deploy code in an always-on production environment.