AWS ECS Fargate – Deploy and Run Multi-Container Web Application (.NET Core, Angular, Postgres)

Introduction

With AWS Fargate, we can run applications without managing servers. Fargate is a serverless compute engine for containers.

In previous post, we covered basics of AWS Fargate and took a step by step approach to deploy and run a .NET Core Web API application on AWS Fargate Service. We used AWS Fargate web console for most of the steps.

Today, we’ll take one step further and deploy a more complex web application to AWS ECS Fargate infrastructure. Also, this time, we’ll use docker cli instead of setting up infrastructure ourselves using AWS Web management console.

Following will be the output of our exercise today. A web application, running in AWS ECS Fargate and publicly available.

Initial Steps

To start with, we need an application.

You can use your own application for the exercise, if you like.

I’ve an existing application which I’ve used for other posts and demos and if you are following along, you might already familiar with it.

The application has following parts

  • Angular FrontEnd
  • .NET Core WebAPI
  • Postgres Database

I’ll not go into details and if you are interested in more information, you check following post

Migrate On-Prem Web app (.NET Core, Angular and Postgres) – to AWS Serverless

Also we’ll not go into the details of Angular, .NET Core, or basics of docker, as the focus is more to deploy this application on AWS ECS Fargate.

Now, the steps we are going to take to deploy and run the application on AWS Fargate are independent of programming language and frameworks. So you can easily apply these learning to any of your application.

Sample application source-code and docker files are available from git repository.

Following picture shows the source-code repository contents and docker files.

Docker Setup

Following is a very simple docker-compose.yml file which models our application.

It has two services accountingapi (backend) and webui (frontend)

We can run the application locally using docker-compose up

and can access the UI running locally

This is all very standard docker stuff.

Now, lets see what it takes to run this application to AWS Elastic Container Service (ECS).

I also published these images to Docker Hub, so those are publicly available (otherwise can store those in AWS ECR as well)

Running Application on AWS Elastic Container Service

Following diagram shows our intention that we want to run application from local machine to AWS ECS.

Note for the database, we could run that into container as well, but here we can instead use a managed service from the platform (which in this case is AWS RDS) and that’s further simplify our workload.

The first step in this direction is to setup the docker context.

Docker context

following command shows the current docker context which is identified with a * symbol.

docker context ls

We can create an ecs type of context and give it a name e.g. aws as follows

docker context create ecs aws

if we list context again, we can see the newly created context aws, is shown but its not yet the current context

we can set an environment variable to set current context to aws as follows

and now context is set to aws and we can use docker commands to work with aws ECS from the terminal.

Deploy to ECS

All that remaining is simply use following command

docker compose up

we can see the output as follows (note command is not docker-compose tool but rather docker cli built in command)

This command will spin up different services and components needed to run our application as modeled in docker-compose.yml file.

On the AWS web console, we can see a new Cluster is setup:

following Task definitions are created

and here are the service status and the logs from our applications

following tags assigned

As you can see that this all infrastructure is automatically setup for our application with just using simple docker command and our existing docker-compose file.

Accessing the Web Application

The infrastructure setup for the application also includes a load balancer

The load balancer is setup with a DNS record and we can use it to access our application:

here is the link for the load balancer to access the application.

With this our application is deployed and running in AWS ECS cluster using serverless Fargate type.

Related Articles

Summary

In this post, we saw that how we can deploy a multi-container application to AWS Elastic Container Service using docker compose and a simple docker-compose yml file.

We learn how to create a docker ecs context and then set it as default context to use docker cli to work with AWS ECS.

Let me know if you have any questions or comments. Till next time, happy coding.

Leave a Comment