logoChibiham
cover
🐳

Complete Resource Overview for ECS Fargate

When building applications with ECS Fargate, you might feel uncertain: "Do I have all the configurations I need?" With so many related resources, it's hard to tell what's essential versus optional.

This article comprehensively organizes the resources needed for ECS Fargate, giving you a clear picture of the whole system. The goal is to reach a state where you can confidently build from scratch.

Understanding the Three-Layer Structure

ECS Fargate resources become easier to understand when divided into three layers.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Traffic Layer β”‚ β”‚ Route53 β†’ ALB β†’ TargetGroup β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Container Layer β”‚ β”‚ Cluster β†’ Service β†’ Task β†’ Container β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Foundation Layer β”‚ β”‚ VPC, Subnet, SG, IAM, ECR, Logs β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • Foundation Layer: Network, permissions, image storageβ€”the base for containers to run
  • Container Layer: ECS-specific resources handling task definition and execution
  • Traffic Layer: The path that delivers external requests to containers

Build from the bottom up. When writing CloudFormation, keeping this order in mind helps manage dependencies effectively.

Overall Architecture Diagram

Here's a practical web application configuration visualized:

Solid lines represent traffic flow, dotted lines represent resource references.

Foundation Layer Resources

VPC / Subnet / Internet Gateway / NAT Gateway

The network foundation resource group.

ResourceARN FormatRole
VPCarn:aws:ec2:{region}:{account}:vpc/{vpc-id}Virtual network boundary
Subnetarn:aws:ec2:{region}:{account}:subnet/{subnet-id}Network segment within VPC
Internet Gatewayarn:aws:ec2:{region}:{account}:internet-gateway/{igw-id}Connection point between VPC and internet
NAT Gatewayarn:aws:ec2:{region}:{account}:natgateway/{nat-id}Outbound traffic from private subnets

Configuration Points:

  • Public Subnet: Place ALB and NAT Gateway here. Has route to Internet Gateway
  • Private Subnet: Place ECS tasks here. External communication via NAT Gateway

Why place tasks in private subnets? For security. While you can assign public IPs directly to tasks, production environments typically allow access only through ALB.

Multi-AZ Configuration: Deploy subnets in at least 2 AZs for availability. This is also an ALB requirement.

Security Group

ResourceARN FormatRole
Security Grouparn:aws:ec2:{region}:{account}:security-group/{sg-id}Inbound/outbound traffic control

Required Security Groups:

  1. ALB Security Group:

    • Inbound: 80/443 from 0.0.0.0/0
    • Outbound: Traffic to ECS task SG
  2. ECS Task Security Group:

    • Inbound: Container port (e.g., 8080) from ALB SG
    • Outbound: 0.0.0.0/0 (external API calls, ECR image pulls, etc.)

Using security group references (specifying SG IDs) enables flexible configuration without depending on IP addresses.

IAM Role

ECS uses two types of IAM roles. This is a common point of confusion.

RoleARN FormatPurpose
Task Execution Rolearn:aws:iam::{account}:role/{role-name}Used by ECS agent. Image pulls from ECR, writing to CloudWatch Logs
Task Rolearn:aws:iam::{account}:role/{role-name}Used by application in container. S3 access, DynamoDB operations, etc.
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ ECS Agent (AWS managed) β”‚ β”‚ └─ Uses Task Execution Role β”‚ β”‚ ・Pull images from ECR β”‚ β”‚ ・Send logs to CloudWatch Logs β”‚ β”‚ ・Retrieve secrets from Secrets Manager β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Container (Application) β”‚ β”‚ └─ Uses Task Role β”‚ β”‚ ・Access S3 buckets β”‚ β”‚ ・Read/write DynamoDB β”‚ β”‚ ・Send messages to SQS β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Policies needed for Task Execution Role:

  • AmazonECSTaskExecutionRolePolicy (AWS managed policy)
  • When using Secrets Manager: secretsmanager:GetSecretValue
  • When using Systems Manager Parameter Store: ssm:GetParameters

Policies needed for Task Role:

  • Access permissions for AWS resources your application needs
  • Design following the principle of least privilege

ECR (Elastic Container Registry)

ResourceARN FormatRole
ECR Repositoryarn:aws:ecr:{region}:{account}:repository/{repo-name}Container image storage and distribution

URI format when specifying images in task definitions:

{account}.dkr.ecr.{region}.amazonaws.com/{repo-name}:{tag}

