Bootstrap FreeKB - Amazon Web Services (AWS) Elastic Container Service (ECS) - Scale Services using the AWS CLI
Amazon Web Services (AWS) Elastic Container Service (ECS) - Scale Services using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

The aws ecs list-clusters command can be used to list your Elastic Container Service (ECS) Clusters.

~]$ aws ecs list-clusters
{
    "clusterArns": [
        "arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster"
    ]
}

 

And then the aws ecs list-services command can be used to list the services that have been created in the cluster.

]$ aws ecs list-services --cluster arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster
{
    "serviceArns": [
        "arn:aws:ecs:us-east-1:123456789012:service/my-ecs-cluster/nginx-service"
    ]
}

 

A Task Definition is kind of like a Dockerfile or a Kubernetes deployment YAML file, in that it contains keys and values that are used to define how something should be, such as the settings for a container, such as:

  • which Docker image to use
  • which ports to expose
  • how much CPU and memory to reserve
  • how to collect logs
  • environment variables

A Task Definition can be used to create one or more Tasks or one or more Services.

  • Task is created when you run a Task directly, which launches container(s) (defined in the task definition) until they are stopped or exit on their own, at which point they are not replaced automatically. Running Tasks directly is ideal for short-running jobs, perhaps as an example of things that were accomplished via CRON

  • Service is used to guarantee that you always have some number of Tasks running at all times. If a Task's container exits due to an error, or the underlying EC2 instance fails and is replaced, the ECS Service will replace the failed Task. This is why we create Clusters so that the Service has plenty of resources in terms of CPU, Memory and Network ports to use. To us it doesn't really matter which instance Tasks run on so long as they run. A Service configuration references a Task definition. A Service is responsible for creating Tasks.

 

 

And then the aws ecs describe-services command can be used to return JSON details for a service, including:

  • desiredCount
  • runningCount
  • pendingCount
]$ aws ecs describe-services --cluster arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster --service arn:aws:ecs:us-east-1:123456789012:service/my-ecs-cluster/nginx-service
{
    "services": [
        {
            "serviceArn": "arn:aws:ecs:us-east-1:123456789012:service/my-ecs-cluster/nginx-service",
            "serviceName": "nginx-service",
            "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster",
            "loadBalancers": [
                {
                    "targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/ecs-target-group/128b66748f625c66",
                    "containerName": "nginx",
                    "containerPort": 80
                }
            ],
            "desiredCount": 2,
            "runningCount": 2,
            "pendingCount": 0,
...

 

And then the aws ecs update-service command can be used to update the desired count of containers for the service.

aws ecs update-service \
--cluster arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster \
--service arn:aws:ecs:us-east-1:123456789012:service/my-ecs-cluster/nginx-service \
--desired-count 3

 

Or, the --force-new-deployment option can be used to, as the name implies, replace the existing deployment with a new deployment.

aws ecs update-service \
--cluster arn:aws:ecs:us-east-1:123456789012:cluster/my-ecs-cluster \
--service arn:aws:ecs:us-east-1:123456789012:service/my-ecs-cluster/nginx-service \
--force-new-deployment

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2de239 in the box below so that we can be sure you are a human.