|
| 1 | +# Role for ECS task |
| 2 | +# This is because our Fargate ECS must be able to pull images from ECS |
| 3 | +# and put logs from application container to log driver |
| 4 | + |
| 5 | +data "aws_iam_policy_document" "ecs_task_exec_role" { |
| 6 | + statement { |
| 7 | + actions = ["sts:AssumeRole"] |
| 8 | + |
| 9 | + principals { |
| 10 | + type = "Service" |
| 11 | + identifiers = ["ecs-tasks.amazonaws.com"] |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +resource "aws_iam_role" "ecsTaskExecutionRole" { |
| 17 | + name = "${var.environment_name}-${var.name}-taskrole-ecs" |
| 18 | + assume_role_policy = data.aws_iam_policy_document.ecs_task_exec_role.json |
| 19 | +} |
| 20 | + |
| 21 | +resource "aws_iam_role_policy_attachment" "ecs_task_exec_role" { |
| 22 | + role = aws_iam_role.ecsTaskExecutionRole.name |
| 23 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" |
| 24 | +} |
| 25 | + |
| 26 | +# Cloudwatch logs |
| 27 | + |
| 28 | +resource "aws_cloudwatch_log_group" "short_stuff" { |
| 29 | + name = "/fargate/${var.environment_name}-${var.name}" |
| 30 | +} |
| 31 | + |
| 32 | +# Cluster |
| 33 | + |
| 34 | +resource "aws_ecs_cluster" "default" { |
| 35 | + depends_on = [aws_cloudwatch_log_group.short_stuff] |
| 36 | + name = "${var.environment_name}-${var.name}" |
| 37 | +} |
| 38 | + |
| 39 | +# Task definition for the application |
| 40 | + |
| 41 | +resource "aws_ecs_task_definition" "short_stuff" { |
| 42 | + family = "${var.environment_name}-${var.name}-td" |
| 43 | + requires_compatibilities = ["FARGATE"] |
| 44 | + cpu = var.ecs_fargate_application_cpu |
| 45 | + memory = var.ecs_fargate_application_mem |
| 46 | + network_mode = "awsvpc" |
| 47 | + execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn |
| 48 | + container_definitions = <<DEFINITION |
| 49 | +[ |
| 50 | + { |
| 51 | + "environment": [ |
| 52 | + {"name": "SECRET_KEY_BASE", "value": "generate one with mix phx.gen.secret"} |
| 53 | + ], |
| 54 | + "image": "${aws_ecr_repository.short_stuff.repository_url}:latest", |
| 55 | + "name": "${var.environment_name}-${var.name}", |
| 56 | + "portMappings": [ |
| 57 | + { |
| 58 | + "containerPort": 4000 |
| 59 | + } |
| 60 | + ], |
| 61 | + "logConfiguration": { |
| 62 | + "logDriver": "awslogs", |
| 63 | + "options": { |
| 64 | + "awslogs-group": "${aws_cloudwatch_log_group.short_stuff.name}", |
| 65 | + "awslogs-region": "${var.aws_region}", |
| 66 | + "awslogs-stream-prefix": "ecs-fargate" |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +] |
| 71 | +DEFINITION |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +resource "aws_ecs_service" "short_stuff" { |
| 76 | + name = "${var.environment_name}-${var.name}-service" |
| 77 | + cluster = aws_ecs_cluster.default.id |
| 78 | + launch_type = "FARGATE" |
| 79 | + task_definition = aws_ecs_task_definition.short_stuff.arn |
| 80 | + desired_count = var.ecs_application_count |
| 81 | + |
| 82 | + load_balancer { |
| 83 | + target_group_arn = aws_lb_target_group.short_stuff.arn |
| 84 | + container_name = "${var.environment_name}-${var.name}" |
| 85 | + container_port = 4000 |
| 86 | + } |
| 87 | + |
| 88 | + network_configuration { |
| 89 | + assign_public_ip = false |
| 90 | + |
| 91 | + security_groups = [ |
| 92 | + aws_security_group.egress-all.id, |
| 93 | + aws_security_group.short_stuff-service.id |
| 94 | + ] |
| 95 | + subnets = [aws_subnet.private.id] |
| 96 | + } |
| 97 | + |
| 98 | + depends_on = [ |
| 99 | + aws_lb_listener.short_stuff_http, |
| 100 | + aws_lb_listener.short_stuff_https, |
| 101 | + aws_ecs_task_definition.short_stuff |
| 102 | + ] |
| 103 | +} |
0 commit comments