Bootstrap FreeKB - Amazon Web Services (AWS) Elastic Container Service (ECS) - Create Services using the AWS CLI
Amazon Web Services (AWS) Elastic Container Service (ECS) - Create 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.

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.

 

Before creating an ECS Service, you will need to create a Task Definition. Check out my article Create an Elastic Container Service (ECS) Task Definition using the AWS CLI. Let's say you have a Task Definition named nginx:1.

~]$ aws ecs list-task-definitions
{
    "taskDefinitionArns": [
        "arn:aws:ecs:us-east-1:123456789012:task-definition/nginx:1"
    ]
}

 

The aws ecs create-service command can be used to create an ECS Service.

aws ecs create-service --cluster my-ecs-cluster --service-name my-nginx-service --task-definition nginx:1 --desired-count 1

 

If you want the service to run on one of your Amazon Web Services Load Balancers, you can use the aws elbv2 describe-target-groups command to get the Amazon Resource Number (ARN) of your Load Balancer Target Group.

~]# aws elbv2 describe-target-groups
{
    "TargetGroups": [
        {
            "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/b12348d3a5abcd1d",

 

Or, you can create a JSON file that contains something like this, including the Amazon Resource Number (ARN) of your Load Balancer Target Group.

  • containerName must be an exact match of the container name in your Task Definition
{
    "cluster": "my-ecs-cluster",
    "serviceName": "my-nginx-service",
    "launchType": "FARGATE",
    "platformVersion": "LATEST",
    "taskDefinition": "nginx:1",
    "loadBalancers": [
        {
            "targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/b12348d3a5abcd1d",
            "containerName": "nginx",
            "containerPort": 80
        }
    ],
    "networkConfiguration": {
      "awsvpcConfiguration": {
        "subnets": [
          "subnet-12345678912345678"
        ],
        "securityGroups": [
          "sg-12345678912345678"
        ],
        "assignPublicIp": "ENABLED"
      }
    },
    "desiredCount": 1
}

 

And then use the aws ecs create-service command with the --cli-input-json option.

aws ecs create-service --cli-input-json file://ecs-nginx-service.json

 




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 b59883 in the box below so that we can be sure you are a human.