|
| 1 | +##### |
| 2 | +# Cloudwatch |
| 3 | +##### |
| 4 | +resource "aws_cloudwatch_log_group" "main" { |
| 5 | + name = var.name_prefix |
| 6 | + retention_in_days = var.log_retention_in_days |
| 7 | + tags = var.tags |
| 8 | +} |
| 9 | + |
| 10 | +##### |
| 11 | +# IAM - Task execution role, needed to pull ECR images etc. |
| 12 | +##### |
| 13 | +resource "aws_iam_role" "execution" { |
| 14 | + name = "${var.name_prefix}-task-execution-role" |
| 15 | + assume_role_policy = data.aws_iam_policy_document.task_assume.json |
| 16 | +} |
| 17 | + |
| 18 | +resource "aws_iam_role_policy" "task_execution" { |
| 19 | + name = "${var.name_prefix}-task-execution" |
| 20 | + role = aws_iam_role.execution.id |
| 21 | + policy = data.aws_iam_policy_document.task_execution_permissions.json |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_iam_role_policy" "read_repository_credentials" { |
| 25 | + count = length(var.repository_credentials) != 0 ? 1 : 0 |
| 26 | + |
| 27 | + name = "${var.name_prefix}-read-repository-credentials" |
| 28 | + role = aws_iam_role.execution.id |
| 29 | + policy = data.aws_iam_policy_document.read_repository_credentials.json |
| 30 | +} |
| 31 | + |
| 32 | +##### |
| 33 | +# IAM - Task role, basic. Append policies to this role for S3, DynamoDB etc. |
| 34 | +##### |
| 35 | +resource "aws_iam_role" "task" { |
| 36 | + name = "${var.name_prefix}-task-role" |
| 37 | + assume_role_policy = data.aws_iam_policy_document.task_assume.json |
| 38 | +} |
| 39 | + |
| 40 | +resource "aws_iam_role_policy" "log_agent" { |
| 41 | + name = "${var.name_prefix}-log-permissions" |
| 42 | + role = aws_iam_role.task.id |
| 43 | + policy = data.aws_iam_policy_document.task_permissions.json |
| 44 | +} |
| 45 | + |
| 46 | +##### |
| 47 | +# Security groups |
| 48 | +##### |
| 49 | +resource "aws_security_group" "ecs_service" { |
| 50 | + vpc_id = var.vpc_id |
| 51 | + name = "${var.name_prefix}-ecs-service-sg" |
| 52 | + description = "Fargate service security group" |
| 53 | + tags = merge( |
| 54 | + var.tags, |
| 55 | + { |
| 56 | + Name = "${var.name_prefix}-sg" |
| 57 | + }, |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +resource "aws_security_group_rule" "egress_service" { |
| 62 | + security_group_id = aws_security_group.ecs_service.id |
| 63 | + type = "egress" |
| 64 | + protocol = "-1" |
| 65 | + from_port = 0 |
| 66 | + to_port = 0 |
| 67 | + cidr_blocks = ["0.0.0.0/0"] |
| 68 | + ipv6_cidr_blocks = ["::/0"] |
| 69 | +} |
| 70 | + |
| 71 | +##### |
| 72 | +# Load Balancer Target group |
| 73 | +##### |
| 74 | +resource "aws_lb_target_group" "task" { |
| 75 | + vpc_id = var.vpc_id |
| 76 | + protocol = var.task_container_protocol |
| 77 | + port = var.task_container_port |
| 78 | + target_type = "ip" |
| 79 | + dynamic "health_check" { |
| 80 | + for_each = [var.health_check] |
| 81 | + content { |
| 82 | + enabled = lookup(health_check.value, "enabled", null) |
| 83 | + healthy_threshold = lookup(health_check.value, "healthy_threshold", null) |
| 84 | + interval = lookup(health_check.value, "interval", null) |
| 85 | + matcher = lookup(health_check.value, "matcher", null) |
| 86 | + path = lookup(health_check.value, "path", null) |
| 87 | + port = lookup(health_check.value, "port", null) |
| 88 | + protocol = lookup(health_check.value, "protocol", null) |
| 89 | + timeout = lookup(health_check.value, "timeout", null) |
| 90 | + unhealthy_threshold = lookup(health_check.value, "unhealthy_threshold", null) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + lifecycle { |
| 95 | + create_before_destroy = true |
| 96 | + } |
| 97 | + |
| 98 | + tags = merge( |
| 99 | + var.tags, |
| 100 | + { |
| 101 | + Name = "${var.name_prefix}-target-${var.task_container_port}" |
| 102 | + }, |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +##### |
| 107 | +# ECS Task/Service |
| 108 | +##### |
| 109 | +locals { |
| 110 | + task_environment = [ |
| 111 | + for k, v in var.task_container_environment : { |
| 112 | + name = k |
| 113 | + value = v |
| 114 | + } |
| 115 | + ] |
| 116 | +} |
| 117 | + |
| 118 | +resource "aws_ecs_task_definition" "task" { |
| 119 | + family = var.name_prefix |
| 120 | + execution_role_arn = aws_iam_role.execution.arn |
| 121 | + network_mode = "awsvpc" |
| 122 | + requires_compatibilities = ["FARGATE"] |
| 123 | + cpu = var.task_definition_cpu |
| 124 | + memory = var.task_definition_memory |
| 125 | + task_role_arn = aws_iam_role.task.arn |
| 126 | + |
| 127 | + container_definitions = <<EOF |
| 128 | +[{ |
| 129 | + "name": "${var.container_name != "" ? var.container_name : var.name_prefix}", |
| 130 | + "image": "${var.task_container_image}", |
| 131 | + %{if var.repository_credentials != ""~} |
| 132 | + "repositoryCredentials": { |
| 133 | + "credentialsParameter": "${var.repository_credentials}" |
| 134 | + }, |
| 135 | + %{~endif} |
| 136 | + "essential": true, |
| 137 | + "portMappings": [ |
| 138 | + { |
| 139 | + "containerPort": ${var.task_container_port}, |
| 140 | + "hostPort": ${var.task_container_port}, |
| 141 | + "protocol":"tcp" |
| 142 | + } |
| 143 | + ], |
| 144 | + "logConfiguration": { |
| 145 | + "logDriver": "awslogs", |
| 146 | + "options": { |
| 147 | + "awslogs-group": "${aws_cloudwatch_log_group.main.name}", |
| 148 | + "awslogs-region": "${data.aws_region.current.name}", |
| 149 | + "awslogs-stream-prefix": "container" |
| 150 | + } |
| 151 | + }, |
| 152 | + "command": ${jsonencode(var.task_container_command)}, |
| 153 | + "environment": ${jsonencode(local.task_environment)} |
| 154 | +}] |
| 155 | +EOF |
| 156 | +} |
| 157 | + |
| 158 | +resource "aws_ecs_service" "service" { |
| 159 | + depends_on = [null_resource.lb_exists] |
| 160 | + name = var.name_prefix |
| 161 | + cluster = var.cluster_id |
| 162 | + task_definition = aws_ecs_task_definition.task.arn |
| 163 | + desired_count = var.desired_count |
| 164 | + launch_type = "FARGATE" |
| 165 | + deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent |
| 166 | + deployment_maximum_percent = var.deployment_maximum_percent |
| 167 | + health_check_grace_period_seconds = var.health_check_grace_period_seconds |
| 168 | + |
| 169 | + network_configuration { |
| 170 | + subnets = var.private_subnet_ids |
| 171 | + security_groups = [aws_security_group.ecs_service.id] |
| 172 | + assign_public_ip = var.task_container_assign_public_ip |
| 173 | + } |
| 174 | + |
| 175 | + load_balancer { |
| 176 | + container_name = var.container_name != "" ? var.container_name : var.name_prefix |
| 177 | + container_port = var.task_container_port |
| 178 | + target_group_arn = aws_lb_target_group.task.arn |
| 179 | + } |
| 180 | + |
| 181 | + deployment_controller { |
| 182 | + type = var.deployment_controller_type # CODE_DEPLOY or ECS |
| 183 | + } |
| 184 | + |
| 185 | + dynamic "service_registries" { |
| 186 | + for_each = var.service_registry_arn == "" ? [] : [1] |
| 187 | + content { |
| 188 | + registry_arn = var.service_registry_arn |
| 189 | + container_port = var.task_container_port |
| 190 | + container_name = var.container_name != "" ? var.container_name : var.name_prefix |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +# HACK: The workaround used in ecs/service does not work for some reason in this module, this fixes the following error: |
| 196 | +# "The target group with targetGroupArn arn:aws:elasticloadbalancing:... does not have an associated load balancer." |
| 197 | +# see https://github.com/hashicorp/terraform/issues/12634. |
| 198 | +# Service depends on this resources which prevents it from being created until the LB is ready |
| 199 | +resource "null_resource" "lb_exists" { |
| 200 | + triggers = { |
| 201 | + alb_name = var.lb_arn |
| 202 | + } |
| 203 | +} |
0 commit comments