How do I deploy a .NET core application on AWS EC2?

Share

In this article, I will take you through how to deploy your first test .NET Core application to AWS EC2. 

I wanted to try deploying my application to aws to for testing purposes. As it was known to me, ECS would be a good way to get started on serverless deployment. But afterward, I came to know that ECS is not a free service if you are deploying your application using AWS Free Tier.

Then chose to go with EC2 as it was just for testing purposes. You need to create an EC2 instance and start running your docker container inside it. Following are the details.

Create EC2 Linux Instance:
  1. Go to EC2 dashboard >> Instances >> Launch Instance
  2. Select the machine and version
  3. Select the instance type as t2.micro which is the lowest configuration type available
  4. Create new key-value pair and download the .pem file
  5. Then create the instance
Connect to EC2 through SSH:
  1. Go inside the instance
  2. Click on Connect button
  3. Go to the SSH option
  4. Copy highlighted command
  5. Open your terminal and go to the path where your download key-value pair .pem file is located
  6. Paste and run the copied command that will connect to the server

Install Docker on Instance:

  1. Visit https://docs.docker.com/engine/install/ubuntu/
  2. Apply commands mentioned in the above document to the connecter server.
  3. It will install the docker on your server.
  4. You can check the installed docker with the command >> docker –version
Upload Docker Image to ECR:
  1. Now build your app’s docker image on your local machine
  2. In AWS Console, go to Elastic Container Registry
  3. Create Private repository 
  4. Login to aws through aws CLI and push your image to a registry
Pull Docker Image into the instance and Run:
  • Now switch to the terminal where you are already connected to an EC2 instance
  • Again login to the aws through aws commands and pull your image from the ECR registry
  • Run the image using the docker run(with option) command
  • Your app will get open when you visit the public IP

Note: If your app uses ports other than 80(default HTTP) and 443(default HTTPS) ports, create a new inbound rule to allow all traffic on the new port.


Share

Leave a Comment