Deploy .Net Applications in AWS

Ravi Aakula
4 min readNov 1, 2018

--

Most of the online examples explains how to deploy .Net Core apps in AWS or deploying Asp.Net full CLR apps in Elastic beanstalk. But there are none or fewer guides to deploy Asp.Net MVC Or WPF apps into AWS using containers.

I will go through how I deployed existing full CLR Asp.net Mvc application in AWS ECS docker containers and Stand-alone WPF in AWS S3 and Database in RDS. I will try to focus more on how to do , less time on why to do only that way. So that these articles will be kept minimal to required.

Found these main areas to explore to deploy full stack .net app in AWS.

  1. Dockerize existing Asp.net MVC app.
  2. Deploy ASP.Net MVC App in AWS ECS Containers .
  3. Deploy WPF app in AWS S3 and SQL Server in RDS.

I assume that you already familiar with technologies like dockers, asp.net and AWS resources. Let’s get started…

Dockerize existing Asp.net MVC app

Visual Studio 2017 supports Dockerization of .net applications and we can even debug application inside container , however you can add docker file to projects manually in Visual Studio 2015. Either case you will require to upgrade application to latest version of .net framework 4.7.1 to take advantage of configuration builder to support environment variables.

  • Install VS 2017 or install .net SDK 4.7.1
  • Migrate all projects in solution to new version 4.7.1 for version compatibility.
  • Web.configs are great for managing environment specific variables , however the basic idea of containerization is to deploy same image in all environments without changing any files . To achieve this phenomena of moving same image , I need to add Configuration builder to my project . This new configuration builder help us to read operating system environment variables and replaces in app config manager.
  • Just add configuration builder package using NuGet package manager.
  • This should automatically adds a new section in your project config file. Otherwise you can add following sections into config files.
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=1.0.0.0, Culture=neutral" />
</builders>
</configBuilders>
<connectionStrings configBuilders="Environment">
<add name="ClaimDatabaseEntities" connectionString="metadata=res://*/ClaimDataModel.csdl|res://*/ClaimDataModel.ssdl|res://*/ClaimDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\ClaimDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings configBuilders="Environment">
<add key="AppSetting1" value="3.0.0.0" />
<add key="AppSetting2" value="false" />
</appSettings>
  • You can actually customize or improve the basic features of read environment variables and replace anything’s in your config file.
https://github.com/aspnet/MicrosoftConfigurationBuilders
https://www.codeproject.com/Articles/1216422/Modern-Configuration-for-ASP-NET-with-Configuratio
  • Build application and try to test configuration builder working fine by setting environment variables in local machine.
  • Install docker for windows and change the host OS to windows(Windows 10 required for this step.) .
  • Add docker support from VS menu , it will add a new Dockerfile in project and it creates a new docker project with docker compose files.
  • We should be able to build run docker project in local machine host container.
  • We can even add Dockerfile with below content to create docker image, if VS2017 not installed.
FROM microsoft/aspnet:4.7.1-windowsservercore-ltsc2016
ARG source
COPY . /inetpub/wwwroot
WORKDIR /inetpub/wwwroot
  • docker-compose.yml file help us to combine all commands to create an image. This also help us to set environment variables, ports, networks and more.
version: '3.4'
services:
claimservice:
image: ${DOCKER_REGISTRY}claimservice
build:
context: .\Src
dockerfile: Dockerfile
ports:
- "80"
environment:
- AppSetting1=TestDM
- AppSetting2=NewSetting
networks:
default:
external:
name: nat
  • If you are not working in windows 10 machine , you can even publish the Webapi project to a folder along with these docker files. And move the publised folder to windows 10 VM machine to run execute containers using docker .
  • I used powershell to create and deploy image. To Check docker version.
docker --version
  • Run docker compose file to build, and run this application in container. This might take a little time for first time, as it will download windows base image of size 10 gb.
docker-compose up -d
  • Check container status withdocker ps
  • Unfortunately, we can not browse or access running container by using localhost in the same machine, but we can get the container IP address and test it in browser.
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" containerid

You might understood that deploying Asp.Net MVC app into container will make our life easier . We don’t have to install IIS or setup IIS with asp.net or create website in IIS. In the next story i will go through how move this image to AWS registry and run this image in AWS ECS containers.

Push Asp.Net MVC image to AWS ECR registry >>>

Refferences:

--

--