Disclaimer: I am a consultant at Amazon Web Services, and this is my personal blog. The opinions expressed here are solely mine and do not reflect the views of Amazon Web Services (AWS). Any statements made should not be considered official endorsements or statements by AWS.
In this post, we will understand how can we deploy an ASP.NET Core application on an Amazon Linux 2 EC2 instance step by step.
In the first step, you have to create an Amazon Linux 2 EC2 instance. Though, in this post, we won't focus much on creating a Linux EC2 instance step by step. But there are certain things that you must know while provisioning the EC2 instance.
Select the Amazon Linux 2 AMI as shown in the below picture. For CPU architecture, you can choose either x86 or ARM. In this post, I have chosen x86 as ARM instances were not available in t2 or t3 instance family.
There are a lot of Linux distributions such as:
Microsoft has published documentation on how to install .NET Core on each of these distributions.
To install the .NET Core runtime, you first have to check the Linux Distribution used by Amazon Linux 2.
Run the below command using SSH to check the Linux distribution.
As you can see, the above command outputs ID_LIKE
property with centos rhel fedora
value. Considering this AMI similar to CentOS, we can proceed further installing .NET Core on Linux CentOS distribution.
To know more about ID_LIKE property, please refer this link.
In this step, we will be following the documentation from Microsoft website to install the .NET Runtime on CentOS.
Before you install .NET Core Runtime, run the following command. This will add the Microsoft package signing key to the list of trusted keys and add the Microsoft package repository.
Next, run the below command to install ASP.NET Core 6 Runtime.
You can see below, that ASP.NET Core 6 Runtime has been installed successfully.
Let's create a new sample application. Here, I am creating an MVC application.
While publishing, make sure the deployment mode is Framework dependent, and Target Runtime is linux-64.
Create a Zip file of the published content as shown in the below picture.
Upload this Zip file to S3 bucket.
SSH to the EC2 and run the below commands to create a directory for your web application.
Create a pre-signed URL by selecting the zip file from the console.
Before you proceed further, make sure your current working directory is /wwwroot/my-mvc-app
, as you will be downloading the zip file and unzipping it there only.
Run the below command to download the zip file in the current working directory.
Run the below command to unzip the published web app.
Run the below command to view unzipped files of the MVC web application.
See the below screenshot, all the files are now present inside /wwwroot/my-mvc-app
directory.
In this step, you will be running the application without creating a linux service. Though, it is not the recommended way to host a Web Application without creating a service, as your application process will be lost if the linux instance restarts due to any reason. Creating a Linux Service for the application guarantees that a Web App will be up and running when the system re-starts.
Hit the below command to run the ASP.NET Core application on port 80.
See the below screenshot.
Visit the public IP of EC2, you can see the app is up and running.
Running a web-app as a Linux service ensures that web-app will be up and running always as the Linux service restarts automatically after a reboot or crash using systemd.
Create the service definition file:
You can find running Linux service under path /etc/systemd/system.
Next, copy the below content in this service file.
Now, start the service.
The above command will start the service, and the service will start the ASP.NET Core app with the Kestrel server that is listening to the port 80. You can now try accessing the app from public IP.
See below screenshot of the running active service.
To redeploy your app with the changes, you need to stop the service, replace the DLLs, and start the service again. You can use the following commands to stop and start the service again.
In this post, we understood how easily we can deploy an ASP.NET Core application on an Amazon Linux 2 EC2 instance. Please let me know your thoughts and feedback in the comment section below.
Thank You ❤️