Bootstrap FreeKB - Amazon Web Services (AWS) Elastic Container Service (ECS) - Deploying a container to an Elastic Container Service (ECS) Cluster
Amazon Web Services (AWS) Elastic Container Service (ECS) - Deploying a container to an Elastic Container Service (ECS) Cluster


Elastic Container Service (ECS) is an Amazon Web Services (AWS) service that can be used to manage containers, similar to Kubernetes and OpenShift. This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

First you will create a cluster. The aws ecs create-cluster command can be used to create an Elastic Container Service (ECS) Cluster.

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

 

Next you will create a task definition. 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. 

The aws ecs register-task-definition command can be used to create an Elastic Container Service (ECS) Task Definition. Almost always, you will want to first create a JSON file that contain the details of the task definition. For example, let's say task-definition.json includes the following, to create a task definitation for an nginx container.

~]$ cat task-definition.json 
{
  "family": "nginx",
  "networkMode": "awsvpc",
  "cpu": "1024",
  "memory": "3072",
  "containerDefinitions": [
      {
          "name": "nginx",
          "image": "nginx:latest",
          "cpu": 0,
          "portMappings": [
          {
           "name": "nginx-80-tcp",
           "containerPort": 80,
           "hostPort": 80,
           "protocol": "tcp",
           "appProtocol": "http"
         }
       ],
       "essential": true,
       "environment": [],
       "environmentFiles": [],
       "mountPoints": [],
       "volumesFrom": [],
       "ulimits": []
    }
  ],
  "placementConstraints": [],
  "requiresCompatibilities": [
      "FARGATE"
  ],
  "runtimePlatform": {
      "cpuArchitecture": "X86_64",
      "operatingSystemFamily": "LINUX"
  }
}

 

The aws ecs register-task-definition command can then be used to create the task definition.

aws ecs register-task-definition --cli-input-json file://task-definition.json

 

In this example, we are going to deploy the service to run on an Application Load Balancer so let's Create an Elastic Load Balancer (ELB) using the AWS CLI.

aws elbv2 create-load-balancer 
--name my-application-load-balancer
--subnets subnet-111222333444555666 subnet-999888777666555444
--security-groups sg-12345678901234567

 

Let's get the ID of your Virtual Private Cloud (VPC) using the AWS CLI.

~]$ aws ec2 describe-vpcs --filter "Name=is-default,Values=true" | grep -i VpcId
            "VpcId": "vpc-12345678912345678",

 

And then Create an Elastic Load Balancer (ELB) Target Group using the AWS CLI using the ID of your Virtual Private Cloud from the prior command. --target-type must be ip.

aws elbv2 create-target-group --name my-ecs-target-group --protocol HTTP --port 80 --target-type ip --vpc-id vpc-12345678912345678

 

Let's get the Amazon Resource Number (ARN) of your Elastic Load Balancer using the AWS CLI.

~]$ aws elbv2 describe-load-balancers --query 'LoadBalancers[?LoadBalancerName==`my-application-load-balancer`]' | grep -i LoadBalancerArn
        "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-application-load-balancer/207642041c61a1dc",

 

And then Create an Elastic Load Balancer (ELB) Listener using the AWS CLI, using the ARN of your Load Balancer returned from the prior command.

aws elbv2 create-listener --load-balancer-arn <your load balancer ARN> \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=<your target group ARN>

 

Now you are almost created to create a service using the Task Definition that will be run on the Load Balancer. But first, let's list your Virtual Private Cloud (VPC) Subnets using the AWS CLI.

~]$ aws ec2 describe-subnets --filter "Name=vpc-id,Values=vpc-123456789012345678" | grep -i SubnetId
            "SubnetId": "subnet-11122233344455566",
            "SubnetId": "subnet-22233344455566677",
            "SubnetId": "subnet-33344455566677788",
            "SubnetId": "subnet-44455566677788899",
            "SubnetId": "subnet-55566677788899900",
            "SubnetId": "subnet-66677788899900011",

 

And you also want to get your Security Group ID using the AWS CLI.

~]$ aws ec2 describe-security-groups --filters Name=group-name,Values=my-security-group | grep -i GroupId
            "GroupId": "sg-123456789012345678"

 

And now you can create a JSON file that will be used to create a service.

{
    "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-11122233344455566"
        ],
        "securityGroups": [
          "sg-12345678912345678"
        ],
        "assignPublicIp": "ENABLED"
      }
    },
    "desiredCount": 1
}

 

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

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

 

And now you should be able to access the container via the load balancer.




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