One of the most promising features of ASP.NET 5 is the ability to run on Windows, Mac, and Linux. With regard to development, I imagine most people will continue to use Windows or develop from OS X (even if the tooling is less mature). When deploying applications to production, this is where I expect the Linux support to shine.
Linux has come to power the vast majority of the internet. A rich ecosystem of hosting and tools have been built around hosting on Linux which makes it a natural choice for deploying ASP.NET 5 applications. Furthermore with technologies like Docker containerization, we can package up our application along with system dependencies as a single image and deploy that to some Linux host in the cloud and have our application run as we’d expect it to.
While hosting providers like Microsoft’s Azure have done a great job make our lives easy for deploying and hosting ASP.NET applications, the cross-platform nature of ASP.NET 5 means we now have the liberty to host our .NET applications almost anywhere.
For that reason I wanted to explore hosting a simple ASP.NET 5 application on Amazon AWS using their EC2 Container Service (ECS). ECS allows me to deploy a Docker image to an image repository like Docker Hub and have it run inside one or more EC2 instances. This is much easier than the alternative of provisioning an EC2 instance and installing the .NET execution environment on it manually.
Instead, I can create a deployable Docker container on my development box, test it out locally, then finally deploy it to a vanilla EC2 instance.
In this post, I’m going to share how I made use of the Microsoft provided microsoft/aspnet Docker image to deploy a simple ASP.NET 5 application to AWS ECS. This Docker image has already done all the heavy lifting of installing and configuring the .NET execution environment for our app. I just need to provide my ASP.NET 5 app and I’m off to the races.
If you’re new to Docker or you’re running on Windows, you may want to check out my post on running ASP.NET 5 using Docker on Windows.
Disclaimer: I work for Amazon as a software engineer on the Amazon Alexa team. While I do use AWS services in my day to day job, I don’t work for the AWS organization. Opinions, ideas, and thoughts shared in this post (and website) are entirely my own and do not necessarily reflect that of Amazon or AWS.
Demo
1. Create an ASP.NET 5 Web Application Project
The first thing I did was create a default ASP.NET 5 web application using the ASP.NET 5 Preview Template for Web Application. I called this project
ECSDemo.
For this demo I’m just going to deploy the default web application to ECS unchanged.
2. Create Dockerfile
The first thing I need to do is create a
Dockerfile– this allows me to script the building of a new Docker image that I’m going to run in ECS. Right click the project and add a new text file called
Dockerfile:
FROM microsoft/aspnet:1.0.0-beta8-coreclr ADD . /project WORKDIR /project RUN ["dnu", "restore"] ENTRYPOINT ["dnx", "web", "--server.urls", "http://0.0.0.0:5000"]
Notice that I’ve provided an extra parameter to the dnx web command to tell it to serve on
0.0.0.0(rather than the default
localhost). This will allow our web application to serve requests that come in from the port forwarding provided by Docker which defaults to
0.0.0.0.
I also had to save this file using
US-ASCIIencoding since Docker doesn’t play well with the default
UTF-8that Visual Studio uses:
3. Build Dockerfile
Now that I have a
Dockerfile, I can jump into my
Docker Quickstart Terminaland kick off a build:
docker@default:/c/Users/armen/projects/ECSDemo/src/ECSDemo$ docker build -t dotnetliberty/ecsdemo .
4. Push Docker image to Docker Hub
I now have a new Docker image that is saved locally under the name
dotnetliberty/ecsdemo. Since I want to make this image accessible to AWS ECS, I have to push this to a remote repository. First I went to my Docker Hub account and created a new repository:
Next, from the terminal I logged into my Docker Hub account using the
docker logincommand:
docker@default:/c/Users/armen/projects/ECSDemo/src/ECSDemo$ docker login Username: dotnetliberty Password: Email: armen@example.com WARNING: login credentials saved in /home/docker/.docker/config.json Login Succeeded docker@default:/c/Users/armen/projects/ECSDemo/src/ECSDemo$
Now that I’ve logged into my Docker Hub account, I can push my new image to my newly created repository:
docker@default:/c/Users/armen/projects/ECSDemo/src/ECSDemo$ docker push dotnetliberty/ecsdemo The push refers to a repository [docker.io/dotnetliberty/ecsdemo] (len: 1) 5685cf03bf52: Image successfully pushed 950804d33124: Pushing [=======> ] 22.25 MB/147.9 MB
When that completes you should be able to see your image on Docker Hub:
5. Setting Up Amazon EC2 Container Service (ECS)
Now that my Docker image is sitting in Docker Hub, I can now setup AWS ECS to use this image. The first time logging into your AWS account and navigating to the EC2 Container Service section you will be greeted with a Getting Started wizard. I chose
Customand pressed
Next Step.
6. ECS – Task Definition
This wizard will take us through defining our
task definitionsand
services. A task definition is a grouping of one or more containers that should run together. In this case I’ve called my task definition
ecsdemoand I’ve defined a single container:
One thing to note is on the container definition I’ve set up the host port 80 (EC2 instance) to map to the container port 5000 since Kestrel will be serving our website on that port, but I want to be able to hit my EC2 instance without specifying a port (using the default HTTP port 80).
7. ECS – Service Definition
After defining my task, I can now schedule the task as a service. The service is the part that actually fires up my container and restarts it if it crashes. With more complicated setups I can have my service run a multiple instances of my task, but for this demo I’m going to leave it at the default of 1.
8. ECS – Cluster Definition
Finally I need to configure the cluster – the underlying EC2 instance(s). In this case, I’m going to use just one
t2.microinstance since that’s all I need.
For key pair name, it wants me to select a key pair that I’ve configured in the EC2 Management Console. This key pair has an encryption key that allows me to
sshinto the EC2 instance from my desktop. I won’t be using
sshin this demo, but make sure that you save your key pair file (.pem) in a safe place if you want to access your EC2 instance.
Finally, ECS wants me to associate an Identity and Access Management (IAM) role for these instances. IAM is the AWS permissions and access control mechanism and I need to do this so that my EC2 instance is able to communicate with ECS. I created a new role and accepted the defaults. Now I was ready to click
Review & Launch.
The last page of this wizard displays all the selected settings and allows me to make any last minute changes. Since I’m happy with the settings I clicked
Launch Instance & Run Service.
9. Grab EC2 Instance Public Address (DNS)
This will cause AWS CloudFormation to go off provisioning all the resources needed to get my new ECS service up and running. After a few minutes it completed and I jumped over to the Amazon ECS console where I could see my default cluster that was just created:
From here I clicked the ECS Instances tab to see a list of running containers. I clicked on the link in the EC2 Instance tab to take me to the EC2 instance that is running my Docker container: I want to grab its DNS address (so I can view it in my browser).
This takes me to the AWS EC2 dashboard with my new EC2 instance selected. Near the bottom I can see the Public DNS record – this is the publicly accessible address for the EC2 container running my Docker image.
10. Visit Website
I can copy this address and paste it into a browser to navigate to the website hosted in my Docker container:
Next Steps
It was pretty encouraging to see how easy it was to get ASP.NET 5 running on AWS using the Elastic Container Service. By making use of the
microsoft/aspnetDocker image I was able to build and test my application locally and deploy it to a generic EC2 instance that can run Docker containers without having to worry about host configuration and installing any ASP.NET 5 dependencies or .NET frameworks on the target host.
In the near future I’ll share a follow-up post on how we can configure ECS to use a private repository to pull our images from (rather than the public Docker Hub repository used in this demo). This will be good since I don’t necessarily want just anybody to pull down my Docker images along with project source files.
I’m excited to play around with ASP.NET 5 on ECS some more and also other hosting providers. I’ll be sure to share my findings with you guys here on .NET Liberty.