Skip to main content

Overview

The ECS Cluster resource deploys containerised applications on AWS ECS with a multi-service architecture. A single cluster hosts one or more services that share a load balancer. Each service specifies its own capacity provider (“FARGATE”, “FARGATE_SPOT”, or “EC2”), containers, CPU/memory, and scaling rules.

Resource Class

EcsCluster is the default export from the module.

Basic Usage

Single Service

Multi-Service with Routing

Worker Cluster (No Load Balancer)

Cluster Props

Cluster Configuration

Service Props

Each service in the services array has these properties:

Routing Configuration

Container Configuration

Each container in the containers array has these properties: The first container with a port is the primary container and receives load balancer traffic. All other containers act as sidecars.

Capacity Providers

FARGATE

Serverless containers. No infrastructure to manage.

FARGATE_SPOT

Up to 70% cheaper than standard Fargate. Tasks may be interrupted.

EC2

Run containers on EC2 instances you control.

Auto-Scaling

CPU-Based Scaling

Memory-Based Scaling

Secrets and Environment Variables

Environment Variables

SSM Parameter Store Secrets

Secrets Manager Imports

Multi-Container Tasks

The first container with a port is the primary container. Additional containers run as sidecars.

Connections

Services can declare resources they need to connect to. The construct creates security group rules for network resources and IAM grants for storage resources.

Methods

Get Load Balancer

Get Service

Get All Services

Get Cluster

Get URL

Complete Example

Best Practices

  1. Use FARGATE for production workloads requiring isolation
  2. Use FARGATE_SPOT for batch processing and fault-tolerant workloads
  3. Set appropriate CPU/memory based on application needs
  4. Enable auto-scaling for production with min 2 tasks
  5. Use secrets for sensitive data, not environment variables
  6. Configure routing when deploying multiple services with ports
  7. Use connections for declarative resource access

Next Steps

Compute Factory

Provision an ECS cluster through the higher-level compute factory pattern.

Application Load Balancer

Configure the ALB that fronts your cluster services.

ECR Repository

Store the container images your services pull at deploy time.

Security Group

Control network access to and from your ECS services.