Serverless Container Operations with AWS Fargate - Usage Patterns for ECS and EKS
Run serverless containers without managing instances. Learn how to choose between ECS and EKS, and optimize costs through task sizing.
How Fargate Works and How It Differs from EC2 Launch Type
Fargate is a serverless compute engine for ECS and EKS. With the EC2 launch type, you manage the provisioning, scaling, and patching of EC2 instances that run your containers, but Fargate completely abstracts all of this away. You simply specify vCPU (0.25 to 16) and memory (0.5 GiB to 120 GiB) in your task definition, and AWS automatically provisions the container execution environment. Each task runs in a Firecracker-based isolated microVM, eliminating kernel sharing between tasks and strengthening security isolation. Access to the host OS is impossible, and privileged containers cannot run. Platform versions (ECS) manage the OS kernel and runtime version; specifying LATEST automatically applies the latest stable version.
Choosing Between ECS on Fargate and EKS on Fargate
ECS on Fargate is an AWS-native container orchestration that can be operated simply with three concepts: task definitions, services, and clusters. It provides ALB integration, service mesh via Service Connect, and service discovery via CloudMap out of the box. ECS Exec lets you attach an interactive shell to containers within tasks for production debugging. EKS on Fargate runs Kubernetes Pods on Fargate. Fargate Profiles define namespace and label selectors, and matching Pods are automatically scheduled to Fargate. Choose this when you want to leverage the Kubernetes ecosystem (Helm, Argo CD, Prometheus, etc.) or when standardizing on Kubernetes across multiple clouds. However, EKS on Fargate has limitations: DaemonSets cannot be used, EBS volumes cannot be mounted (EFS is supported), GPU instances are unavailable, and startup is slower than EC2 nodes because an ENI is assigned per Pod.
Cost Optimization
Fargate pricing is pay-per-use based on vCPU-seconds and memory GB-seconds. Proper task sizing is the key to cost optimization. Use CloudWatch Container Insights to measure actual CPU and memory utilization, then reduce resources for over-provisioned tasks. Fargate Spot offers up to 70% discount compared to on-demand, with a 30-second grace period after a SIGTERM signal is sent upon interruption. It's well-suited for interrupt-tolerant workloads like queue-based workers and batch processing. Compute Savings Plans apply across Fargate, Lambda, and EC2, offering up to 50% discount with a 1-year commitment. For a systematic understanding of Fargate, check out related books (Amazon).
Design Best Practices and Pitfalls
A valid matrix of vCPU and memory combinations exists; you cannot specify arbitrary combinations (e.g., 0.25 vCPU only allows 0.5/1/2 GiB). If you don't properly set the health check grace period (healthCheckGracePeriodSeconds), containers with slow startup enter an infinite loop of being stopped and restarted due to health check failures. Ephemeral storage defaults to 20 GiB (max 200 GiB) and is lost on task termination, so use EFS or S3 for persistent data. The awsvpc network mode is mandatory, and since each task gets an ENI, watch for subnet IP address exhaustion. For workloads running many tasks, provision at least a /19 CIDR or enable ENI Trunking.
Fargate vs. App Runner
App Runner is an even higher-abstraction container service than Fargate that automatically builds, deploys, and scales from source code or container images. No VPC configuration or task definitions are needed, enabling the fastest path to deploying web applications and APIs. Fargate, on the other hand, offers VPC placement, fine-grained security group control, sidecar containers, multi-container task definitions, EFS mounts, and GPU support (EC2 launch type) for advanced networking and customization. App Runner suits steady-traffic web APIs, while ECS on Fargate is ideal for batch processing, multi-container configurations, and workloads requiring direct access to VPC resources. EKS on Fargate is chosen when reusing existing Kubernetes manifests and toolchains.
Summary
Fargate is a serverless compute engine that eliminates infrastructure management from container operations. Start simply with ECS on Fargate, and migrate to EKS on Fargate when Kubernetes is needed. Optimize costs with Fargate Spot and Savings Plans, and continuously monitor resource utilization with Container Insights. Understanding typical pitfalls like subnet IP exhaustion, valid vCPU/memory combinations, and health check settings ensures stable production operations.