Permalink
Fetching contributors…
Cannot retrieve contributors at this time
179 lines (146 sloc) 5.03 KB
title
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 powerful. Ufo gives you the power to build and control your ECS task definition directly.

Let's take a look at the 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 directly accessible. You can configure it to your requirements. Here is an example of an ERB template .ufo/templates/main.json.erb and shows how to modify the task definition:

.ufo/templates/main.json.erb:

{
    "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:

.ufo/task_definitions.rb:

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

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

Shared Variables

Ufo has a concept of shared variables, covered in [Shared Variables]({% link _docs/variables.md %}). The shared variables are set in the variables folder and allow you to use a set of shared variables common to your templates:

.ufo/variables/base.rb:

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

.ufo/variables/development.rb:

@environment = helper.env_vars(%Q{
  RAILS_ENV=development
  SECRET_KEY_BASE=secret
})

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 suit your own needs it is simple, just edit main.json.erb. You do not have to dive deep into internal code somewhere. It is all there for you to control fully.

The task_definition.rb has access to some useful helper methods detailed in [Helpers]({% link _docs/helpers.md %}).

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:
  .ufo/output/demo-web.json
  .ufo/output/demo-worker.json
  .ufo/output/demo-clock.json
Task Definitions built in .ufo/output
$

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

{
  "family": "demo-web",
  "containerDefinitions": [
    {
      "name": "web",
      "image": "tongueroo/demo-ufo:ufo-2018-06-29T23-20-47-20b3a10",
      "cpu": 256,
      "memory": 512,
      "memoryReservation": 512,
      "portMappings": [
        {
          "containerPort": 4567,
          "protocol": "tcp"
        }
      ],
      "command": null,
      "environment": [
        {
          "name": "RAILS_ENV",
          "value": "development"
        },
        {
          "name": "SECRET_KEY_BASE",
          "value": "secret"
        }
      ],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "ecs/demo-web",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "demo"
        }
      },
      "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 similar to this:

$ ufo tasks register
Equivalent aws cli command:
  aws ecs register-task-definition --cli-input-json file://.ufo/output/demo-web.json
demo-web task definition registered.
Equivalent aws cli command:
  aws ecs register-task-definition --cli-input-json file://.ufo/output/demo-clock.json
demo-clock task definition registered.
Equivalent aws cli command:
  aws ecs register-task-definition --cli-input-json file://.ufo/output/demo-worker.json
demo-worker task definition registered.
$

Back Next Step

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