Back to all patterns

Table of Contents

About

Development Tool

cloudformation

Type

pattern

Amazon ECS task with maximum lifespan

An Amazon ECS task that will run for a specified duration, then be automatically stopped.

Maish Saidel-Keesing
Maish Saidel-Keesing
Senior Developer Advocate at AWS

About

In some cases you may wish to limit how long a task can run for. This can be used to prevent a batch job from running too long, or to provide a maximum lifespan for an ephemeral game server or similar server that should boot clients after a period of time.

This pattern will show how to use a sidecar container to trigger Amazon ECS task to stop the task after a duration that you set.

Task Definition

Create a task definition using one of the following methods:

File: task-definition.yml Language: yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
AWSTemplateFormatVersion: '2010-09-09'
Description: An example task definition that has a maximum lifespan. After
             the duration is up, the task automatically stops.

Resources:

  SampleTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: lifespan
      Cpu: 256
      Memory: 512
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      ContainerDefinitions:

        # The application container
        - Name: nginx
          Image: public.ecr.aws/nginx/nginx:mainline
          Essential: true

        # Sidecar container that gives this task a lifetime
        - Name: lifespan
          Image: public.ecr.aws/docker/library/busybox:stable
          Command:
            - "sh"
            - "-c"
            - "sleep $LIFESPAN_SECONDS"
          Environment:
            - Name: LIFESPAN_SECONDS
              Value: 60
          Essential: true

Deploy the CloudFormation template above by using the AWS CloudFormation web console. Alternatively you can deploy from the command line with:

aws cloudformation deploy \
   --stack-name sample-task-definition \
   --template-file task-definition.yml
File: task-definition.json Language: json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
  "family": "lifespan",
  "networkMode": "awsvpc",
  "requiresCompatibilities": [
    "EC2",
    "FARGATE"
  ],
  "cpu": "256",
  "memory": "512",
  "containerDefinitions": [
    {
      "name": "nginx",
      "image": "public.ecr.aws/nginx/nginx:mainline",
      "essential": true
    },
    {
      "name": "lifespan",
      "image": "public.ecr.aws/docker/library/busybox:stable",
      "essential": true,
      "command": [
        "sh",
        "-c",
        "sleep $LIFESPAN_SECONDS"
      ],
      "environment": [
        {
          "name": "LIFESPAN_SECONDS",
          "value": "60"
        }
      ]
    }
  ]
}

In the ECS console, click “Create new task definition with JSON” and paste the JSON into the task definition editor.

Alternatively you can download the JSON file and pass it as a parameter to the AWS CLI:

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

Test it Out

You can use the Amazon ECS console to launch a copy of the task as a standalone task. After 60 seconds you should see the task stop with a final message of “Essential container in task exited”