Bird
Raised Fist0
AWScloud~10 mins

Task definitions in AWS - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Task definitions
Write Task Definition JSON
↓
Register Task Definition in ECS
↓
ECS stores Task Definition
↓
Use Task Definition to Run Tasks or Services
↓
ECS launches containers as defined
↓
Tasks run with specified settings
↓
Monitor & Update
You create a task definition JSON, register it with ECS, then ECS uses it to run containers with the settings you defined.
Execution Sample
AWS
{
  "family": "my-task",
  "containerDefinitions": [
    {
      "name": "app",
      "image": "nginx",
      "memory": 128
    }
  ]
}
Defines a task with one container named 'app' using the nginx image and 128 MB memory.
Process Table
StepActionInput/StateResult/Output
1Write task definition JSONDefine family 'my-task', container 'app', image 'nginx', memory 128JSON ready to register
2Register task definitionSend JSON to ECSECS stores task definition with revision 1
3Run task using definitionRequest ECS to run 'my-task:1'ECS launches container 'app' with nginx image
4Container startsContainer runs with 128 MB memoryApp container running
5Update task definitionChange memory to 256New revision 2 registered
6Run new taskRequest ECS to run 'my-task:2'ECS launches container with updated memory
7Stop tasksRequest ECS to stop running tasksContainers stopped
8ExitNo more tasks to runExecution ends
💡 No more tasks requested, ECS stops containers and ends execution
Status Tracker
VariableStartAfter Step 2After Step 5Final
Task Definition RevisionNone122
Container Memory (MB)128 (initial JSON)128256 (updated)256
Task Running StateNoYes (step 3)Yes (step 6)No (step 7)
Key Moments - 3 Insights
Why do we need to register a task definition before running tasks?
Because ECS needs a stored definition to know what containers to launch and how. See execution_table step 2 where registration stores the definition.
What happens if you update the task definition JSON but don't register it?
ECS will still use the old registered version. Only registered revisions are used to run tasks, as shown in steps 5 and 6.
Can you run tasks with different settings without changing the original task definition?
No, you must register a new revision with updated settings. ECS runs tasks only from registered revisions (step 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the task definition revision after step 2?
ARevision 0
BRevision 1
CRevision 2
DNo revision yet
💡 Hint
Check the 'Result/Output' column at step 2 in execution_table
At which step does ECS launch the container with updated memory?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look for when the task definition revision changes and a new task runs in execution_table
If you stop tasks at step 7, what is the running state after that?
AYes, tasks still running
BUnknown
CNo, tasks stopped
DTasks paused
💡 Hint
Check 'Task Running State' in variable_tracker after step 7
Concept Snapshot
Task definitions are JSON files describing containers to run.
You register them with ECS to create revisions.
ECS uses these revisions to launch containers.
Update by registering new revisions.
Run tasks by specifying revision.
Stop tasks to end containers.
Full Transcript
Task definitions in AWS ECS are JSON documents that describe how to run one or more containers. You write the JSON with container details like image and memory. Then you register this JSON with ECS, which stores it as a revision. When you want to run containers, you tell ECS which task definition revision to use. ECS launches containers based on that definition. If you want to change settings, you update the JSON and register a new revision. Running tasks always use a registered revision. You can stop running tasks to end containers. This flow ensures ECS knows exactly what to run and how.

Practice

(1/5)
1. What is the main purpose of an AWS ECS task definition?
easy
A. To specify how to run a containerized application including resources and settings
B. To create a virtual machine in AWS
C. To store data in AWS S3
D. To manage user permissions in AWS IAM

Solution

  1. Step 1: Understand what a task definition is

    A task definition tells AWS ECS how to run your containerized app, including details like CPU, memory, and ports.
  2. Step 2: Compare options to the definition

    Only To specify how to run a containerized application including resources and settings correctly describes this purpose. Other options describe unrelated AWS services.
  3. Final Answer:

    To specify how to run a containerized application including resources and settings -> Option A
  4. Quick Check:

    Task definition = container run instructions [OK]
Hint: Task definitions describe container setup and resources [OK]
Common Mistakes:
  • Confusing task definitions with EC2 instances
  • Thinking task definitions manage storage or permissions
  • Mixing up ECS with other AWS services
2. Which of the following is the correct JSON snippet to define a container image in an ECS task definition?
easy
A. "docker_image": "myrepo/myapp:latest"
B. "image": "myrepo/myapp:latest"
C. "img": "myrepo/myapp:latest"
D. "container_image": "myrepo/myapp:latest"

Solution

  1. Step 1: Recall ECS task definition JSON format

    The correct key for specifying the container image is "image" inside the container definitions.
  2. Step 2: Check each option's key

    Only "image": "myrepo/myapp:latest" uses the correct key "image". Others use incorrect keys not recognized by ECS.
  3. Final Answer:

    "image": "myrepo/myapp:latest" -> Option B
  4. Quick Check:

    Container image key = "image" [OK]
Hint: Use "image" key exactly for container images in task definitions [OK]
Common Mistakes:
  • Using incorrect keys like "container_image" or "img"
  • Confusing task definition keys with Dockerfile syntax
  • Misspelling the key name
3. Given this partial ECS task definition snippet:
{
  "containerDefinitions": [{
    "name": "web",
    "image": "nginx",
    "cpu": 256,
    "memory": 512,
    "portMappings": [{"containerPort": 80, "hostPort": 80}]
  }]
}

What will happen when this task runs?
medium
A. The container will use default CPU and memory values ignoring specified ones
B. The task will fail because hostPort cannot be 80
C. The container will run but without port 80 exposed
D. A container named 'web' runs nginx using 256 CPU units and 512 MB memory, exposing port 80

Solution

  1. Step 1: Analyze container resource settings

    The task defines CPU as 256 units and memory as 512 MB, which are valid resource allocations.
  2. Step 2: Check port mapping

    Port 80 is mapped from container to host, so port 80 is exposed correctly.
  3. Final Answer:

    A container named 'web' runs nginx using 256 CPU units and 512 MB memory, exposing port 80 -> Option D
  4. Quick Check:

    Resources and ports set correctly = runs as expected [OK]
Hint: Port mappings expose container ports to host ports directly [OK]
Common Mistakes:
  • Assuming hostPort 80 is invalid (it is allowed)
  • Ignoring resource settings and thinking defaults apply
  • Missing port exposure due to misunderstanding portMappings
4. You have this ECS task definition snippet:
{
  "containerDefinitions": [{
    "name": "app",
    "image": "myapp:latest",
    "cpu": 512,
    "memory": 1024
  }]
}

When you try to run this task, it fails with an error about missing port mappings. What is the likely fix?
medium
A. Add a portMappings section specifying containerPort and hostPort
B. Remove the cpu and memory fields
C. Change the image name to include a registry URL
D. Rename the container from 'app' to 'web'

Solution

  1. Step 1: Understand the error about missing port mappings

    If the app requires network access, port mappings must be defined to expose ports.
  2. Step 2: Identify the fix

    Adding a portMappings section with containerPort and hostPort will resolve the error by exposing the needed ports.
  3. Final Answer:

    Add a portMappings section specifying containerPort and hostPort -> Option A
  4. Quick Check:

    Missing ports error = add portMappings [OK]
Hint: Add portMappings to expose container ports when needed [OK]
Common Mistakes:
  • Removing CPU or memory which are unrelated to port errors
  • Changing image name unnecessarily
  • Renaming container without fixing ports
5. You want to update your ECS service to run a new version of your app with environment variables and increased memory. Which part of the task definition should you modify?
hard
A. Change the taskRoleArn to a new IAM role
B. Modify the family name of the task definition
C. Update the containerDefinitions section to include new environment variables and increase the memory value
D. Add a new volume definition without changing container settings

Solution

  1. Step 1: Identify where environment variables and memory are set

    Environment variables and memory limits are set inside the containerDefinitions section of the task definition.
  2. Step 2: Understand the update process

    To update the app version and resources, modify containerDefinitions with new environment variables and increased memory.
  3. Final Answer:

    Update the containerDefinitions section to include new environment variables and increase the memory value -> Option C
  4. Quick Check:

    Env vars and memory updated in containerDefinitions [OK]
Hint: Change containerDefinitions to update app settings and resources [OK]
Common Mistakes:
  • Changing taskRoleArn which controls permissions, not env or memory
  • Modifying family name which identifies task definition versions
  • Adding volumes without updating container environment or memory
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