|
| 1 | +locals { |
| 2 | + name = "${var.project}-${var.env}-${var.service}" |
| 3 | + |
| 4 | + container_name = var.container_name == null ? local.name : var.container_name |
| 5 | + |
| 6 | + task_definition = "${aws_ecs_task_definition.job.family}:${aws_ecs_task_definition.job.revision}" |
| 7 | + |
| 8 | + tags = { |
| 9 | + managedBy = "terraform" |
| 10 | + Name = local.name |
| 11 | + project = var.project |
| 12 | + env = var.env |
| 13 | + service = var.service |
| 14 | + owner = var.owner |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +# Only one of the following is active at a time, depending on var.manage_task_definition |
| 19 | +resource "aws_ecs_service" "job" { |
| 20 | + name = local.name |
| 21 | + cluster = var.cluster_id |
| 22 | + count = var.manage_task_definition ? 1 : 0 |
| 23 | + launch_type = "FARGATE" |
| 24 | + |
| 25 | + task_definition = local.task_definition |
| 26 | + desired_count = var.desired_count |
| 27 | + deployment_maximum_percent = var.deployment_maximum_percent |
| 28 | + deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent |
| 29 | + scheduling_strategy = "REPLICA" |
| 30 | + |
| 31 | + network_configuration { |
| 32 | + subnets = var.task_subnets |
| 33 | + security_groups = var.security_group_ids |
| 34 | + } |
| 35 | + |
| 36 | + tags = local.tags |
| 37 | +} |
| 38 | + |
| 39 | +resource "aws_ecs_service" "unmanaged-job" { |
| 40 | + name = local.name |
| 41 | + cluster = var.cluster_id |
| 42 | + count = var.manage_task_definition ? 0 : 1 |
| 43 | + launch_type = "FARGATE" |
| 44 | + |
| 45 | + task_definition = local.task_definition |
| 46 | + desired_count = var.desired_count |
| 47 | + deployment_maximum_percent = var.deployment_maximum_percent |
| 48 | + deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent |
| 49 | + scheduling_strategy = "REPLICA" |
| 50 | + |
| 51 | + network_configuration { |
| 52 | + subnets = var.task_subnets |
| 53 | + security_groups = var.security_group_ids |
| 54 | + } |
| 55 | + |
| 56 | + lifecycle { |
| 57 | + ignore_changes = [task_definition] |
| 58 | + } |
| 59 | + |
| 60 | + tags = local.tags |
| 61 | +} |
| 62 | + |
| 63 | +# Default container definition if var.manage_task_definition == false |
| 64 | +# Defaults to a minimal hello-world implementation; should be updated separately from |
| 65 | +# Terraform, e.g. using ecs deploy or czecs |
| 66 | +locals { |
| 67 | + template = <<TEMPLATE |
| 68 | +[ |
| 69 | + { |
| 70 | + "name": "${local.container_name}", |
| 71 | + "image": "library/busybox:1.29", |
| 72 | + "command": ["sh", "-c", "while true; do { echo -e 'HTTP/1.1 200 OK\r\n\nRunning stub server'; date; } | nc -l -p 8080; done"] |
| 73 | + } |
| 74 | +] |
| 75 | +TEMPLATE |
| 76 | +} |
| 77 | + |
| 78 | +resource "aws_ecs_task_definition" "job" { |
| 79 | + family = local.name |
| 80 | + container_definitions = var.manage_task_definition ? var.task_definition : local.template |
| 81 | + task_role_arn = var.task_role_arn |
| 82 | + requires_compatibilities = ["FARGATE"] |
| 83 | + cpu = var.cpu |
| 84 | + memory = var.memory |
| 85 | + network_mode = "awsvpc" |
| 86 | + execution_role_arn = aws_iam_role.task_execution_role.arn |
| 87 | +} |
0 commit comments