Amazon Web Services offers a few different container solutions. The one we are going to look at in this section is part of the Amazon Elastic Container Service (ECS) and is called AWS Fargate.
Traditionally, Amazon ECS launches EC2 instances. Once launched, an Amazon ECS agent is deployed alongside a container runtime that allows you to then manage your containers using the AWS Console and command-line tools. AWS Fargate removes the need to launch EC2 instances, allowing you to simply launch containers without having to worry about managing a cluster or having the expense of EC2 instances.
We are going to cheat slightly and work through the Amazon ECS first run process. You can access this by going to the following URL: https://console.aws.amazon.com/ecs/home#/firstRun. This will take us through the four steps we need to take to launch a container within a Fargate cluster.
Amazon ECS uses the following components:
- Container definition
- Task definition
The first step in launching our AWS Fargate hosted container is to actually configure the first two components, that is, the container and task definitions.
The container definition is where the base configuration for the container is defined. Think of this as adding the flags you would use to launch a container using the Docker client on the command line—for example, you name the container, define the image to use, set the network, and so on.
For our example, there are three predefined options and a custom option. Click on the Configure button in the custom options and enter the following information:
- Container name: cluster-container
- Image: russmckendrick/cluster:latest
- Memory Limits (MiB): Leave at the default
- Port mappings: Enter 80 and leave tcp selected
Then, click on the Update button. For the task definition, click on the Edit button and enter the following:
- Task definition name: cluster-task
- Network mode: Should be awsvpc; you can't change this option
- Task execution role: Leave as ecsTaskExecutionRole
- Compatibilities: This should default to FARGATE and you should not be able to edit it
- Task memory and Task CPU: Leave both at their default options
Once updated, click on the Save button. Now, you can click on the Next button at the bottom of the page. This will take us to the second step which is where the service is defined.
A service runs tasks which in turn has a container associated with them. The default services are fine, so click on the Next button to proceed to the third step of the launch process. The first step is where the cluster is created. Again, the default values are fine, so click on the Next button to be taken to the review page.
This is your last chance to double check the task, service, and cluster definitions before any services are launched. If you are happy with everything, then click on the Create button. From here, you will be taken to a page where you can view the status of the various AWS services that make our AWS Fargate cluster:
Once everything has changed frompendingto complete, you will be able to click on theView servicebutton to be taken to theServiceoverview page:
Now, we just need to know the public IP address of our container. To find this, click on theTasktab, and then select the unique ID of therunningtask. In theNetworksectionofthe page, you should be able to find both the Private and Public IP addresses of the tasks. Entering the Public IP in your browser should bring up the now familiar cluster application:
You will notice that the container name that's displayed is the hostname of the container, and includes the internal IP address. You can also view the logs from the container by click on the Logs tab:
So, how much is this costing? To be able to run the container for an entire month would cost around $14, which works out at about $0.019 per hour.
This costing means that if you are going to be running a number of tasks 24/7, then Fargate may not be the most cost-effective way of running your containers. Instead, you may want to take the Amazon ECS EC2 option, where you can pack more containers onto your resource, or the Amazon EKS service, which we will look at later in this chapter. However, for quickly bringing up a container and then terminating it, Fargate is great—there is a low barrier to launching the containers and the number of supporting resources in small.
Once you have finished with your Fargate container, you should delete the cluster. This will remove all of the services associated with the cluster. Once the cluster has been removed, go into the Task Definitions page and deregister them if needed.
If you enjoyed reading this article and want to learn more, you must check out Mastering Docker - Third Edition. Docker has been a game-changer when it comes to how modern applications are deployed and architected. Mastering Docker - Third Edition shows you how to leverage the power of Docker; you'll find new and innovative ways to use Docker Compose, Docker Swarm, and Kubernetes to help you take control of your containers in an efficient way.