Introduction to AWS Fargate

What is AWS Fargate?

As described by AWS, AWS Fargate is a technology that you can use with Amazon ECS to run containers without having to manage servers or clusters of Amazon EC2 instances. With Fargate, you no longer have to provision, configure, or scale clusters of virtual machines to run containers.

That means you focus more on developing your applications, and your business value, and spare the time to manage and maintain the underlying infrastructure.

fargate

Components

Cluster
With AWS ECS we use cluster to group tasks or services together, and to isolate applications.

Task definitions
A task definition is a text file that describes a container ot containers that form your application. it is a JSON file and for task definition it can be use to describe up to ten containers.
The file contains information like which port to open for the application.

Task
A Task is created when you run a Task directly, which launches container(s) defined in the task definition.

Service
A Service is responsible for creating Tasks. it guarantee that you always have some number of tasks running all the time, if a Task’s container fails due to an error or something, or the EC2 instance fails, ECS service will replace it by launching another instance based on your task definition

What do we mean by managing apps and services at the container level?
To better understands this, Here is a simple architecture of Fargate in AWS ECS stack.


fargate-arch

As illustrated above, with ECS and Fargate it starts with a “Task Definition”, you as a customer use the task definition to define the container, CPU, memory, image, along with other details that tells the container how it should run.
Then a “Task” represents one or more containers that make up the application.
To run the task, you simply use the start task API and choose the launch type, either in EC2 instance you manage, or using managed environment by Fargate.

You can use AWS Fargate from AWS Console, AWS CLI, and Amazon ECS CLI.

Networking with Fargate
AWS Fargate runs in your VPC, you select the VPC, subnets, and Security groups the you need to attach to the instances that runs your containerized applications

Fargate supports Application Load Balancers “ALB”, and Network Load Balancers “NLP”.

Security with Fargate
No SSH access, as you offload the management of the infrastructure to AWS, there is no need to have SSH access, so it is secure.
And you have cluster-level isolation of your containers.

Fargate use cases

  • Long running services
  • Highly available workloads
  • Monolithic applications
  • Microservices applications

When should not you use Fargate?
You should go for EC2 mode if you have reserved instances running your applications.
As Fargate charges you on seconds of CPU and memory consumed, there is no way to translate that to reserved instances saving plan.

Notice: Before 2019 Spot instance plan was not supported, then at AWS re:Invent 2019 AWS announced AWS Fargate Spot. Fargate Spot is a new capability on AWS Fargate that can run interruption tolerant Amazon ECS Tasks at up to a 70% discount off the Fargate price.

Container services on AWS
If you are new to containers and want to make use of Fargate it is recommended to understand more about container services on AWS, and there is no better way than reading about them on AWS documentations. So here are what you need to read.

Amazon ECS
Amazon EKS
Amazon ECR

Tutorial “How to use AWS Fargate”
Now you understand the basics and want to explore some hands on experience, I will walk you through this tutorial on how to deploy a sample container using AWS Fargate. Let’s jump in.

1- Sign in to the console and search for AWS ECS, then click in “Get Started”
The first component to create is the cluster that will contain a group of tasks together.
2- Click on “Create Cluster”
Enter the cluster name and choose the VPC and subnets your want to create the cluster in.

cluster

In the infrastructure section, you will find AWS Fargate is selected for you.

fargate-selected

Click “Create”

3- Click on the cluster name to open its page, and then from the service tap, click create, to create a new service.

4- Select the “Launch Type” Fargate.

Launch-Type

5- Scroll down to “Deployment configuration” section, and click on “Task definitions”

Task-definitions

This will open another browser tap to create the “Task definitions”

6- Click on “Create new task definition”
Enter “Task definition family” name.
In the container details section enter the container name, and the Image URI.
If you don’t have an image in AWS ECR, please get one first.

Task-definitions2

7- Review the “Port mappings”, then click “Next”

Port-mappings

8- In the “Environment” section make sure AWS Fargate is selected and choose the CPU and Memory that works for you.

Environment

9- Accept all the default settings and scroll down, then click “Next”

10- In the “Review and create” page, review every thing and click Create

11- Now back to the “Create service” page, you will need to refresh the page to load the task definition you just created and select it, then enter a “Service name”.

def

12- Review the Networking and Load Balance sections if needed, and finally click “Create”

13- Click on the Service to open its page, and review the health

health

14- Click on “Configuration and task”

Configuration-task

15- Scroll down, and check that the task is running, then click on it is name

task

16- Click on open address, to confirm that your service is running.

running

You will need to add a rule to the security group to allow traffic to port 80, in order to be able to connect like in step 16

That will be all.
Make sure you delete the container and clean all the resources to avoid unexpected costs in your bill.

I hope this has been beneficial to you, and I would like to thank you for reading.

Follow me for more articles and tutorials in serverless.