Bootstrap FreeKB - Amazon Web Services (AWS) Elastic Container Service (ECS) - Create a Task Definition using the AWS CLI
Amazon Web Services (AWS) Elastic Container Service (ECS) - Create a Task Definition using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

A Task Definition is kind of like a Dockerfile or a Kubernetes deployment YAML file, in that it contains keys and values that are used to define how something should be, such as the settings for a container, such as:

  • which Docker image to use
  • which ports to expose
  • how much CPU and memory to reserve
  • how to collect logs
  • environment variables

A Task Definition can be used to create one or more Tasks or one or more Services.

  • Task is created when you run a Task directly, which launches container(s) (defined in the task definition) until they are stopped or exit on their own, at which point they are not replaced automatically. Running Tasks directly is ideal for short-running jobs, perhaps as an example of things that were accomplished via CRON

  • Service is used to guarantee that you always have some number of Tasks running at all times. If a Task's container exits due to an error, or the underlying EC2 instance fails and is replaced, the ECS Service will replace the failed Task. This is why we create Clusters so that the Service has plenty of resources in terms of CPU, Memory and Network ports to use. To us it doesn't really matter which instance Tasks run on so long as they run. A Service configuration references a Task definition. A Service is responsible for creating Tasks.

 

The aws ecs register-task-definition command can be used to create an Elastic Container Service (ECS) Task Definition. Almost always, you will want to first create a JSON file that contain the details of the task definition. For example, let's say task-definition.json includes the following, to create a task definitation for an nginx container.

~]$ cat task-definition.json 
{
  "family": "nginx",
  "networkMode": "awsvpc",
  "cpu": "1024",
  "memory": "3072",
  "containerDefinitions": [
      {
          "name": "nginx",
          "image": "nginx:latest",
          "cpu": 0,
          "portMappings": [
          {
           "name": "nginx-80-tcp",
           "containerPort": 80,
           "hostPort": 80,
           "protocol": "tcp",
           "appProtocol": "http"
         }
       ],
       "essential": true,
       "environment": [],
       "environmentFiles": [],
       "mountPoints": [],
       "volumesFrom": [],
       "ulimits": []
    }
  ],
  "placementConstraints": [],
  "requiresCompatibilities": [
      "FARGATE"
  ],
  "runtimePlatform": {
      "cpuArchitecture": "X86_64",
      "operatingSystemFamily": "LINUX"
  }
}

 

The aws ecs register-task-definition command can then be used to create the task definition.

aws ecs register-task-definition --cli-input-json file://task-definition.json

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9ba33f in the box below so that we can be sure you are a human.