How to Deploy .NET Apps to Containers on AWS?

As a.NET developer, you may be unaware of AWS’s dedication to the platform, including developer advocates, specialized APIs for your.NET apps to easily interact with AWS services, and even some great tooling for our IDEs. The AWS Toolkit for Visual Studio interacts with a wide range of AWS services, whereas the AWS Toolkits for Visual Studio Code and JetBrains Rider focus on serverless features. Toolkit can be used to deploy.NET apps to fully managed AWS containers without leaving Visual Studio.

Elastic Container Repository (or ECR, a service for storing Docker images) and container orchestrators Elastic Kubernetes Service (EKS) and the proprietary Elastic Container Service (ECS), both of which integrate with ECR, are available on AWS.

On the other hand, if you don’t want to think about how those orchestrators work, AWS Fargate is your service.

It takes care of pushing images, spinning up containers, creating load balancers, and managing container instances for your flutter app development after providing configuration information. It’s a very simple method for getting your workload up and running.

In this article, you’ll learn how to publish a simple ASP.NET Core application to Fargate using the AWS Toolkit for Visual Studio. We’ll start with a template ASP.NET Core Web application and add some text output to the default page. Then, using the toolkit, we’ll deploy it to Fargate and examine the application running in a pair of AWS containers.

  • Requirements

Although.NET 5, ASP.NET Core 5, and EF Core 5 are on the horizon, and we’ll stick with Visual Studio 2019 and the 3.1 versions of these frameworks.

Using Linux containers’ default setting, Docker Desktop must also be installed and running on your computer.

As part of the.NET Core cross-platform development workload, your Visual Studio installation should include the Container Development Tools component.

The AWS Toolkit for Visual Studio must also be installed. 

If you do not already have an AWS account, you can create one after installing the toolkit, and the toolkit will guide you through creating a user role and associating the toolkit with that role. This grants the toolkit permission to carry out your developer tasks on AWS.

Creating the ASP.NET Core Application

  • Step 1: Creating the ASP.NET Core Application

First, in Visual Studio, create a new ASP.NET Core Web Application project. Our handle is ChocolateEatersAnon.

The next step is to decide what kind of web app you want. Make sure that.NET Core and ASP.NET Core 3.1 are selected in the top dropdowns, then select Web Application. Leave the default “Configure for HTTPS” and “Enable Docker Support” checked, and then click Create to finish creating the project.

The template will generate a straightforward web application with two pages: index and privacy. Because we enabled Docker support, a docker file exists that instructs Docker on how to create a Docker image from your code and the ASP.NET Core runtime.

  • Step 2: Adding a Bit of Intelligence to the Website

Let’s make some changes to the Welcome page. A Razor page is made up of two files: cshtml and cs. Index.cshtml and index.cs drive the Welcome page. IndexModel is the name of the class in the index.cs file.

We’ll add a message property to IndexModel and display it on the page, add a public string property called message, and modify the OnGet method to set the Message property’s value.

When you launch the app, you’ll see the message on the welcome page, which will update every time you refresh the page. You can debug locally in Docker in addition to debugging in IIS or Kestrel. The VS Container tools handle everything, and the only difference is the local port to which the container sends its output.

  • Step 3: Deploying as Docker Containers in the Cloud

Now that the application has this brilliant logic, it’s time to deploy it to AWS Fargate as Docker containers—yes, plural; even with a small application, it’s always a good idea to have at least one extra instance of the container running in case something causes a container to fail. Fargate will automatically switch to a working container, and no one will notice.

You can publish apps using the AWS Toolkit from the project’s context menu or the Project menu in the toolbar. You must select the option “Publish Container to AWS…”, which is available when the project contains a docker file.

This will launch a wizard with five configuration pages. Some of the details are pre-populated and will suffice for our demonstration, while others require fine-tuning. Overall, we didn’t find it complicated or require any DevOps expertise to tweak, even on our first pass. Setting up the configuration on a subsequent deployment should be a breeze once you’re familiar with what’s on each page.

