Introduction to AWS Fargate
Tags: awsfargate, serverless, containers
Published: 2023-03-16 08:58:16

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/peetg2uwfjxt3e7kxhj1.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4twjhpquqzcq8odjcw6.png)

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](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html)
[Amazon EKS](https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html)
[Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw0o7xvbdasawk7odgb4.png)

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

![fargate-selected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/naq4j9xphs9upy60kfl3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69yhy8kcbbbkwye4rc42.png)

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

![Task-definitions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnkduk6igtcigy8lxkbb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zjxn6p6p7znwb840l87.png)

7- Review the "Port mappings", then click "Next"

![Port-mappings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k4q3q9yak09nhw3s9ft0.png)

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

![Environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hmlp6pr2yyubc61h7qg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2g1js1nuv1fs74ysgno4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56syblaxr2e8dtju1pac.png)

14- Click on "Configuration and task"

![Configuration-task](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyyssdhc7lhii1kg7aim.png)

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

![task](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wztux0oyco6rui01zzxo.png)

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

![running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyd2w07zm6y6udcnsd2a.png)

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.