Amazon ECS is a highly scalable, fast, container management service that makes it easy to run, stop, and manage Docker containers on a cluster. You can host your cluster on a serverless infrastructure that is managed by Amazon ECS by launching your services or tasks using the Fargate launch type. For more control you can host your tasks on a cluster of Amazon Elastic Compute Cloud (Amazon EC2) instances that you manage by using the EC2 launch type.
The Fargate launch type allows you to run your containerized applications without the need to provision and manage the backend infrastructure. Just register your task definition and Fargate launches the container for you.
The EC2 launch type allows you to run your containerized applications on a cluster of Amazon EC2 instances that you manage.
A cluster allows the use of both ways of launching applications, thus giving better control over the apps we want to launch, for example, if we need an application that requires your data to be persistent, at the time of creating this post, Fargate does not support this type of storage and therefore the type of EC2 launch tends to be used.
Using the AWS Console for launching one or more applications using the EC2 type or combined with Fargate inside of the same cluster, can be confusing if you want to know everything you need to accomplish your goal.
On the main page of the ECS cluster creation section it has a kind of demo where we can launch predefined or customized applications, however, it is only for the Fargate launch type and it is somewhat limited as its name says it is just a demo that AWS offers.
For the absolute control and personalization of all the necessary parts for the infrastructure, it would be to create each part manually and relate them and configure them with each other, however, this method, although it is very customizable, requires a wider knowledge and configurations are a little more complicated, as an example we see the creation diagram of the different parts needed for the infrastructure and their dependencies with a network style with only public subnets.
Below we show a method that allows us to customize all the parts without becoming too complicated, whether it is only required to launch applications via EC2, or even EC2 + Fargate in the same cluster.
1.- When we create a cluster, we have three ways to create it: empty cluster, cluster plus the network or cluster plus the network and an EC2 Linux or Windows instance (for the purpose of this post, we used an EC2 Linux instance as example), we can take advantage of this last option to indicate all the requirements of the network if we already have them pre-created as they are: VPC, subnets and security group among others, and if not, we can create them in this same step without much complication.
Then it is just filling in the different options with the appropriate data to the project, some of them are shown in the following image, IMPORTANT! this is just an example, you have to adapt the data for what your project needs:
2.- Since the cluster has been created, it is ready to launch both Fargate and EC2 applications as shown in following picture, the next step would be to create the Task Definitions (TD) corresponding to the type of launch we need.
When creating the TD, it offers us to choose the launch type compatibility, be Fargate or EC2.
Both forms have very similar settings that can be configured with the exception of some attributes, which are very specific for the type of launch that is required, as an example: Network Mode, which is for the EC2 launch can be bridged among others and for Fargate it can only be awsvpc.
3.- Next, the creation of the service within the cluster, in the same way as the TD, some steps are very specific for the type of launch that is required, as an example: the Application Load Balancer (ALB) that only has the option of being used or not. When an application is launched by launch type EC2; although internally Fargate also handles them, it does not give the option to configure it.
Likewise, in the EC2 type launch, aspects such as: in which cluster the service will be hosted, VPC, Security Group (SG), Subnets among others, must be defined, while in the Fargate type launch it is done internally and therefore it is not necessary to perform these steps.
This service will be of the EC2 or Fargate type depending on the TD we have assigned when configuring the service itself.
Finally, the complete diagram with the most important points to differentiate when carrying out the previous process described above is shown.
After creating the cluster, step 1, we can repeat steps 2 and 3 to add as many applications as we want (according to the capabilities of the cluster) and by the type of launch calculated according to the requirements of each application.
* The ALB can or cannot be created, depending on the type of launch that will be used. If EC2 is chosen ALB can be required, and Fargate manages the ALB internally and therefore the configuration steps are not performed manually.