|
| 1 | +# Create an ECS cluster and services for each Bloom binary. |
| 2 | +resource "aws_iam_service_linked_role" "ecs" { |
| 3 | + aws_service_name = "ecs.amazonaws.com" |
| 4 | +} |
| 5 | +resource "aws_ecs_cluster" "bloom" { |
| 6 | + region = var.aws_region |
| 7 | + name = "bloom" |
| 8 | + depends_on = [aws_iam_service_linked_role.ecs] |
| 9 | + setting { |
| 10 | + name = "containerInsights" |
| 11 | + value = "enabled" |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +# API service. |
| 16 | +resource "aws_iam_role" "bloom_api_ecs" { |
| 17 | + name = "bloom-api-ecs" |
| 18 | + description = "Role the ECS service uses when launching the Bloom api container." |
| 19 | + # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html#create_task_iam_policy_and_role |
| 20 | + assume_role_policy = jsonencode({ |
| 21 | + Version = "2012-10-17" |
| 22 | + Statement = [{ |
| 23 | + Action = "sts:AssumeRole" |
| 24 | + Effect = "Allow" |
| 25 | + Principal = { |
| 26 | + Service = "ecs-tasks.amazonaws.com" |
| 27 | + } |
| 28 | + Condition = { |
| 29 | + ArnLike = { |
| 30 | + "aws:SourceArn" = "arn:aws:ecs:${var.aws_region}:${var.aws_account_number}:*" |
| 31 | + } |
| 32 | + StringEquals = { |
| 33 | + "aws:SourceAccount" = var.aws_account_number |
| 34 | + } |
| 35 | + } |
| 36 | + }] |
| 37 | + }) |
| 38 | +} |
| 39 | +resource "aws_iam_role_policy" "bloom_api_ecs" { |
| 40 | + name = "bloom-api-ecs" |
| 41 | + role = aws_iam_role.bloom_api_ecs.id |
| 42 | + policy = jsonencode({ |
| 43 | + Version = "2012-10-17" |
| 44 | + Statement = [ |
| 45 | + { |
| 46 | + Action = "secretsmanager:GetSecretValue" |
| 47 | + Effect = "Allow" |
| 48 | + Resource = aws_db_instance.bloom.master_user_secret[0].secret_arn |
| 49 | + }, |
| 50 | + { |
| 51 | + Action = [ |
| 52 | + "logs:CreateLogStream", |
| 53 | + "logs:PutLogEvents", |
| 54 | + ] |
| 55 | + Effect = "Allow" |
| 56 | + Resource = "${aws_cloudwatch_log_group.bloom_api.arn}:log-stream:*" |
| 57 | + }, |
| 58 | + ] |
| 59 | + }) |
| 60 | +} |
| 61 | +resource "aws_iam_role" "bloom_api_container" { |
| 62 | + name = "bloom-api-container" |
| 63 | + description = "Role the Bloom API container runs as." |
| 64 | + # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html#create_task_iam_policy_and_role |
| 65 | + assume_role_policy = jsonencode({ |
| 66 | + Version = "2012-10-17" |
| 67 | + Statement = [{ |
| 68 | + Action = "sts:AssumeRole" |
| 69 | + Effect = "Allow" |
| 70 | + Principal = { |
| 71 | + Service = "ecs-tasks.amazonaws.com" |
| 72 | + } |
| 73 | + Condition = { |
| 74 | + ArnLike = { |
| 75 | + "aws:SourceArn" = "arn:aws:ecs:${var.aws_region}:${var.aws_account_number}:*" |
| 76 | + } |
| 77 | + StringEquals = { |
| 78 | + "aws:SourceAccount" = var.aws_account_number |
| 79 | + } |
| 80 | + } |
| 81 | + }] |
| 82 | + }) |
| 83 | +} |
| 84 | +resource "aws_iam_role_policy" "bloom_api_container" { |
| 85 | + name = "bloom-api-container" |
| 86 | + role = aws_iam_role.bloom_api_container.id |
| 87 | + policy = jsonencode({ |
| 88 | + Version = "2012-10-17" |
| 89 | + Statement = [{ |
| 90 | + Action = "*" |
| 91 | + Effect = "Deny" |
| 92 | + Resource = "*" |
| 93 | + }] |
| 94 | + }) |
| 95 | +} |
| 96 | +resource "aws_cloudwatch_log_group" "bloom_api" { |
| 97 | + region = var.aws_region |
| 98 | + name = "bloom-api" |
| 99 | + log_group_class = "STANDARD" |
| 100 | + retention_in_days = local.is_prod ? 30 : 7 |
| 101 | +} |
| 102 | +resource "aws_ecs_task_definition" "bloom_api" { |
| 103 | + region = var.aws_region |
| 104 | + family = "bloom-api" |
| 105 | + network_mode = "awsvpc" |
| 106 | + requires_compatibilities = ["FARGATE"] |
| 107 | + runtime_platform { |
| 108 | + operating_system_family = "LINUX" |
| 109 | + cpu_architecture = "X86_64" |
| 110 | + } |
| 111 | + |
| 112 | + execution_role_arn = aws_iam_role.bloom_api_ecs.arn |
| 113 | + task_role_arn = aws_iam_role.bloom_api_container.arn |
| 114 | + |
| 115 | + # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size |
| 116 | + cpu = 1024 # 1 vCPU |
| 117 | + memory = 2 * 1024 # 2 GiB in MiB |
| 118 | + |
| 119 | + container_definitions = jsonencode([ |
| 120 | + { |
| 121 | + Name = "bloom-api" |
| 122 | + image = var.bloom_api_image |
| 123 | + command = [ |
| 124 | + "/bin/bash", |
| 125 | + "-c", |
| 126 | + # TODO: https://www.prisma.io/docs/orm/more/help-and-troubleshooting/dataguide/connection-uris#percent-encoding-values |
| 127 | + "export DATABASE_URL=postgres://$DB_USER:$(echo $DB_PASSWORD | sed 's/:/%3A/')@$DB_HOST/bloomprisma && env && yarn db:migration:run && yarn start:prod", |
| 128 | + ] |
| 129 | + secrets = [ |
| 130 | + { |
| 131 | + name = "DB_PASSWORD" |
| 132 | + # The master_user_secret tofu attribute is a block, so it is exposed as a list even though |
| 133 | + # there is only one element. |
| 134 | + # |
| 135 | + # The RDS managed secret has a username key and a password key. Docs for how to read |
| 136 | + # a specific key out of a secret: |
| 137 | + # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/secrets-envvar-secrets-manager.html#secrets-envvar-secrets-manager-update-container-definition |
| 138 | + valueFrom = "${aws_db_instance.bloom.master_user_secret[0].secret_arn}:password::" |
| 139 | + } |
| 140 | + ] |
| 141 | + environment = [ |
| 142 | + { |
| 143 | + name = "PORT" |
| 144 | + value = "3100" |
| 145 | + }, |
| 146 | + { |
| 147 | + name = "NODE_ENV" |
| 148 | + value = "production" |
| 149 | + }, |
| 150 | + { |
| 151 | + name = "APP_SECRET" |
| 152 | + value = "totally a secret" |
| 153 | + }, |
| 154 | + { |
| 155 | + name = "DB_USER" |
| 156 | + value = aws_db_instance.bloom.username |
| 157 | + }, |
| 158 | + { |
| 159 | + name = "DB_HOST" |
| 160 | + value = aws_db_instance.bloom.endpoint |
| 161 | + }, |
| 162 | + ] |
| 163 | + portMappings = [ |
| 164 | + { |
| 165 | + containerPort = 3100 |
| 166 | + appProtocol = "http" |
| 167 | + } |
| 168 | + ] |
| 169 | + restartPolicy = { |
| 170 | + enabled = false |
| 171 | + } |
| 172 | + healthCheck = { |
| 173 | + command = [ |
| 174 | + "curl", |
| 175 | + "--fail", |
| 176 | + "http://127.0.0.1:3100/" |
| 177 | + ] |
| 178 | + interval = 5 # seconds |
| 179 | + timeout = 2 # seconds |
| 180 | + retries = 10 |
| 181 | + startPeriod = 5 # seconds |
| 182 | + } |
| 183 | + logConfiguration = { |
| 184 | + logDriver = "awslogs" |
| 185 | + options = { |
| 186 | + "awslogs-region" = var.aws_region |
| 187 | + "awslogs-group" = aws_cloudwatch_log_group.bloom_api.name |
| 188 | + "awslogs-stream-prefix" = "bloom-api" |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + ]) |
| 193 | +} |
| 194 | +resource "aws_ecs_service" "bloom_api" { |
| 195 | + region = var.aws_region |
| 196 | + cluster = aws_ecs_cluster.bloom.arn |
| 197 | + name = "bloom-api" |
| 198 | + force_delete = true # allow deletion of the service without scaling down tasks to 0 first. |
| 199 | + task_definition = aws_ecs_task_definition.bloom_api.arn |
| 200 | + wait_for_steady_state = false |
| 201 | + depends_on = [ |
| 202 | + aws_db_instance.bloom, |
| 203 | + aws_vpc_endpoint.secrets_manager, |
| 204 | + aws_route_table_association.api_to_nat, |
| 205 | + ] |
| 206 | + |
| 207 | + network_configuration { |
| 208 | + security_groups = [aws_security_group.api.id] |
| 209 | + subnets = [for s in aws_subnet.api : s.id] |
| 210 | + assign_public_ip = false |
| 211 | + } |
| 212 | + # load_balancer {} |
| 213 | + # health_check_grace_period_seconds - LB |
| 214 | + |
| 215 | + launch_type = "FARGATE" |
| 216 | + scheduling_strategy = "REPLICA" |
| 217 | + availability_zone_rebalancing = "ENABLED" |
| 218 | + desired_count = local.is_prod ? 2 : 1 |
| 219 | + deployment_configuration { |
| 220 | + strategy = "ROLLING" |
| 221 | + } |
| 222 | + deployment_circuit_breaker { |
| 223 | + enable = true |
| 224 | + rollback = true |
| 225 | + } |
| 226 | + deployment_controller { |
| 227 | + type = "ECS" |
| 228 | + } |
| 229 | + deployment_maximum_percent = 200 # allow surge of up to twice desired task count. |
| 230 | +} |
0 commit comments