0
votes

I am using AWS CDK for my Infrastructure-as-Code (CDK code fragments in C# .NET below).

I have a VPC created as follows:

        // vpc (testing)
        this.vpc = new Vpc(this, "vpc", new VpcProps() {
            MaxAzs = 1,
            NatGateways = 1
        });

I have a cluster created as follows:

        // cluster
        this.cluster = new Cluster(this, "cluster", new ClusterProps() {
            Vpc = this.vpc
        });

I have a worker definition for a Fargate Task as follows:

        // worker
        this.workerDefinition = new FargateTaskDefinition(this, 
            "worker-definition", 
            new FargateTaskDefinitionProps() {
                Cpu = 256,
                MemoryLimitMiB = 512
            }
        );

I have the Fargate worker service as follows that ties the worker service to the cluster with the instance count and task definition:

            var worker = new FargateService(this, "worker", new FargateServiceProps() {
                Cluster = this.cluster,
                DesiredCount = k,       // up to k worker instances
                TaskDefinition = this.workerDefinition
            });

Questions:

  1. I want to use k reserved EC2 instances in the cluster as a minimum. Should I create those EC2 instances and add them to the cluster?
  2. If I call ecs.runTask() using a FARGATE launch type, would the Fargate service be launched on one of the pre-spun EC2 instances until the limit of k is reached (and then, I intend to use an autoscaling rule based on workload to spin up FARGATE_SPOT instances on-demand)?
  3. Is my understanding that EC2 instances pre-created and added to the cluster will be used to run the FARGATE tasks correct?
  4. How does the scheduler decide where to place the tasks? Suppose my task is re-entrant (ie., I can run multiple instances on the same machine), how does Fargate know (or how do I instruct it so that it would know) that it can place multiple instances on the same machine (ie., does Fargate automatically use CPU/IO utilization as a metric for packing tasks on a container)?
1
I'm not sure you can mix EC2 and fargate cluster this way. Seems there is misunderstanding what fargate means. In Fargate you have no access to underlying infrastructure, nothing to do with EC2, no reserved instances. Using Fargate you may use the "Saving plan" instead of RI. If you want to mix workload on your EC2-based ECS and Fargate, you may create a Kubernetes (EKS) cluster to manage multiple NodeGroups, examplesgusto2

1 Answers

2
votes

Fargate tasks that you run will never run on EC2 instances you create and manage, they will run on compute capacity managed by AWS. One of the benefits of using Fargate is that you don't have to worry about managing EC2 instances.

To answer your questions:

  1. If you want to run your tasks on EC2 instances that you create, you will need to launch the tasks using the EC2 launch mode.

  2. If you use the FARGATE launch type, Fargate will run your task on compute capacity managed by AWS. Any EC2 instance you run will not be used for these tasks.

  3. No, EC2 instances you create and add to the cluster are only used for tasks launched with the EC2 launch type.

  4. Running a service, the ECS Service Scheduler will start or stop tasks based on the scaling criteria you define for the service.