Task Definitions

Build the ECS Task Definitions

Now that we have a docker image pushed to a registry we can use that image for ECS. Ufo takes that image and adds it to an ECS task definition. This is where ufo is super powerful. Ufo gives you the power to build and control your ECS task definition easily.

Let’s take a look at the 2 files that are used by ufo to build the ECS task definition. These files were generated by the ufo init command at the beginning.

  1. ufo/templates/main.json.erb
  2. ufo/task_definitions.rb

Ufo task definitions are written as an ERB template that makes it every easily accessible and configurable to your requirements. Here is is an example of an ERB template .ufo/templates/main.json.erb that shows how easy it is to modfied the task definition you want to be uploaded by ufo:


    "family": "<%= @family %>",
    "containerDefinitions": [
            "name": "<%= @name %>",
            "image": "<%= @image %>",
            "cpu": <%= @cpu %>,
            <% if @memory %>
            "memory": <%= @memory %>,
            <% end %>
            <% if @memory_reservation %>
            "essential": true

The instance variable values are specified in .ufo/task_definitions.rb via a DSL. Here’s the file:


task_definition "hi-web" do
  source "main" # will use ufo/templates/main.json.erb
    family: task_definition_name,
    name: "web",
    container_port: helper.dockerfile_port,
    command: ["bin/web"]

task_definition "hi-worker" do
  source "main" # will use ufo/templates/main.json.erb
    family: task_definition_name,
    name: "worker",
    command: ["bin/worker"]

Shared Variables

Ufo has a concept of shared variables, covered in Shared Variables. The shared variables are set in the variables folder and essentially allow you to use a set of shared variables through your templates:


@image = helper.full_image_name # includes the git sha tongueroo/hi:ufo-[sha].
@cpu = 128
@memory_reservation = 256
@environment = helper.env_file(".env")


@environment = helper.env_vars(%Q{

Ufo combines the main.json.erb template, task_definitions.rb definitions, and variables in the .ufo/variables folder. It then generates the raw AWS formatted task definition in the output folder.

If you need to modify the task definition template to suite your own needs it is super simple, just edit main.json.erb. You do not have to dive deep into internal code somewhere. It is all there for you to fully control.

The task_definition.rb has access to some useful helper methods detailed in Helpers.

Let’s build the task definitions:

ufo tasks build

You should see output similar to below:

$ ufo tasks build
Building Task Definitions...
Generating Task Definitions:
Task Definitions built in .ufo/output

Let’s take a look at one of the generated files: .ufo/output/hi-web.json.

  "family": "hi-web",
  "containerDefinitions": [
      "name": "web",
      "image": "tongueroo/hi:ufo-2018-02-13T11-33-15-27aa242",
      "cpu": 192,
      "memoryReservation": 256,
      "portMappings": [
          "containerPort": "3000",
          "protocol": "tcp"
      "command": null,
      "environment": [
          "name": "RAILS_ENV",
          "value": "development"
          "name": "SECRET_KEY_BASE",
          "value": "secret"
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "ecs/hi-web",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "hi"
      "essential": true

Register the ECS Task Definitions

You have built the ecs task definitions locally on your machine. To register the task definitions in the output folder run:

ufo tasks register

You should see something similiar to this:

hi-clock task definition registered.
hi-web task definition registered.
hi-worker task definition registered.

Pro tip: Use the <- and -> arrow keys to move back and forward.

Edit this page

See a typo or an error? You can improve this page. This website is available on GitHub and contributions are encouraged and welcomed. We love pull requests from you!