|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# AWS |
| 3 | +# ------------------------------------------------------------------------------ |
| 4 | +data "aws_region" "current" {} |
| 5 | + |
| 6 | +# ------------------------------------------------------------------------------ |
| 7 | +# Cloudwatch |
| 8 | +# ------------------------------------------------------------------------------ |
| 9 | +resource "aws_cloudwatch_log_group" "main" { |
| 10 | + name = "${var.name_prefix}" |
| 11 | + retention_in_days = "${var.log_retention_in_days}" |
| 12 | + tags = "${var.tags}" |
| 13 | +} |
| 14 | + |
| 15 | +# ------------------------------------------------------------------------------ |
| 16 | +# IAM - Task execution role, needed to pull ECR images etc. |
| 17 | +# ------------------------------------------------------------------------------ |
| 18 | +resource "aws_iam_role" "execution" { |
| 19 | + name = "${var.name_prefix}-task-execution-role" |
| 20 | + assume_role_policy = "${data.aws_iam_policy_document.task_assume.json}" |
| 21 | +} |
| 22 | + |
| 23 | +resource "aws_iam_role_policy" "task_execution" { |
| 24 | + name = "${var.name_prefix}-task-execution" |
| 25 | + role = "${aws_iam_role.execution.id}" |
| 26 | + policy = "${data.aws_iam_policy_document.task_execution_permissions.json}" |
| 27 | +} |
| 28 | + |
| 29 | +# ------------------------------------------------------------------------------ |
| 30 | +# IAM - Task role, basic. Users of the module will append policies to this role |
| 31 | +# when they use the module. S3, Dynamo permissions etc etc. |
| 32 | +# ------------------------------------------------------------------------------ |
| 33 | +resource "aws_iam_role" "task" { |
| 34 | + name = "${var.name_prefix}-task-role" |
| 35 | + assume_role_policy = "${data.aws_iam_policy_document.task_assume.json}" |
| 36 | +} |
| 37 | + |
| 38 | +resource "aws_iam_role_policy" "log_agent" { |
| 39 | + name = "${var.name_prefix}-log-permissions" |
| 40 | + role = "${aws_iam_role.task.id}" |
| 41 | + policy = "${data.aws_iam_policy_document.task_permissions.json}" |
| 42 | +} |
| 43 | + |
| 44 | +# ------------------------------------------------------------------------------ |
| 45 | +# Security groups |
| 46 | +# ------------------------------------------------------------------------------ |
| 47 | +resource "aws_security_group" "ecs_service" { |
| 48 | + vpc_id = "${var.vpc_id}" |
| 49 | + name = "${var.name_prefix}-ecs-service-sg" |
| 50 | + description = "Fargate service security group" |
| 51 | + tags = "${merge(var.tags, map("Name", "${var.name_prefix}-sg"))}" |
| 52 | +} |
| 53 | + |
| 54 | +resource "aws_security_group_rule" "egress_service" { |
| 55 | + security_group_id = "${aws_security_group.ecs_service.id}" |
| 56 | + type = "egress" |
| 57 | + protocol = "-1" |
| 58 | + from_port = 0 |
| 59 | + to_port = 0 |
| 60 | + cidr_blocks = ["0.0.0.0/0"] |
| 61 | + ipv6_cidr_blocks = ["::/0"] |
| 62 | +} |
| 63 | + |
| 64 | +# ------------------------------------------------------------------------------ |
| 65 | +# LB Target group |
| 66 | +# ------------------------------------------------------------------------------ |
| 67 | +resource "aws_lb_target_group" "task" { |
| 68 | + vpc_id = "${var.vpc_id}" |
| 69 | + protocol = "${var.task_container_protocol}" |
| 70 | + port = "${var.task_container_port}" |
| 71 | + target_type = "ip" |
| 72 | + health_check = ["${var.health_check}"] |
| 73 | + |
| 74 | + # NOTE: TF is unable to destroy a target group while a listener is attached, |
| 75 | + # therefor we have to create a new one before destroying the old. This also means |
| 76 | + # we have to let it have a random name, and then tag it with the desired name. |
| 77 | + lifecycle { |
| 78 | + create_before_destroy = true |
| 79 | + } |
| 80 | + |
| 81 | + tags = "${merge(var.tags, map("Name", "${var.name_prefix}-target-${var.task_container_port}"))}" |
| 82 | +} |
| 83 | + |
| 84 | +# ------------------------------------------------------------------------------ |
| 85 | +# ECS Task/Service |
| 86 | +# ------------------------------------------------------------------------------ |
| 87 | +data "null_data_source" "task_environment" { |
| 88 | + count = "${var.task_container_environment_count}" |
| 89 | + |
| 90 | + inputs = { |
| 91 | + name = "${element(keys(var.task_container_environment), count.index)}" |
| 92 | + value = "${element(values(var.task_container_environment), count.index)}" |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +resource "aws_ecs_task_definition" "task" { |
| 97 | + family = "${var.name_prefix}" |
| 98 | + execution_role_arn = "${aws_iam_role.execution.arn}" |
| 99 | + network_mode = "awsvpc" |
| 100 | + requires_compatibilities = ["FARGATE"] |
| 101 | + cpu = "${var.task_definition_cpu}" |
| 102 | + memory = "${var.task_definition_memory}" |
| 103 | + task_role_arn = "${aws_iam_role.task.arn}" |
| 104 | + |
| 105 | + container_definitions = <<EOF |
| 106 | +[{ |
| 107 | + "name": "${var.name_prefix}", |
| 108 | + "image": "${var.task_container_image}", |
| 109 | + "essential": true, |
| 110 | + "portMappings": [ |
| 111 | + { |
| 112 | + "containerPort": ${var.task_container_port}, |
| 113 | + "hostPort": ${var.task_container_port}, |
| 114 | + "protocol":"tcp" |
| 115 | + } |
| 116 | + ], |
| 117 | + "logConfiguration": { |
| 118 | + "logDriver": "awslogs", |
| 119 | + "options": { |
| 120 | + "awslogs-group": "${aws_cloudwatch_log_group.main.name}", |
| 121 | + "awslogs-region": "${data.aws_region.current.name}", |
| 122 | + "awslogs-stream-prefix": "container" |
| 123 | + } |
| 124 | + }, |
| 125 | + "command": ${jsonencode(var.task_container_command)}, |
| 126 | + "environment": ${jsonencode(data.null_data_source.task_environment.*.outputs)} |
| 127 | +}] |
| 128 | +EOF |
| 129 | +} |
| 130 | + |
| 131 | +resource "aws_ecs_service" "service" { |
| 132 | + depends_on = ["null_resource.lb_exists"] |
| 133 | + name = "${var.name_prefix}" |
| 134 | + cluster = "${var.cluster_id}" |
| 135 | + task_definition = "${aws_ecs_task_definition.task.arn}" |
| 136 | + desired_count = "${var.desired_count}" |
| 137 | + launch_type = "FARGATE" |
| 138 | + deployment_minimum_healthy_percent = 50 |
| 139 | + deployment_maximum_percent = 200 |
| 140 | + |
| 141 | + network_configuration { |
| 142 | + subnets = ["${var.private_subnet_ids}"] |
| 143 | + security_groups = ["${aws_security_group.ecs_service.id}"] |
| 144 | + assign_public_ip = "${var.task_container_assign_public_ip}" |
| 145 | + } |
| 146 | + |
| 147 | + load_balancer { |
| 148 | + container_name = "${var.name_prefix}" |
| 149 | + container_port = "${var.task_container_port}" |
| 150 | + target_group_arn = "${aws_lb_target_group.task.arn}" |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +# HACK: The workaround used in ecs/service does not work for some reason in this module, this fixes the following error: |
| 155 | +# "The target group with targetGroupArn arn:aws:elasticloadbalancing:... does not have an associated load balancer." |
| 156 | +# see https://github.com/hashicorp/terraform/issues/12634. |
| 157 | +# Service depends on this resources which prevents it from being created until the LB is ready |
| 158 | +resource "null_resource" "lb_exists" { |
| 159 | + triggers { |
| 160 | + alb_name = "${var.lb_arn}" |
| 161 | + } |
| 162 | +} |
0 commit comments