Looking for a custom database development company? Applify is one of the leading database software development companies in the UK.

  • Step 4: Define Where to Publish the Application

The first page’s defaults are correct. Your AWS toolkit profile grants permission to publish and everything is created in the chosen region. Docker Image Build specifies the type of build desired and the name of the image to be stored in the AWS repository (ECR). Deployment Target is configured to run as a service on an ECS cluster, the best option for long-running applications such as this website.

Setting up the Launch Configuration Page

  • Step 5:

Choose “Create a New Cluster” and provide a name for the ECS cluster where your service will run. Because we are creating a new ECS cluster, the launch type is already set to Fargate. Everything else should be correct, but we’ll explain them anyway:

  • The defaults for Allocated Compute Capacity (.25 vCPU and 512MB memory) are sufficient for the demo and supported by the free tier.
  • Network Configuration lets you specify the subnets and security groups of the VPC that will host your Fargate service. A VPC (Virtual Private Cloud) is an isolated virtual network on AWS. The default setting is to use your account’s default VPC. 
  • A subnet is a subset of that VPC network, and any access to a VPC must go through two or more of its subnets. By default, the wizard selected all three subnets from the default VPC.

A security group is a firewall that protects your VPC. The wizard selected the default security group of the VPC.

  • Step 6: Setting up the Service Configuration Page

The settings on the following page default to creating a new service and naming it after the project.

Within the service, a task is running a single Docker container. Even for a small demo, we like to have a minimum of two containers. If one fails, Fargate will automatically switch to the other. This is the only modification required.

Fargate determines how many containers should be running at any given time using the Minimum and Maximum Percent thresholds. Leave those at their default settings.

  • Step 7: Setting up the Application Load Balancer Configuration Page

The application load balancer’s two main tasks are to act as a point of contact gateway to the running containers and ensure that the appropriate number of containers are always running and available. A load balancer for your web application is a good idea, so you don’t have to worry about availability.

To configure a load balancer, check the first checkbox. When you check that box, the other options become active. Choose to Create New from the Load Balancer dropdown menu. This will auto-populate all of the remaining options, including creating the additional resources that your new load balancer requires and using the project name where one is required.

  • Step 8: Defining the Application Tasks

The final configuration page allows you to create a task definition. The parameters for each container that Fargate will run for your application are described in a task definition.

Begin by choosing Create New from the Task Definition dropdown menu. This will also auto-fill the task definition’s name and specify that the task should create new containers.

The task uses an IAM role to gain access to other services. Select PowerUserAccess from the “New Role-Based on AWS Managed User Policy” drop-down menu. This is a suitable option for our demonstration, but keep in mind that it is overkill for this application. You’ll need to be more selective about the permissions granted to the role you create for your real-world applications.

The task execution role will allow Fargate to create containers by pulling Docker images from ECR. When you select Create New, the appropriate role will be created for you.

Defining the Application Tasks

The defaults for the port mapping (80) and the ASP.NET Core Environment variables are correct.

It’s finally time to hit the Publish button!

This causes Docker to build the images locally. Once the images are created, the toolkit will upload them to AWS ECR, and Fargate will generate all of the resources required based on your configurations. When everything is in place, Fargate will run two containers from the image pushed to ECR using the task definition.

The wizard will leave an open window to inform you of the deployment’s status. When everything is finished, the status should show two Fargate tasks running and the URL of the app as a link.

The app is now operational, and we can see that it is time to eat some chocolate once again.

Applify.co is the best on-demand app development company you can look for in the UK.

As artists sketch thoughts, we create digitally empowered products. Applify delivers hands-on experience across multiple technologies for mobile apps, gaming apps, TV, and wearable apps. We have been recognized by Clutch as one of the top industry leaders in the global B2B market for 2021. Our team has reached new heights and we’ve worked together with Cashify, Coca-Cola, Mixpanel, SiteDrop, Show to Stream, TA, and more.

Leave a Reply

Your email address will not be published. Required fields are marked *