Bird
Raised Fist0
AWScloud~5 mins

Fargate vs EC2 launch type in AWS - CLI Comparison

Choose your learning style10 modes available
Introduction
When running containerized applications on AWS, you need to choose how to manage the servers that run your containers. Fargate lets AWS handle the servers for you, while EC2 launch type means you manage the servers yourself. This choice affects how much control and responsibility you have over the infrastructure.
When you want to run containers without managing servers or clusters, so you can focus on your app code.
When you need full control over the server environment, such as installing custom software or tuning performance.
When you want to save time by letting AWS handle scaling and patching of servers automatically.
When you have specific compliance or security needs that require managing your own EC2 instances.
When you want to optimize costs by using reserved or spot EC2 instances for your container workloads.
Commands
This command creates an ECS cluster for running containers on EC2 instances. You manage the servers in this cluster.
Terminal
aws ecs create-cluster --cluster-name my-ec2-cluster
Expected OutputExpected
{ "cluster": { "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/my-ec2-cluster", "clusterName": "my-ec2-cluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0 } }
→
--cluster-name - Names the ECS cluster you are creating
This command creates an ECS cluster for running containers using Fargate. AWS manages the servers for you.
Terminal
aws ecs create-cluster --cluster-name my-fargate-cluster
Expected OutputExpected
{ "cluster": { "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/my-fargate-cluster", "clusterName": "my-fargate-cluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0 } }
→
--cluster-name - Names the ECS cluster you are creating
This command runs a container task on Fargate, letting AWS handle the server management and networking setup.
Terminal
aws ecs run-task --cluster my-fargate-cluster --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[subnet-12345678],assignPublicIp=ENABLED}" --task-definition my-task
Expected OutputExpected
{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/my-fargate-cluster/abcdef1234567890", "lastStatus": "PENDING" } ], "failures": [] }
→
--launch-type - Specifies to use Fargate for serverless container hosting
→
--network-configuration - Defines networking details required by Fargate tasks
This command runs a container task on EC2 instances you manage in the cluster.
Terminal
aws ecs run-task --cluster my-ec2-cluster --launch-type EC2 --task-definition my-task
Expected OutputExpected
{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/my-ec2-cluster/123456abcdef7890", "lastStatus": "PENDING" } ], "failures": [] }
→
--launch-type - Specifies to use EC2 instances for container hosting
This command lists running tasks in the Fargate cluster to verify your container is running.
Terminal
aws ecs list-tasks --cluster my-fargate-cluster
Expected OutputExpected
{ "taskArns": [ "arn:aws:ecs:us-east-1:123456789012:task/my-fargate-cluster/abcdef1234567890" ] }
Key Concept

If you remember nothing else from this pattern, remember: Fargate means AWS manages servers for you, EC2 means you manage servers yourself.

Common Mistakes
Trying to run a Fargate task without specifying the required network configuration.
Fargate tasks need specific network settings like subnets and IP assignment to run properly.
Always include the --network-configuration flag with valid subnet IDs and IP settings when running Fargate tasks.
Running EC2 launch type tasks in a cluster without registered EC2 instances.
EC2 launch type requires active EC2 instances in the cluster to run containers; without them, tasks cannot start.
Make sure EC2 instances are launched and registered in the ECS cluster before running EC2 launch type tasks.
Summary
Create ECS clusters separately for EC2 and Fargate launch types.
Run tasks with --launch-type set to either FARGATE or EC2 depending on your management preference.
Fargate requires network configuration; EC2 requires active instances in the cluster.

Practice

(1/5)
1. What is the main difference between AWS Fargate and EC2 launch types for running containers?
easy
A. Fargate runs containers without managing servers, EC2 requires managing servers.
B. Fargate requires managing servers, EC2 runs containers without servers.
C. Both require managing servers but use different operating systems.
D. Both run containers without any server management.

Solution

  1. Step 1: Understand Fargate's server management

    Fargate abstracts server management, so you don't handle servers directly.
  2. Step 2: Understand EC2 launch type server management

    EC2 launch type requires you to manage the servers where containers run.
  3. Final Answer:

    Fargate runs containers without managing servers, EC2 requires managing servers. -> Option A
  4. Quick Check:

    Server management difference = D [OK]
Hint: Fargate = no servers to manage, EC2 = you manage servers [OK]
Common Mistakes:
  • Confusing which launch type manages servers
  • Thinking both require server management
  • Assuming Fargate needs server setup
2. Which of the following is the correct way to specify the launch type as Fargate in an ECS service JSON?
easy
A. "launchType": "EC2"
B. "launchType": "FARGATE"
C. "launchType": "Serverless"
D. "launchType": "Container"

Solution

  1. Step 1: Check valid launch type values in ECS

    ECS supports "EC2" and "FARGATE" as launch type values in uppercase.
  2. Step 2: Identify correct Fargate syntax

    The correct string for Fargate is "FARGATE" in uppercase letters.
  3. Final Answer:

    "launchType": "FARGATE" -> Option B
  4. Quick Check:

    Fargate launch type syntax = A [OK]
Hint: Use uppercase "FARGATE" for launchType in ECS JSON [OK]
Common Mistakes:
  • Using lowercase "fargate" instead of uppercase
  • Using invalid launch type names like "Serverless"
  • Confusing EC2 and Fargate strings
3. Given this ECS service configuration snippet, what launch type will it use?
{
  "launchType": "EC2",
  "taskDefinition": "my-task-def"
}
medium
A. It will run containers on EC2 instances you manage.
B. It will run containers on AWS Fargate without server management.
C. It will fail because "EC2" is not a valid launch type.
D. It will run containers on Lambda functions.

Solution

  1. Step 1: Read the launchType value

    The launchType is set to "EC2" which means containers run on EC2 instances.
  2. Step 2: Understand EC2 launch type behavior

    EC2 launch type requires you to manage the servers where containers run.
  3. Final Answer:

    It will run containers on EC2 instances you manage. -> Option A
  4. Quick Check:

    launchType "EC2" means managed servers = A [OK]
Hint: launchType "EC2" means you manage servers [OK]
Common Mistakes:
  • Thinking "EC2" means serverless
  • Assuming "EC2" is invalid syntax
  • Confusing EC2 with Lambda
4. You tried to deploy an ECS task with this launch type: "launchType": "fargate" but got an error. What is the likely cause?
medium
A. Fargate launch type is not supported in ECS.
B. The launch type should be a boolean, not a string.
C. You must specify the launch type as "EC2" for tasks.
D. The launch type value is case-sensitive and must be uppercase "FARGATE".

Solution

  1. Step 1: Check launch type case sensitivity

    ECS launch type values are case-sensitive and require uppercase strings like "FARGATE".
  2. Step 2: Identify error cause

    Using lowercase "fargate" causes a validation error because it's invalid syntax.
  3. Final Answer:

    The launch type value is case-sensitive and must be uppercase "FARGATE". -> Option D
  4. Quick Check:

    Case-sensitive launchType = B [OK]
Hint: Use uppercase "FARGATE" to avoid launch type errors [OK]
Common Mistakes:
  • Using lowercase launch type values
  • Thinking Fargate is unsupported
  • Confusing data types for launchType
5. You want to run a containerized app with minimal server management and automatic scaling. Which launch type should you choose and why?
hard
A. EC2 launch type, because it gives full control over servers and scaling.
B. EC2 launch type, because it is cheaper and requires no scaling setup.
C. Fargate launch type, because it removes server management and handles scaling automatically.
D. Fargate launch type, because it requires manual server setup but better performance.

Solution

  1. Step 1: Identify launch type for minimal server management

    Fargate removes the need to manage servers, unlike EC2 which requires server control.
  2. Step 2: Consider automatic scaling capabilities

    Fargate automatically scales containers based on demand without manual server scaling.
  3. Final Answer:

    Fargate launch type, because it removes server management and handles scaling automatically. -> Option C
  4. Quick Check:

    Minimal management + auto scaling = Fargate = C [OK]
Hint: Choose Fargate for no servers and auto scaling [OK]
Common Mistakes:
  • Choosing EC2 for minimal management
  • Assuming EC2 auto scales automatically
  • Thinking Fargate needs manual server setup
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard