|
| 1 | +##### |
| 2 | +# Execution IAM Role |
| 3 | +##### |
| 4 | +resource "aws_iam_role" "execution" { |
| 5 | + name = "${var.name_prefix}-execution-role" |
| 6 | + assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json |
| 7 | + |
| 8 | + tags = var.tags |
| 9 | +} |
| 10 | + |
| 11 | +resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy_attach" { |
| 12 | + role = aws_iam_role.execution.name |
| 13 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" |
| 14 | +} |
| 15 | + |
| 16 | +resource "aws_iam_role_policy" "read_repository_credentials" { |
| 17 | + count = length(var.repository_credentials) != 0 ? 1 : 0 |
| 18 | + |
| 19 | + name = "${var.name_prefix}-read-repository-credentials" |
| 20 | + role = aws_iam_role.execution.id |
| 21 | + policy = data.aws_iam_policy_document.read_repository_credentials.json |
| 22 | +} |
| 23 | + |
| 24 | +##### |
| 25 | +# IAM - Task role, basic. Append policies to this role for S3, DynamoDB etc. |
| 26 | +##### |
| 27 | +resource "aws_iam_role" "task" { |
| 28 | + name = "${var.name_prefix}-task-role" |
| 29 | + assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json |
| 30 | + |
| 31 | + tags = var.tags |
| 32 | +} |
| 33 | + |
| 34 | +resource "aws_iam_role_policy" "log_agent" { |
| 35 | + name = "${var.name_prefix}-log-permissions" |
| 36 | + role = aws_iam_role.task.id |
| 37 | + policy = data.aws_iam_policy_document.task_permissions.json |
| 38 | +} |
| 39 | + |
| 40 | +##### |
| 41 | +# ECS task definition |
| 42 | +##### |
| 43 | + |
| 44 | +locals { |
| 45 | + task_environment = [ |
| 46 | + for k, v in var.task_container_environment : { |
| 47 | + name = k |
| 48 | + value = v |
| 49 | + } |
| 50 | + ] |
| 51 | +} |
| 52 | + |
| 53 | +resource "aws_ecs_task_definition" "task" { |
| 54 | + family = var.name_prefix |
| 55 | + execution_role_arn = aws_iam_role.execution.arn |
| 56 | + network_mode = "awsvpc" |
| 57 | + requires_compatibilities = ["FARGATE"] |
| 58 | + cpu = var.task_definition_cpu |
| 59 | + memory = var.task_definition_memory |
| 60 | + task_role_arn = aws_iam_role.task.arn |
| 61 | + |
| 62 | + container_definitions = <<EOF |
| 63 | +[{ |
| 64 | + "name": "${var.container_name != "" ? var.container_name : var.name_prefix}", |
| 65 | + "image": "${var.task_container_image}", |
| 66 | + %{if var.repository_credentials != ""~} |
| 67 | + "repositoryCredentials": { |
| 68 | + "credentialsParameter": "${var.repository_credentials}" |
| 69 | + }, |
| 70 | + %{~endif} |
| 71 | + "essential": true, |
| 72 | + "portMappings": [ |
| 73 | + { |
| 74 | + "containerPort": ${var.task_container_port}, |
| 75 | + %{if var.task_host_port != 0~} |
| 76 | + "hostPort": ${var.task_host_port}, |
| 77 | + %{~endif} |
| 78 | + "protocol":"tcp" |
| 79 | + } |
| 80 | + ], |
| 81 | + %{if var.cloudwatch_log_group_name != ""~} |
| 82 | + "logConfiguration": { |
| 83 | + "logDriver": "awslogs", |
| 84 | + "options": { |
| 85 | + "awslogs-group": "${var.cloudwatch_log_group_name}", |
| 86 | + "awslogs-region": "${data.aws_region.current.name}", |
| 87 | + "awslogs-stream-prefix": "container" |
| 88 | + } |
| 89 | + }, |
| 90 | + %{~endif} |
| 91 | + "command": ${jsonencode(var.task_container_command)}, |
| 92 | + "environment": ${jsonencode(local.task_environment)} |
| 93 | +}] |
| 94 | +EOF |
| 95 | + |
| 96 | + dynamic "placement_constraints" { |
| 97 | + for_each = var.placement_constraints |
| 98 | + content { |
| 99 | + expression = lookup(placement_constraints.value, "expression", null) |
| 100 | + type = placement_constraints.value.type |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + dynamic "proxy_configuration" { |
| 105 | + for_each = var.proxy_configuration |
| 106 | + content { |
| 107 | + container_name = proxy_configuration.value.container_name |
| 108 | + properties = lookup(proxy_configuration.value, "properties", null) |
| 109 | + type = lookup(proxy_configuration.value, "type", null) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + dynamic "volume" { |
| 114 | + for_each = var.volume |
| 115 | + content { |
| 116 | + name = volume.value.name |
| 117 | + host_path = lookup(volume.value, "host_path", null) |
| 118 | + docker_volume_configuration = lookup(volume.value, "docker_volume_configuration", null) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + tags = merge( |
| 124 | + var.tags, |
| 125 | + { |
| 126 | + Name = var.container_name != "" ? var.container_name : var.name_prefix |
| 127 | + }, |
| 128 | + ) |
| 129 | +} |
| 130 | + |
0 commit comments