ECS Task Definition Generator

Configure Fargate and EC2 Task Definitions with sidecar containers, CloudWatch logs, and precise resource mappings.

Container Definitions

Container 1
task-definition.json
Valid
1

Overview

The AWS ECS Task Definition Generator allows you to visually build JSON blueprints for Amazon Elastic Container Service (ECS) and AWS Fargate.

A task definition is required to run Docker containers in ECS. It specifies the container image, CPU/memory limits, IAM roles, networking modes, and logging configurations. Writing these complex JSON documents manually is prone to configuration errors that prevent containers from starting. This tool provides a structured interface to generate validation-passed task definitions instantly.

How It Works

1. Define the Task: Give your task family a name and select the launch type—Fargate (serverless) or EC2. Fargate requires awsvpc network mode.

2. Configure Compute: Set the task-level CPU and Memory limits. Fargate has strict supported combinations for these values, which the generator automatically enforces.

3. Add Container Definitions: Specify the Docker image URI, port mappings, and environment variables. You can add multiple containers (e.g., an application container and a sidecar container) within a single task.

4. Export JSON: The resulting JSON file is ready to be registered via the AWS CLI (aws ecs register-task-definition --cli-input-json file://task.json) or deployed via Terraform.

Best Practices

  • Always specify a Task Execution Role (used by the ECS agent to pull images and publish logs) AND a Task Role (used by your application code to access AWS services like S3 or DynamoDB).
  • Use 'awslogs' (CloudWatch Logs) as the log driver for all containers to ensure you capture standard output (stdout) for debugging.
  • Do not hardcode sensitive data in environment variables. Use 'secrets' instead of 'environment' in your container definition to pull values from AWS Secrets Manager or SSM Parameter Store.
  • Implement health checks at the container definition level to allow ECS to automatically restart unhealthy containers.

Common Mistakes

  • Setting memory/CPU limits at the container level that exceed the total limits set at the task level.
  • Forgetting to map container ports to host ports when using the EC2 launch type.
  • Using the 'bridge' network mode with AWS Fargate (Fargate only supports 'awsvpc' mode).
  • Using the 'latest' tag for container images. Always pin to a specific version (e.g., 'nginx:1.24.0') to ensure reproducible deployments.

Security Recommendations

  • Run containers as a non-root user by setting the 'user' parameter in the container definition.
  • Mount root filesystems as read-only ('readonlyRootFilesystem': true) to prevent malware from modifying system files.
  • Use ECR (Elastic Container Registry) for your images and enable 'Scan on Push' to detect vulnerabilities before deployment.

Production Tips

  • If your application needs temporary storage, configure Ephemeral Storage in the task definition (Fargate supports up to 200 GB).
  • For logging at scale, consider using the 'awsfirelens' log router to send logs to external destinations like Datadog, Splunk, or Elasticsearch.
  • Leverage ECS capacity providers to mix Fargate and Fargate Spot pricing models for cost optimization.

Frequently Asked Questions

What is the difference between Task Execution Role and Task Role?
The Task Execution Role grants permissions to the ECS infrastructure to pull container images from ECR and push logs to CloudWatch. The Task Role grants permissions to your application running inside the container (e.g., allowing your Node.js app to read from an S3 bucket).
Why is my ECS Task stuck in 'PENDING'?
Tasks often stick in PENDING if they lack a route to the internet to pull the Docker image from ECR/DockerHub, or if the Task Execution Role lacks the necessary permissions. Ensure your VPC subnets have a NAT Gateway or Internet Gateway.
Can I run multiple containers in one Task Definition?
Yes, this is called the sidecar pattern. You can define multiple containers (e.g., a web server and a log router) in the same task definition. They will be scheduled on the same underlying host and share the same network namespace.

Related Generators