Skip to main content

Running ECS Tasks

Scheduling Containerized jobs via AWS ECS(Elastic Container Service) Tasks

Requirements#

  • Docker
  • Aws CLI

NOTES:#

For purposes of this document the us-east-2 region will be used. This region is chosen because it currently supports Scheduled Tasking via Fargate list of supported regions

Introduction#

1. Building of a Container#

Create a dockerfile in delployment root order to build up containerized environment. deploy code to deployment root Using the Docker CLI build a docker image

docker build . -t <tag-name>

This is a "container". It contains the needed elements with which to execute in a self contained manner as defined in the Dockerfile. An "image" of the running container is produced. you can view created images by calling:

docker images

This "image" can be uploaded to Container Registries. You can run a container creating an image via

docker run <image name>

This will locally mount and run the container, from there you can issues commands via

docker exec <command>

2 Placing a container on Amazon ECR(Elastic Container Registry)#

Create a repository on ECR

this can be done via the ECR Dashboard Create a repository by hitting the create repository key and naming the repository. This will produce a repository with a copyable URI

in order to push to a ECR an acess token must be generated via the AWS CLI this can be done via aws ecr get-login --no-include-email --region <region name

ie --region us-east-2

This will produce an docker login command that will generate a 12hour access token to the ECR on that region

WARNING: Running this link will produce the access credentials in plain text and is not secure...

in order to place a container image in the ECR it is necessary to first tag the image to the repository this is done via the following call:

docker tag <image reference> <repo-uri>:<tag-name>

the <image reference> can refer to either the IMAGE ID or the REPOSITORY:TAG

Finally a image can be pushed to the ECR via the push command

docker push repo:tag

#2.1 Updating a container registry#

A container can be updated to run new things with the same tag to do this tag the source image with the Image uri

push the repo

NOTE: this will copy your old image to an old untagged image and create a new image with the used tag you can verify that a new image pushed by looking at the pushed at attribute in the ECR web interface

Building a Fargate Cluster#

NOTE: We are building a Fargate Cluster as it allows for a billing model based resource consumption. This is ideal for isolated task runs that only require short periods of compute time

Using the ECS Clusters Dashboard hit the Create Cluster Button and select "Networking only" which will allow for a Fargate cluster with minimum additional configuration.

Next name your cluster and Select the "Create VPC" Option keeping the default values.

QUESTION: CAN you run tasks via FARGATE without a new VPC?

Building a Task Definition#

From the cluster interface select your created cluster then select the Task Definition option.

Create new Fargate Task Definition

Set Task Role to ecsTaskExecutionRole

Set Task Size Link Image from Image URI in the ECR

There are a bunch of options here but you can Click Confirm and it will be workable.

Running A Task#

You can run a Task over Fargate from the Tasks Tab Click Run New Task Select Task Definition you want to run Select VPC and Subnets Change Security Group to Default Issuing a command to built container In "Advanced Options" -> Container Overrides -> Service Name Command Override, type the desired command