Deploy ASP.Net MVC App in AWS ECS Containers

Ravi Aakula
4 min readNov 1, 2018

--

In the previous article we learned how to modernize existing ASP.Net MVC app by adding docker support and deployed to containers in local host machine. We will go through how to move local image to AWS ECS Registry and run this image in AWS ECS cluster.

Push Asp.Net MVC image to AWS ECR registry

  • Create an IAM user in AWS console and note down credentials.
  • ECR is a AWS docker registry to manage images in cloud.
  • Create a new Repository and note down repository url.
  • Install AWS CLI in your local machine and configure aws profile.
aws configure
  • Login to AWS ECR by running this in windows command prompt.
aws ecr get-login --no-include-email --region us-west-2
  • Copy the result from above command and paste again in the command prompt.
  • You should receive success login message.
  • Now add another tag your running docker image with ECR repository URL.
docker tag claimservice awsaccountnumber.dkr.ecr.awsregion.amazonaws.com/containerimagename:latest
  • Now push newly tagged image to ECR with command.
docker push awsaccountnumber.dkr.ecr.awsregion.amazonaws.com/awsecssample:latest
  • You should be able to confirm that image is pushed to AWS by logging into AWS ECS console.

Create AWS infrastructure to deploy App in AWS ECS

I love serverlesss architecture, but windows containers in AWS still requires you to create EC2 windows machine. Because they have not yet supports FARGATE for windows containers.

Here is the architecture of AWS resources inside VPC.

courtesy: https://github.com/aws-samples/ecs-refarch-cloudformation-windows
  • I have created (modified from online :)) a cloudformation templates to install necessary resources in AWS to deploy app in windows containers. You can find here.
  • I assume that you already know VPC, Subnets and networking in AWS, otherwise follow same basic tutorials.
  • Just create a new cloundformation stack in AWS console with first template — ecs-windows-cluster-modi.yaml
  • This Cloudformation stack will create following resources .
  1. ECS Cluster: AWS custom orchestration for containers in Cloud.
  2. Auto Scalling Group: It is group of EC2 instances with similar characteristics. We use this group to launch EC2 instances automatically on the fly with demand grows.
  3. EC2 Instances: We require one or more windows EC2 instances to run containers.
  4. Elastic Load Balancer: To distribute http requests to all EC2 instances equally we use load balancer. ELB is fully manager load balancer in AWS.
  5. Target Group: We create Listener rules in ELB to route request to specific Targets(such as container app or ec2 web app). We actually create groups for this targets and mention them in EC2 listeners.
  6. IAM Roles: Service role is to run tasks in ECS and Instance role to access all other AWS resources from EC2 instances with in this cluster.
  7. Security Groups: ELB security group to open ports for specific ips or specific people to acceess this WebAPI. And EC2 Security group , just allows requests from ELB and blocks everything else.

Create ECS Service and Run Tasks in ECS

  • Once the first stack creation completed successfully, then we can create another stack to create actual service and tasks to run the ECS containers. Edit cloundformation template cluster_service.yml and start stack.
courtesy: https://medium.com/@YadavPrakshi/automate-zero-downtime-deployment-with-amazon-ecs-and-lambda-c4e49953273d
  • This stack will create following resources and run the containers using ECS cluster services and tasks.
  1. ECS Service: It is part of AWS ECS to make sure number of tasks are running everytime with given configuration.
  2. Task Defination: It is set of instructions to create container and run container in this ECS cluster. It usually contains what ECR to use, what are all environment variables and ports. It is equivalent to docker-compose.
  3. IAM Roles: Service role is to run tasks in ECS.
  • Usually it takes around 20–30 minutes to run this stack first time, as it take 20 mins to download Windows base image into EC2 machine. You can delete and recreate this stack ,if want to change any parameters or task definitions .It will be fast from 2nd time onward.
  • Browse to ELB dns name url that can be found in EC2 ELB dashboard .

In the next article , i will go through how to deploy WPF application.

Refferences:

--

--