Amazon ECS task definition with a custom entrypoint command

Nathan Peck profile picture
Nathan Peck
Senior Developer Advocate at AWS

The following snippets show how to create a task definition which runs a custom command when the container image starts up. This can be especially useful when overriding the existing entrypoint command in a generic image, such as when running a background batch job.

Example: Custom Alpine image to run ping

The following task defintions launch a standard Alpine Linux container and the task definition tells ECS to run the ping command inside the container:

  • AWS CloudFormation
  • Raw JSON
File: task-definition.ymlLanguage: yml
AWSTemplateFormatVersion: '2010-09-09'
Description: An example task definition which has a custom entrypoint
             and command that overrides the default container image entrypoint

Resources:
  PingTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: alpine-ping
      Cpu: 256
      Memory: 128
      ContainerDefinitions:
        - Name: alpine-ping
          Image: public.ecr.aws/docker/library/alpine:latest
          Essential: true
          EntryPoint:
            - ping

          # Note that separate command line arguments must be
          # expressed as items in an array, otherwise they will
          # all by stuffed into the first argument passed to the
          # entrypoint
          Command:
            - "-c"
            - '4'
            - amazon.com

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

Language: sh
aws cloudformation deploy \
   --stack-name sample-task-definition \
   --template-file task-definition.yml

Example: Custom load test container

With this snippet you can create a custom load test container that utilizes hey , and then pass commands to it to configure how many requests to send, and which URL to send them too.

First define a custom image that has the desired entrypoint already set, but no commands:

Language: Dockerfile
FROM public.ecr.aws/amazonlinux/amazonlinux:latest
RUN curl https://hey-release.s3.us-east-2.amazonaws.com/hey_linux_amd64 -o /usr/bin/hey && chmod +x /usr/bin/hey
ENTRYPOINT [ "/usr/bin/hey" ]

Next create a task definition that keeps the existing entrypoint from the image, but sets a custom command to run against that entrypoint:

File: load-test-task-definition.ymlLanguage: yml
AWSTemplateFormatVersion: '2010-09-09'
Description: An example task definition which has a custom entrypoint
             and command that overrides the default container image entrypoint

Resources:
  HeyLoadTestTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: hey-load-test
      Cpu: 256
      Memory: 128
      ContainerDefinitions:
        - Name: hey

          # Example URI of the image from the Dockerfile above
          Image: 012345678910.dkr.ecr.us-west-2.amazonaws.com/test-suite/hey:latest
          Essential: true

          # Keep the existing entrypoint but just pass a custom command
          Command:
            # Send 100 concurrent requests at a time
            - '-c'
            - '100'
            # Send 100,000 total requests
            - '-n'
            - '100000'
            # URI to send to
            - http://your-uri-here

The resulting task definition can now be launched with ECS RunTask API to start a containerized instance of hey with the given load test parameters.

See Also