</>
Skip to content
AWS lessons (13/47)

AWS — ECS

What is ECS?

Managed container orchestration service for Docker containers.

Create cluster

aws ecs create-cluster --cluster-name my-cluster

Register task definition

{
  "family": "my-task",
  "containerDefinitions": [
    {
      "name": "my-container",
      "image": "nginx:latest",
      "cpu": 256,
      "memory": 512,
      "portMappings": [
        {"containerPort": 80, "hostPort": 80}
      ]
    }
  ]
}
aws ecs register-task-definition --cli-input-json file://task.json

Run task

aws ecs run-task \
  --cluster my-cluster \
  --task-definition my-task:1

Create service

aws ecs create-service \
  --cluster my-cluster \
  --service-name my-service \
  --task-definition my-task:1 \
  --desired-count 2 \
  --launch-type FARGATE \
  --network-configuration "awsvpcConfiguration={subnets=[subnet-xxx],securityGroups=[sg-xxx]}"

Fargate vs EC2

FeatureFargateEC2
ServerlessYesNo
Instance mgmtAWSYou
PricingPer taskPer instance
FlexibilityLimitedFull

Scaling

aws ecs update-service \
  --cluster my-cluster \
  --service my-service \
  --desired-count 4

Auto scaling

aws application-autoscaling register-scalable-target \
  --service-namespace ecs \
  --scalable-dimension ecs:service:DesiredCount \
  --resource-id service/my-cluster/my-service \
  --min-capacity 2 \
  --max-capacity 10

Best practices

  1. Use Fargate for simplicity
  2. Set up auto scaling
  3. Use task roles for permissions
  4. Monitor with CloudWatch

Mini Practice

  1. Create a cluster
  2. Deploy a task definition
  3. Create a service
  4. Set up auto scaling

Up Next

Continue with EKS - Kubernetes Service.

Related Topics

Frequently Asked Questions about ECS

What is ECS in AWS?

ECS is a fundamental concept in AWS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn ECS?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn ECS.

Why is ECS important in AWS?

ECS is essential for AWS development. Understanding this concept will help you write better code and solve real-world problems more effectively.