ECR is referenced directly from task definitions and pulled using Task Execution Role permissions.

CloudWatch Logs

ResourceARN FormatRole
Log Grouparn:aws:logs:{region}:{account}:log-group:{log-group-name}Container log aggregation and storage

Specify in logConfiguration within the task definition. Create log groups in advance or use the autoCreateGroup option.

json
"logConfiguration": {
    "logDriver": "awslogs",
    "options": {
        "awslogs-group": "/ecs/my-app",
        "awslogs-region": "ap-northeast-1",
        "awslogs-stream-prefix": "ecs"
    }
}

Container Layer Resources

ECS Cluster

ResourceARN FormatRole
Clusterarn:aws:ecs:{region}:{account}:cluster/{cluster-name}Logical grouping of tasks and services

A cluster is like a "box"β€”it doesn't have many configuration options itself. For Fargate, FARGATE and FARGATE_SPOT automatically become available as capacity providers.

Container Insights: Enabling this enhances metrics and logs but incurs additional costs.

Task Definition

ResourceARN FormatRole
Task Definitionarn:aws:ecs:{region}:{account}:task-definition/{family}:{revision}Container blueprint

Task definitions are the core ECS resource. Here are the key configuration items:

Task-level settings:

ItemDescription
familyTask definition family name. Unit for revision management
requiresCompatibilitiesSpecify ["FARGATE"]
networkModeFixed to awsvpc for Fargate
cpuTotal task CPU (256, 512, 1024, 2048, 4096)
memoryTotal task memory (constraints based on cpu combination)
executionRoleArnTask Execution Role ARN
taskRoleArnTask Role ARN (when app uses AWS resources)

Container definition settings:

ItemDescription
nameContainer name
imageECR image URI
portMappingsPorts to expose
environmentEnvironment variables
secretsValues from Secrets Manager/Parameter Store
logConfigurationLog settings
healthCheckContainer-level health check
essentialIf true, task stops when this container stops

CPU/Memory combination constraints:

Fargate has fixed valid combinations. For example:

  • 256 CPU: 512, 1024, 2048 MB
  • 512 CPU: 1024–4096 MB
  • 1024 CPU: 2048–8192 MB
  • Increases progressively thereafter

Service

ResourceARN FormatRole
Servicearn:aws:ecs:{region}:{account}:service/{cluster-name}/{service-name}Task execution, maintenance, and scaling

A service is responsible for "keeping the specified number of tasks running." Key settings:

ItemDescription
taskDefinitionTask definition ARN to use
desiredCountNumber of tasks to launch
launchTypeSpecify FARGATE
networkConfigurationSubnets, security groups, public IP assignment
loadBalancersAssociation with target groups
deploymentConfigurationRolling update settings
enableExecuteCommandWhether to enable ECS Exec

networkConfiguration:

json
"networkConfiguration": {
    "awsvpcConfiguration": {
        "subnets": ["subnet-xxx", "subnet-yyy"],
        "securityGroups": ["sg-xxx"],
        "assignPublicIp": "DISABLED"
    }
}

Use assignPublicIp: DISABLED when placing in private subnets.

Traffic Layer Resources

Application Load Balancer

ResourceARN FormatRole
Load Balancerarn:aws:elasticloadbalancing:{region}:{account}:loadbalancer/app/{name}/{id}Traffic entry point, SSL termination
Listenerarn:aws:elasticloadbalancing:{region}:{account}:listener/app/{lb-name}/{lb-id}/{listener-id}Request reception per port
Listener Rulearn:aws:elasticloadbalancing:{region}:{account}:listener-rule/app/{lb-name}/{lb-id}/{listener-id}/{rule-id}Routing based on path or host
Target Grouparn:aws:elasticloadbalancing:{region}:{account}:targetgroup/{name}/{id}Health checks, target management

Configuration relationship:

