Bird
Raised Fist0
AWScloud~5 mins

Task definitions in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you want to run containers on AWS ECS, you need to tell AWS what your container looks like and how it should run. A task definition is like a recipe that describes your container, what resources it needs, and how it connects to the network.
When you want to run a web app inside a container on AWS ECS.
When you need to specify environment variables or ports for your container.
When you want to update your container image and redeploy with new settings.
When you want to run multiple containers together as one task.
When you want to control CPU and memory limits for your container.
Config File - task-definition.json
task-definition.json
{
  "family": "my-app-task",
  "networkMode": "awsvpc",
  "containerDefinitions": [
    {
      "name": "my-app-container",
      "image": "nginx:1.23",
      "portMappings": [
        {
          "containerPort": 80,
          "hostPort": 80,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "memory": 256,
      "cpu": 128
    }
  ],
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512"
}

family: The name of this task definition group.

networkMode: How the network is set up; awsvpc means each task gets its own network interface.

containerDefinitions: Describes the container(s) to run, including image, ports, CPU, and memory.

requiresCompatibilities: Specifies this task runs on AWS Fargate, a serverless container service.

cpu and memory: Total CPU and memory for the task.

Commands
This command tells AWS ECS to create a new task definition using the details in the JSON file. It registers the recipe for your container.
Terminal
aws ecs register-task-definition --cli-input-json file://task-definition.json
Expected OutputExpected
{ "taskDefinition": { "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/my-app-task:1", "family": "my-app-task", "revision": 1, "status": "ACTIVE", "requiresAttributes": [ { "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" } ], "containerDefinitions": [ { "name": "my-app-container", "image": "nginx:1.23", "cpu": 128, "memory": 256, "essential": true, "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" } }
→
--cli-input-json - Specifies the JSON file with the task definition details.
This command lists all versions of the task definitions registered under the family name 'my-app-task'. It helps you see your task definition history.
Terminal
aws ecs list-task-definitions --family-prefix my-app-task
Expected OutputExpected
TASKDEFINITIONARNS arn:aws:ecs:us-east-1:123456789012:task-definition/my-app-task:1
→
--family-prefix - Filters task definitions by family name.
This command shows detailed information about the specific task definition version 1. Useful to verify the settings before running tasks.
Terminal
aws ecs describe-task-definition --task-definition my-app-task:1
Expected OutputExpected
{ "taskDefinition": { "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/my-app-task:1", "family": "my-app-task", "revision": 1, "containerDefinitions": [ { "name": "my-app-container", "image": "nginx:1.23", "cpu": 128, "memory": 256, "essential": true, "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" } }
→
--task-definition - Specifies which task definition version to describe.
Key Concept

If you remember nothing else from this pattern, remember: a task definition is the detailed recipe AWS ECS uses to run your container exactly how you want.

Common Mistakes
Forgetting to specify the network mode as 'awsvpc' when using Fargate.
Fargate requires 'awsvpc' network mode to assign network interfaces properly; without it, the task won't run.
Always set "networkMode": "awsvpc" in your task definition JSON when using Fargate.
Using hostPort different from containerPort without understanding port mapping.
If ports don't match or are blocked, your container won't receive traffic as expected.
Set hostPort and containerPort to the same value unless you have a specific reason to map differently.
Not registering a new task definition version after changing the container image.
ECS will keep running the old version until you register and deploy the new task definition.
Register a new task definition version with updated image and deploy it.
Summary
Create a task definition JSON file describing your container and resources.
Register the task definition with AWS ECS using the AWS CLI.
List and describe task definitions to verify and manage versions.

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