Introduction
In previous two posts in this series, we have setup 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 plan to cover serverless offerings from AWS later in this series. However, today, we will not go in those details.
As I have this concern (of going serverless in 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 the solution looks like:
App has a ProductsController which returns list of products from the database and we will use it for our demo:
and here is the database ConnectionString:
We are using the exactly same database which we setup in previous posts, but you can use a different one if you like.
and here is 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 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 easy 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 can set permission using following command:
chmod 777 ./AWSServerlessDemo.Web
Then try again to run the applicaiton:
and this time, our application is running. Lets 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 background:
You can list / kill the nohup process using 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 again:
if we try to access products API, and you can see that this time, we are getting data from the database. This is exactly same data, we were getting using Nodejs application in previous post.
Configure NGNIX
Like we did with Nodejs application in previous post, let’s configure NGNIX to reverse proxy to .NET core application. Remember, our NGNIX is on public EC2 instance (check 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 world :
Our application is up and running.
Architecture Diagram
Here is our architecture with .NET Core application running:
Summary
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 NodeJS application on certain level. Now, if we want to deploy a JAVA application, it will be very similar.
Same goes for databases. Instead of PostgreSQL, we can instead use MySQL database or the likes. We can also bring in AWS RDS in the mix, if we want.
Let me know if you have some questions or comments. Till next time, Happy Coding.
My Recent Books
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.
1 thought on “AWS Application Deployment Basics – .NET Core Apps”
Comments are closed.