AWS Fargate and serverless computing services
Lately we are seeing how serverless computing services where programmers can focus on creating their application and forget about the underlying infrastructure don’t stop growing and increasing their features. Since late 2017, we have AWS Fargate at our disposal.
Fargate is a service that allows us to run containers on an AWS managed infrastructure, not having to worry about the instances that our containers will be running. Also, the price will depend on the time that our application is running.
Moreover, from the past AWS re-Invent we have Fargate Spot at our disposal, which uses a similar infrastructure to EC2 Spot instances, allowing us to save up to 70% in our task running costs.
To understand how does this service work and its use cases, we will see an example of a script on Python that runs a “Hello World”.
Why should you use Fargate over Lambda?
We don’t have to stick with the offered runtimes available on Lambda. We can use the language and version that we need.
We can run legacy services that are no longer supported.
We don’t have an execution time limit, and we can run more memory intensive tasks as limits are higher than in Lambda.
It’s portable to any platform: in the end, it’s a Docker container.
Let’s talk about costs!
As we said, in the case of Lambda the costs will depend on the execution time and the resources allocated.
In this case, for on-demand instances in Ireland (eu-west-1):
And in case of Spot instances in Ireland (eu-west-1):
Now that we know possible use cases on Fargate and its costs, let’s proceed with a little tutorial on how to run our first task.
The steps are as follows:
- Create an ECS cluster
- Upload our Docker image to an ECR
- Create the task definition
- Run the task
In the post you will find all the steps covered using the AWS CLI through the AWS console. And, if there’s the chance, we will put at your disposal some CloudFormation files on Github.
1. Cluster creation
To create the cluster, the first thing that we have to specify is the base where we want to start from. In our case we will select “Networking Only” as we will use the cluster for Fargate tasks or services.
Then we have to give it a name to identify it the cluster, in case we want tags to give the resource a context.
In case we want to do it with from CLI, the following command is the same as the steps described:
aws ecs create-cluster --cluster-name capside-blog-post --region eu-west-1
And the expected output is something like this:
{
"cluster": {
"status": "ACTIVE",
"pendingTasksCount": 0,
"runningTasksCount": 0,
"statistics": [],
"clusterArn": "cluster-arn",
"clusterName": "capside-blog-post",
"registeredContainerInstancesCount": 0,
"activeServicesCount": 0
}
}
2. Create and upload the image to an ECR
For this post, we will assume that you have experience with Docker and you can create an image with your service.
Once we have the image ready on our local Docker, we must upload it to a container repository. For the purpose of this tutorial, we will be using AWS’s ECR. But just like we said, you can use whatever you want.
The steps are the following:
1. Obtain the ECR credentials from AWS
aws ecr get-login --region eu-west-1 > credentials.txt
2. Authenticate against the ECR
docker login -u AWS https://aws_account_id.dkr.ecr.eu-west-1.amazonaws.com
3. Create a repository
aws ecr create-repository --repository-name our_service
4. Create a tag from the image
docker tag our_service:1.0 aws_account_id.dkr.ecr.eu-west-1.amazonaws.com/our_service:1.0
5. Push the image to the repository
docker push aws_account_id.dkr.ecr.eu-west-1.amazonaws.com/ our_service:1.0
3. Create the task definition
Once we have a cluster to run our task in and the Docker image uploaded to a repository, we can go ahead and create the definition of the task.
First, we have to select the launch type of the task. In our case, Fargate.
Here’s where we have to define the properties of the task. We will set the amount of CPU and memory that we are going to use, the image, whether we need volumes, access roles to the AWS resources…
Then, we will set up the name and the role for the task:
Once we have that set up, we will be adding the size of the task. Which means the amount of memory and CPU you are going to use:
And finally, the container definition, which is the image we are going to use.
To do it through the console, the equivalent command would be the following:
aws ecs register-task-definition --family Fargate --cpu 256 --memory 1024 --container-definitions '{ "name": "capside-blog-post-task", "image": " aws_account_id.dkr.ecr.eu-west-1.amazonaws.com/our_service:1.0"}' --region eu-west-1
4. Run the task
Now that we finally have the cluster, the image and the task definition, we need to run it.
To do so, select “Run task” button on the ECS menu and set up the launch type, which in this case is Fargate. Select the task definition we had previously created and the cluster where we want to run the task in.
With this being done, we have our first task running.
In this case, the equivalent console command is:
aws ecs run-task --cluster capside-blog-post --task-definition capside-blog-post-task:1 --network-configuration