arun-gupta/ecs-workshop


AWS EC2 Container Service Hands-on Workshop

  • Users starred: 9
  • Users forked: 11
  • Users watching: 9
  • Updated at: 2020-04-06 23:45:51

Getting Started with Amazon EC2 Container Service (ECS)

ECS Basic Concepts

Container Instance

An AMI instance that is primed for running containers. By default, each Amazon instance uses Amazon ECS-Optimized Linux AMI. This is the recommended image to run ECS container service. The key components of this base image are:

  1. Amazon Linux AMI

  2. Amazon ECS Container Agent – manages containers lifecycle on behalf of ECS and allows them to connect to the cluster

  3. Docker Engine

Task

A task is defined as a JSON file and describes an application that contains one or more container definitions. This usually points to Docker images from a registry, port/volume mapping, etc.

Service

ECS maintains the “desired state” of your application. This is achieved by creating a service. A service specifies the number of instances of a task definition that needs to run at a given time. If the task in a service becomes unhealthy or stop running, then the service scheduler will bounce the task. It ensures that the desired and actual state are match. This is what provides resilience in ECS.New tasks within a Service are balanced across Availability Zones in your cluster. Service scheduler figures out which container instances can meet the needs of a service and schedules it on a valid container instance in an optimal Availability Zone (one with the fewest number of tasks running).

Manage ECS Cluster

Using AWS Console

This section will use AWS Console and show how to:

  • Create an ECS cluster

  • Create a task definition

  • Create a service using that task

  • Start tht service in the cluster

Let’s get started.

  1. Go to AWS Console for ECS

  2. Click on Get Started, select Deploy a sample application onto an Amazon ECS Cluster. This exercise does not require to deploy images to ECR and so unselect that.

  3. Create a task definition:

    ecs task wildfly

    Click on Next Step.

  4. Create a service definition:

    ecs service wildfly

    Click on Next Step.

  5. Configure cluster:

    ecs cluster wildfly

    Click on Review & Launch.

  6. Click on Launch instance & run service. It takes a few seconds for the cluster to be created and then it shows the following output:

    ecs cluster create complete
  7. Wait for a few minutes for the Docker image to be downloaded to EC2 instances. Two task instances in the service are then started.

    ecs service view
  8. Click on the Service Name to see more details about the tasks running in this service:

    ecs task view

Using CLI

  1. Install AWS ECS CLI

    curl -o ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest
    chmod +x ecs-cli
    1. Why there is no brew install ecs-cli?

Create a cluster

  1. Create a cluster:

    aws ecs create-cluster --cluster-name default
    ecs-cli configure --cluster default
    ecs-cli up --keypair arun-west1 --capability-iam --size 2 --port 8080 --instance-type m3.xlarge
    1. Why cluster create and configure is split between two CLIs? aws ecs create-cluster feels like a fake command.

    2. ecs-cli uses --help where as aws ecs CLI uses help

    3. Why ecs-cli configure and then ecs-cli up instead of adding --cluster-name in up?

Create Service and Task

  1. Register task definition:

    aws ecs register-task-definition --cli-input-json file://./wildfly-task.json
  2. Create service:

    aws ecs create-service --service-name webapp-service --task-definition webapp --desired-count 1
  3. Get running count of the tasks in the service:

    aws ecs describe-services --services webapp-service | jq .services[0].deployments[0].runningCount
  4. Scale service to 0:

    aws ecs update-service --service webapp-service --desired-count 0
  5. Delete service:

    aws ecs delete-service --service webapp-service

Shutdown Cluster

  1. Shutdown the cluster:

    ecs-cli down --force

Using Cloud Formation

Launch an automatically named ECS cluster in a region of your choice:

aws cloudformation deploy \
  --template-file cloudformation/ecs-cluster.yml \
  --region <region> \
  --stack-name <stack name> \
  --capabilities CAPABILITY_NAMED_IAM

CAPABILITY_NAMED_IAM allows to create IAM policies with names. This will create a CloudFormation stack and an ECS cluster with <stack name>.

A typical usage will be:

aws cloudformation deploy \
  --template-file cloudformation/ecs-cluster.yml \
  --stack-name default \
  --capabilities CAPABILITY_NAMED_IAM

Note that the stack defaults to launching two micro instances. If you want to launch more instances or instances of a different type you must provide parameter overrides:

aws cloudformation deploy \
  --template-file cloudformation/ecs-cluster.yml \
  --parameter-overrides DesiredCapacity=3, MaxSize=5, InstanceType=m4.xlarge
  --stack-name default \
  --capabilities CAPABILITY_NAMED_IAM

Create Service and Task

Shutdown Cluster

aws cloudformation delete-stack --stack-name default

Using Terraform

The usage of Terraform scripts can be compared to using the CloudFormation. Creating an ECS cluster with Terraform is fairly easy

resource "aws_ecs_cluster" "cluster" {
  name = "${var.cluster}"
}

But for an ECS cluster to work more things need to be created like described in the previous sections here above. For a full explanation on how it works and how to build it please see: https://github.com/arminc/terraform-ecs

Load Balancing

TBD

Application with Multiple Tasks

TBD

Service Discovery

TBD

EC2 Container Registry

  • Build a Docker Image

  • Push and Pull images to EC2 Container Registry using Maven

CI/CD

using CodePipeline + ECS

TBD

Using Jenkins + ECS Plugin

TBD

Distributed Tracing using X-Ray

TBD

Stateful Containers

TBD