What is Task Definition in ECS: Explained Simply
task definition in AWS ECS is a blueprint that describes how Docker containers should run in a task. It specifies details like which container images to use, resource needs, and networking settings, allowing ECS to launch and manage containers consistently.How It Works
Think of a task definition as a recipe for cooking a meal. Just like a recipe lists ingredients and steps, a task definition lists the containers to run, the resources they need, and how they connect. ECS uses this recipe to create tasks, which are running instances of your containers.
When you want to run or update your application, you create or update the task definition. ECS then uses it to start containers with the exact settings you specified, ensuring your app runs the same way every time.
Example
This example shows a simple task definition JSON that runs a single container using the nginx image. It sets CPU and memory limits and exposes port 80.
{
"family": "nginx-task",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "nginx-container",
"image": "nginx:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
}
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512"
}When to Use
Use a task definition whenever you want to run containers on ECS. It is essential for deploying microservices, batch jobs, or any containerized app. When you update your app, you create a new task definition version to roll out changes safely.
For example, if you have a web app and a database running in separate containers, you define each in the task definition. ECS then manages starting, stopping, and scaling these containers based on your settings.
Key Points
- A task definition is a JSON blueprint for running containers in ECS.
- It defines container images, CPU, memory, ports, and networking.
- Each update creates a new version to manage deployments.
- ECS uses it to launch and manage container tasks consistently.