0
0
AWScloud~5 mins

Fargate vs EC2 launch type in AWS - CLI Comparison

Choose your learning style9 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.