ALB └─ Listener (443) └─ Listener Rule (path: /api/*) └─ Target Group └─ ECS Tasks (dynamically registered)

Target Group configuration points:

ItemDescription
targetTypeSpecify ip for Fargate (not instance)
protocolHTTP or HTTPS
portContainer port
healthCheckPathPath for health checks
deregistrationDelayWait time when deregistering targets (affects deployment speed)

ECS Service references the Target Group and automatically registers/deregisters task IP addresses.

Route53 / ACM

ResourceARN FormatRole
Hosted Zonearn:aws:route53:::hostedzone/{zone-id}DNS management
Certificatearn:aws:acm:{region}:{account}:certificate/{cert-id}SSL/TLS certificate

For HTTPS support:

  1. Issue certificate with ACM (DNS validation is easiest)
  2. Associate certificate with ALB HTTPS listener
  3. Create Alias record to ALB in Route53

Use Case Configuration Variations

Web Application (Primary)

The configuration explained so far. Receives HTTP requests via ALB.

Internet β†’ Route53 β†’ ALB β†’ ECS Service β†’ Tasks

Required resources: VPC suite, ALB suite, ECS suite, IAM, ECR, CloudWatch Logs

Batch Processing (Scheduled Execution)

Schedule execution pattern using EventBridge (formerly CloudWatch Events).

EventBridge Rule β†’ ECS RunTask β†’ Task (terminates after execution)

Differences from web app:

  • ALB and Target Group not needed
  • Call RunTask directly from EventBridge instead of ECS Service
  • IAM role needed for EventBridge Rule to execute ECS tasks

Additional resources needed:

ResourceARN FormatRole
EventBridge Rulearn:aws:events:{region}:{account}:rule/{rule-name}Schedule definition
EventBridge Target Rolearn:aws:iam::{account}:role/{role-name}Permission to execute RunTask

Internal API (Service Connect / Cloud Map)

Internal API pattern accessed only from other ECS services.

ECS Service A β†’ Service Connect β†’ ECS Service B (internal API)

Differences from web app:

  • ALB not needed (or use Internal ALB)
  • Use Service Connect or Cloud Map (Service Discovery)
  • Communication within same VPC

Service Connect: ECS Service Connect (introduced 2022) simplifies service-to-service communication. Enables access by service name.

Additional resources needed:

ResourceARN FormatRole
Service Connect Namespacearn:aws:servicediscovery:{region}:{account}:namespace/{namespace-id}Service namespace

Worker (SQS Integration)

Pattern for retrieving and processing messages from SQS.

SQS Queue β†’ ECS Task (polling) β†’ Processing

Differences from web app:

  • ALB not needed
  • Task Role needs SQS access permissions
  • Application needs SQS polling logic
  • Auto Scaling adjusts task count based on queue depth

Additional resources needed:

ResourceARN FormatRole
SQS Queuearn:aws:sqs:{region}:{account}:{queue-name}Message queue
Application Auto Scaling-Task count scaling

Resource Dependencies

When building with CloudFormation, you need to be aware of resource creation order. Here's a diagram showing the main dependencies:

Reading the dependency diagram: Resources at the arrow's origin are referenced by resources at the arrow's destination. This means the origin resource must be created first.

Checklist

A checklist to verify "Do I have everything I need?"

Foundation Layer

  • Created VPC
  • Created Public Subnets (2+ AZs)
  • Created Private Subnets (2+ AZs)
  • Created Internet Gateway and attached to VPC
  • Created NAT Gateway (or NAT Instance)
  • Configured route tables correctly
    • Public Subnet β†’ Internet Gateway
    • Private Subnet β†’ NAT Gateway
  • Created ALB security group
  • Created ECS task security group
  • Created Task Execution Role
  • Created Task Role (if needed)
  • Created ECR repository
  • Created CloudWatch Logs log group

Container Layer

  • Created ECS cluster
  • Created task definition
    • CPU/memory combination is valid
    • Container image URI is correct
    • Configured port mappings
    • Added log configuration
    • Set required environment variables/secrets
  • Created ECS service
    • Specified correct subnets
    • Specified correct security groups
    • Configured Target Group association

Traffic Layer (for web apps)

  • Created ALB
  • Created Target Group (targetType: ip)
  • Created Listener
  • For HTTPS support:
    • Issued ACM certificate
    • Associated certificate with HTTPS listener
  • Created Route53 record (for custom domains)

Summary

This article organized ECS Fargate application resources into a three-layer structure.

  • Foundation Layer: VPC, Subnet, Security Group, IAM Role, ECR, CloudWatch Logs
  • Container Layer: Cluster, Task Definition, Service
  • Traffic Layer: ALB, Target Group, Listener, Route53, ACM

Particularly confusing points:

  1. Two IAM Roles: Distinguishing Task Execution Role (for ECS agent) from Task Role (for app)
  2. Target Group Type: Use ip type for Fargate
  3. Network Configuration: Private subnet + NAT Gateway configuration is production-ready

Use the checklist to verify you have all necessary resources as you build.