Amazon Elastic Container Service
Developer Guide (API Version 2014-11-13)

Example Task Definitions

This section provides some task definition examples that you can use to start creating your own task definitions. For more information, see Task Definition Parameters and Creating a Task Definition.

Example: Webserver

The following is an example task definition using the Fargate launch type that sets up a web server:

{ "containerDefinitions": [ { "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "entryPoint": [ "sh", "-c" ], "essential": true, "image": "httpd:2.4", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group" : "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "name": "sample-fargate-app", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "cpu": "256", "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "family": "fargate-task-definition", "memory": "512", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ] }

Example: WordPress and MySQL

The following example specifies a WordPress container and a MySQL container that are linked together. This WordPress container exposes the container port 80 on the host port 80. The security group on the container instance would need to open port 80 in order for this WordPress installation to be accessible from a web browser.

For more information about the WordPress container, see the official WordPress Docker Hub repository at For more information about the MySQL container, go to the official MySQL Docker Hub repository at

{ "containerDefinitions": [ { "name": "wordpress", "links": [ "mysql" ], "image": "wordpress", "essential": true, "portMappings": [ { "containerPort": 80, "hostPort": 80 } ], "memory": 500, "cpu": 10 }, { "environment": [ { "name": "MYSQL_ROOT_PASSWORD", "value": "password" } ], "name": "mysql", "image": "mysql", "cpu": 10, "memory": 500, "essential": true } ], "family": "hello_world" }


If you use this task definition with a load balancer, you need to complete the WordPress setup installation through the web interface on the container instance immediately after the container starts. The load balancer health check ping expects a 200 response from the server, but WordPress returns a 301 until the installation is completed. If the load balancer health check fails, the load balancer deregisters the instance.

Example: awslogs Log Driver

The following example demonstrates how to use the awslogs log driver in a task definition that uses the Fargate launch type. The nginx container sends its logs to the ecs-log-streaming log group in the us-west-2 region. For more information, see Using the awslogs Log Driver.

{ "containerDefinitions": [ { "memory": 128, "portMappings": [ { "hostPort": 80, "containerPort": 80, "protocol": "tcp" } ], "essential": true, "name": "nginx-container", "image": "nginx", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "ecs-log-streaming", "awslogs-region": "us-west-2", "awslogs-stream-prefix": "fargate-task-1" } }, "cpu": 0 } ], "networkMode": "awsvpc", "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "memory": "2048", "cpu": "1024", "requiresCompatibilities": [ "FARGATE" ], "family": "example_task_1" }

Example: Amazon ECR Image and Task Definition IAM Role

The following example uses an Amazon ECR image called aws-nodejs-sample with the v1 tag from the registry. The container in this task inherits IAM permissions from the arn:aws:iam::123456789012:role/AmazonECSTaskS3BucketRole role. For more information, see IAM Roles for Tasks.

{ "containerDefinitions": [ { "name": "sample-app", "image": "", "memory": 200, "cpu": 10, "essential": true } ], "family": "example_task_3", "taskRoleArn": "arn:aws:iam::123456789012:role/AmazonECSTaskS3BucketRole" }

Example: Entrypoint with Command

The following example demonstrates the syntax for a Docker container that uses an entry point and a command argument. This container pings four times and then exits.

{ "containerDefinitions": [ { "memory": 32, "essential": true, "entryPoint": [ "ping" ], "name": "alpine_ping", "readonlyRootFilesystem": true, "image": "alpine:3.4", "command": [ "-c", "4", "" ], "cpu": 16 } ], "family": "example_task_2" }

Example: Container Dependency

This example demonstrates the syntax for a task definition with multiple containers where container dependency is specified. In the following task definition, the envoy container must reach a healthy status, determined by the required container healthcheck parameters, before the app container will start. For more information, see Container Dependency.

{ "family": "appmesh-gateway", "proxyConfiguration":{ "type": "APPMESH", "containerName": "envoy", "properties": [ { "name": "IgnoredUID", "value": "1337" }, { "name": "ProxyIngressPort", "value": "15000" }, { "name": "ProxyEgressPort", "value": "15001" }, { "name": "AppPorts", "value": "9080" }, { "name": "EgressIgnoredIPs", "value": "," } ] }, "containerDefinitions": [ { "name": "app", "image": "application_image", "portMappings": [ { "containerPort": 9080, "hostPort": 9080, "protocol": "tcp" } ], "essential": true, "dependsOn": [ { "containerName": "envoy", "condition": "HEALTHY" } ] }, { "name": "envoy", "image": "", "essential": true, "environment": [ { "name": "APPMESH_VIRTUAL_NODE_NAME", "value": "mesh/meshName/virtualNode/virtualNodeName" }, { "name": "ENVOY_LOG_LEVEL", "value": "info" } ], "healthCheck": { "command": [ "CMD-SHELL", "echo hello" ], "interval": 5, "timeout": 2, "retries": 3 } } ], "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "networkMode": "awsvpc" }