--- 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 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 directly accessible. You can configure to your requirements. Here is an example of an ERB template `.ufo/templates/main.json.erb` that shows how to modify the task definition you want ufo to upload: **.ufo/templates/main.json.erb**: ```json { "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 "hi-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 "hi-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 essentially allow you to use a set of shared variables throughout your templates: **.ufo/variables/base.rb**: ```ruby @image = helper.full_image_name # includes the git sha tongueroo/hi:ufo-[sha]. @cpu = 128 @memory_reservation = 256 @environment = helper.env_file(".env") ``` **.ufo/variables/development.rb**: ```ruby @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: ```sh ufo tasks build ``` You should see output similar to below: ```sh $ ufo tasks build Building Task Definitions... Generating Task Definitions: .ufo/output/hi-web.json .ufo/output/hi-worker.json .ufo/output/hi-clock.json Task Definitions built in .ufo/output $ ``` Let's take a look at one of the generated files: `.ufo/output/hi-web.json`. ```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: ```sh ufo tasks register ``` You should see something similar to this: ```sh 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.