AWS Application Deployment Basics: .NET Core Apps
.NET Core Application and AWS Serverless Application Templatew
In the previous two posts in this series, we have set up a VPC with public and private subnets, launched EC2 instances. Setup PostgreSQL database and deploy a Nodejs application which we served via NGINX reverse proxy mechanism.
We have the following architecture in place:
If you are new to these topics, you can first check the previous posts on the following URL and then it will be easier to follow the contents in the current post:
- AWS Applications Deployment Basics: Part 1
- AWS Applications Deployment Basics: Part 2 (Setup NGINX)
.NET Core Application and AWS Serverless Application Template
.NET Core is cross-platform and this makes it very easy to run on Ubuntu. We can build a .NET Core application and publish it for ubuntu.
AWS Lambda is a serverless offering and AWS SDK makes it very easy to build .NET Core web applications which can be later deployed to AWS Lambda. I have a plan to cover serverless offerings from AWS later in this series. However, today, we will not go into those details.
As I have this concern (of going serverless in the future) so I build a .NET Core WebApi application using AWS Serverless template (though we will be deploying it as a normal .NET Core Application on EC2).
The application is very basic and you can download the source code from the git repo on this link. We will not get into details of this application code today and if you have your own .NET core application, I will encourage you to instead use that one to solidify the learning.
Here is what the solution looks like:
App has a ProductsController which returns a list of products from the database and we will use it for our demo:
and here is the database ConnectionString:
We are using the exact same database which we set up in previous posts, but you can use a different one if you like.
And here is the corresponding Startup.cs Code with database ConnectionsString:
and ProductsRepository is using Dapper for the data access:
Now, we want to deploy and run this application on the same EC2 instance (Ubuntu) where our Nodejs application and Postgres database are already running.
Publishing .NET Core Application for Ubuntu
First, we need to publish our .NET core application. We use the following command for this purpose:
dotnet publish "AWSServerlessDemo.Web.csproj" -c Release -r ubuntu.20.04-x64
It will publish the output in a directory as shown below:
Now, to easily transfer these files to Ubuntu EC2, I copied the published folder to a different git repository, which I can git pull on the Ubuntu EC2.
Run the Published Application
I have cloned the repo (published app) on target EC2:
git clone https://github.com/jawadhasan/awspublishapp.git
Then cd into the directory and try to run the application and here is the output of this attempt:
We can set permission using the following command:
chmod 777 ./AWSServerlessDemo.Web
Then try again to run the application:
and this time, our application is running. Let's run it in the background, so the bash prompt is not blocked:
nohup ./AWSServerlessDemo.Web > /dev/null 2>&1&
Notice this command also gives us a process-id (1471) which we can use later to identify and kill if we want to stop/restart our application running in the background:
You can list/kill the
nohup process using the following command:
ps -ef //show list
kill 1471 //PID to kill app
Let’s try to access the products API:
and nothing is returned.
Now open the environment file on EC2 with the following command:
sudo nano /etc/environment
and add the following Authority and DefaultConnection items:
CTRL+X to Save and close the file.
Next, Kill the process:
and Restart the application:
if we try to access products API, and you can see that this time, we are getting data from the database. This is exactly the same data, we were getting using the Nodejs application in the previous post.
Like we did with the Nodejs application in the previous post, let’s configure NGNIX to reverse proxy to the .NET core application. Remember, our NGNIX is on a public EC2 instance (check the previous post for details).
sudo nano /etc/nginx/nginx.conf
Add a location entry for .NET Core application:
sudo service nginx restart
Now, if I try to access the application and because we configured that webserver can be reached from anywhere in the world:
Our application is up and running.
Here is our architecture with the .NET Core application running:
In this post, we saw a simple way to deploy your .NET Core application on AWS. You may have noticed that this process was very similar to the NodeJS application on a certain level. Now, if we want to deploy a JAVA application, it will be very similar.
The same goes for databases. Instead of PostgreSQL, we can instead use MySQL database or the likes. We can also bring AWS RDS in the mix if we want.
Let me know if you have any questions or comments. Till next time, Happy Coding!
Hi there! I’m Jawad and I’m a software solutions Team Lead , Cloud Architect and AWS Community Builder. I live in Dusseldorf, Germany, have a great interest in books and movies, and Astrophysics as well. If you want to know a bit more about me, here is my blog you can check: http://www.hexquote.com