AWS Auto Scaling Spot Fargate Cluster— Quickstart with CloudFormation

Running a cluster of machines can be hard. Fargate removes the need to manage instances on which to run containers. In this article, I demonstrate how to create a service running on Amazon’s Elastic Container Service with Fargate using CloudFormation.

Fargate removes the need to think about running a container on a machine and instead leaves the user to only need to think about running applications on the container level. A lot more knowledge and understanding is required to manage your own instances; check out our previous on Auto Scaling with a SpotFleet Cluster if you’re interested in learning more about this.

Architecture

For this example, I am going to create a Dockerised Python web server and deploy it to an ECS cluster which auto scales the number of Fargate containers. An Application Load Balancer (ALB) will be used to create an API which load balances the containers running the service.

🔗 Get The AWS Auto Scaling Spot Fargate Cluster Quickstart Code On Github 📔

The service

The service is an asynchronous Python web server running on port 5000 with CORS enabled. Note that the healthcheck endpoint is required for ECS to keep track of the service.

Deployment

Using AWS CLI to deploy CloudFormation is as simple as:

The deployment is split into four templates:

Let’s Build! 🔩

VPC

I am building this service inside a VPC described in a previous article

It’s pretty standard. There are three public and three private (hybrid) subnets.

Load Balancer

The service requires a public facing load balancer which distributes HTTP requests to the machines running the web server.

Cluster

The cluster orchestrates containers running on the machines. If you are unfamiliar with Docker, check out this article. Dockerising the Python web server can be done in few lines:

In 2020, AWS introduced Capacity Providers to ECS, this includes Spot Fargate, which are a fraction of the price of standard Fargate containers.

The following template configures an ECS cluster using Fargate Spot, and ECR to store the Docker image of the Python web server:

Service

I’ve configured the load balancer to listen on port 80 for HTTP requests and send them to a Target Group — a reference we can use when defining the service to access traffic.

ECS runs the Task Definition as a persistent service using the web server image in ECR. It’s as simple as defining it as Fargate.

Configuring an auto-scaling policy on the containers works in much the same way as the EC2 machines as they have defined memory and CPU so can be scaled based on those metrics, too:

Usage

After a successful deployment, it is possible to access the DNS name of the ALB in the EC2 section of the AWS console which should look something like:

I am now able to view the response inside Post Man:

Use a Domain Name

But it’s quite simple to use a custom domain using AWS. You must first transfer your DNS management to Route 53 and then create a new record set which is aliased to the load balancer.

🔗 Get The AWS Auto Scaling Spot Fargate Cluster Quickstart Code On Github 📔

Thanks For Reading

I hope you have enjoyed this article. If you like the style, check out T3chFlicks.org for more tech focused educational content (YouTube, Instagram, Facebook, Twitter).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store