Serverless compute lets you run code without provisioning or managing servers, automatically scaling to handle your application's workload.
AWS Lambda lets you upload your code and configure it to run based on triggers. It can run code for up to 15 minutes at a time.
AWS Fargate lets you run your containers on top of a serverless compute platform. It is the alternative to hosting your containers on EC2 instances.
So as we've learnt, EC2 offers flexibility, reliability, and scalability - but you are responsible for a lot of the set up and management too. Think handling patching, scaling, and ensuring high availability. This is not as much work as on-premise servers, but management processes will still need to be in place.
We then learnt about containers, which offer efficiency and portability. AWS' container services are Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS). Both ECS and EKS can run on top of EC2.
But what if you want to keep it even more hands free - what if you want to skip having to manage those EC2 instances?
That's where AWS Fargate comes in. Fargate is a serverless compute platform for ECS or EKS. But before we dive into Fargate, let's understand what serverless means.
The term “serverless” means your code still runs on servers, but you won't need to create or manage these servers. In fact, you can't even see or access the underlying infrastructure or instances that are hosting your application.
AIl the management (creating, ensuring high availability, and maintenance) is taken care of for you. Another benefit of serverless computing is that it can scale your applications automatically.
You only need to focus on creating your application. What a dream!
AWS offers multiple serverless compute options. AWS Lambda is an iconic serverless compute option that's considered one of AWS' top services.
AWS Lambda is a service that lets you run code without needing to provision or manage servers.
Here's how it works:
1. You upload your code into a Lambda function.
*A function is like a recipe in programming. It's a set of instructions that you can use to do a specific task, like adding numbers, sorting a list, or sending an email. You can use it over and over to save time and make your code more organised.
2. Configure a trigger* and from there, the service waits for the trigger.
*A Lambda trigger is like a switch that turns on a small piece of code when a specific event occurs. This event could be things like a file upload, a database change, or a website visit. When the trigger event happens, the code does its job and then turns off until the next trigger.
3. When the trigger is detected, the code is automatically run in an environment that is automatically scalable, highly available and all of the maintenance covered by AWS. For example, a simple Lambda function might involve automatically resizing uploaded images uploaded to S3. In this case, the function triggers when it detect a new uploaded image.
Because Lambda's functions are triggered by changes in data, user activity, or some event you define, people often use the word event-driven to describe Lambda.
*An invocation = each time you trigger a Lambda function to run its code.
3. While using AWS Lambda, you pay only for the compute time that you use.
- Charges apply only when your code is running.
- In the previous example of resizing images, you would pay only for the compute time that you use when uploading new images. Uploading the images triggers Lambda to run code for the image resizing function.
AWS Fargate is a serverless compute service for containers. It works with both Amazon ECS and Amazon EKS, making it easy to run containers at scale.
Example scenario: Imagine you’re developing a chat app that requires real-time messaging capabilities. With AWS Fargate, you can deploy a containerised chat server. Fargate automatically manages the container resources, scaling up or down based on the number of active users. This ensures seamless communication and eliminates the need for manual infrastructure management.