@@ -19,7 +19,7 @@ resource "aws_alb_target_group" "app" {
1919 protocol = " HTTP"
2020 matcher = " 200"
2121 timeout = 3
22- path = " /api/health" # Grafana health check endpoint
22+ path = " /api/health" # Grafana health check endpoint
2323 unhealthy_threshold = 2
2424 }
2525}
@@ -99,36 +99,36 @@ resource "aws_appautoscaling_policy" "down" {
9999# ecs.tf
100100
101101resource "aws_ecs_cluster" "main" {
102- name = " ${ local . prefix } -cluster"
102+ name = " ${ local . prefix } -cluster"
103103}
104104
105105data "template_file" "grafana_app" {
106- template = file (" ${ path . module } /templates/ecs/grafana_app.json.tpl" )
107-
108- vars = {
109- app_image = local.app_image
110- app_name = local.app_name
111- app_port = var.app_port
112- fargate_cpu = var.fargate_cpu
113- fargate_memory = var.fargate_memory
114- aws_region = var.aws_region
115- log_group = local.log_group
116- health_check_path = var.health_check_path
117- }
106+ template = file (" ${ path . module } /templates/ecs/grafana_app.json.tpl" )
107+
108+ vars = {
109+ app_image = local.app_image
110+ app_name = local.app_name
111+ app_port = var.app_port
112+ fargate_cpu = var.fargate_cpu
113+ fargate_memory = var.fargate_memory
114+ aws_region = var.aws_region
115+ log_group = local.log_group
116+ health_check_path = var.health_check_path
117+ }
118118}
119119
120120resource "aws_ecs_task_definition" "app" {
121- family = " ${ local . prefix } -app"
122- execution_role_arn = aws_iam_role. ecs_task_execution_role . arn
123- task_role_arn = aws_iam_role. ecs_task_role . arn
124- network_mode = " awsvpc"
125- requires_compatibilities = [" FARGATE" ]
126- cpu = var. fargate_cpu
127- memory = var. fargate_memory
128- container_definitions = data. template_file . grafana_app . rendered
129- tags = merge (var. tags , {
130- Name = " ${ local . prefix } -task"
131- })
121+ family = " ${ local . prefix } -app"
122+ execution_role_arn = aws_iam_role. ecs_task_execution_role . arn
123+ task_role_arn = aws_iam_role. ecs_task_role . arn
124+ network_mode = " awsvpc"
125+ requires_compatibilities = [" FARGATE" ]
126+ cpu = var. fargate_cpu
127+ memory = var. fargate_memory
128+ container_definitions = data. template_file . grafana_app . rendered
129+ tags = merge (var. tags , {
130+ Name = " ${ local . prefix } -task"
131+ })
132132
133133}
134134
@@ -209,16 +209,16 @@ resource "aws_iam_policy" "ecs_task_execution_policy" {
209209 Statement = [
210210 {
211211 Effect = " Allow" ,
212- " Action" : [
213- " ecr:GetDownloadUrlForLayer" ,
214- " ecr:BatchGetImage" ,
215- " ecr:BatchCheckLayerAvailability" ,
216- " ecr:GetAuthorizationToken" ,
217- " logs:CreateLogGroup" ,
218- " logs:CreateLogStream" ,
219- " logs:PutLogEvents" ,
220- " s3:*"
221- ],
212+ " Action" : [
213+ " ecr:GetDownloadUrlForLayer" ,
214+ " ecr:BatchGetImage" ,
215+ " ecr:BatchCheckLayerAvailability" ,
216+ " ecr:GetAuthorizationToken" ,
217+ " logs:CreateLogGroup" ,
218+ " logs:CreateLogStream" ,
219+ " logs:PutLogEvents" ,
220+ " s3:*"
221+ ],
222222 Resource = " *"
223223 }
224224 ]
@@ -260,7 +260,7 @@ resource "aws_iam_role" "ecs_task_role" {
260260EOF
261261}
262262
263- # Resource = ${aws_iam_role.monitoring_role.arn}
263+ # Resource = ${aws_iam_role.monitoring_role.arn}
264264
265265
266266resource "aws_iam_policy" "ecs_task_policy" {
@@ -273,9 +273,9 @@ resource "aws_iam_policy" "ecs_task_policy" {
273273 Effect = " Allow" ,
274274 Action = [
275275 " logs:CreateLogGroup" ,
276- " logs:CreateLogStream" ,
277- " logs:PutLogEvents" ,
278- ],
276+ " logs:CreateLogStream" ,
277+ " logs:PutLogEvents" ,
278+ ],
279279 Resource = " *"
280280 }
281281 ]
@@ -292,7 +292,7 @@ resource "aws_iam_role_policy_attachment" "task_s3" {
292292data "aws_iam_policy_document" "ecs_auto_scale_role" {
293293 version = " 2012-10-17"
294294 statement {
295- effect = " Allow"
295+ effect = " Allow"
296296 actions = [" sts:AssumeRole" ]
297297
298298 principals {
@@ -303,7 +303,7 @@ data "aws_iam_policy_document" "ecs_auto_scale_role" {
303303}
304304# ECS auto scale role
305305resource "aws_iam_role" "ecs_auto_scale_role" {
306- name = " ${ local . prefix } -ecs_role"
306+ name = " ${ local . prefix } -ecs_role"
307307 assume_role_policy = data. aws_iam_policy_document . ecs_auto_scale_role . json
308308}
309309# ECS auto scale role policy attachment
@@ -318,13 +318,13 @@ resource "aws_iam_role" "monitoring_role" {
318318 name = " ${ local . prefix } -monitoring-role"
319319
320320 assume_role_policy = jsonencode ({
321- " Version" : " 2012-10-17" ,
322- " Statement" : [
321+ " Version" : " 2012-10-17" ,
322+ " Statement" : [
323323 {
324- " Effect" : " Allow" ,
325- " Action" : " sts:AssumeRole" ,
326- " Principal" : {
327- " Service" : " ecs-tasks.amazonaws.com"
324+ " Effect" : " Allow" ,
325+ " Action" : " sts:AssumeRole" ,
326+ " Principal" : {
327+ " Service" : " ecs-tasks.amazonaws.com"
328328 }
329329 },
330330 {
@@ -339,35 +339,35 @@ resource "aws_iam_role" "monitoring_role" {
339339}
340340
341341resource "aws_iam_role_policy" "monitoring_policy" {
342- name = " ${ local . prefix } -monitoring-policy"
343- role = aws_iam_role. monitoring_role . id
342+ name = " ${ local . prefix } -monitoring-policy"
343+ role = aws_iam_role. monitoring_role . id
344344
345345 policy = jsonencode ({
346- " Version" : " 2012-10-17" ,
347- " Statement" : [
346+ " Version" : " 2012-10-17" ,
347+ " Statement" : [
348348 {
349- " Sid" : " AllowReadingMetricsFromCloudWatch" ,
350- " Effect" : " Allow" ,
351- " Action" : [
349+ " Sid" : " AllowReadingMetricsFromCloudWatch" ,
350+ " Effect" : " Allow" ,
351+ " Action" : [
352352 " cloudwatch:DescribeAlarmsForMetric" ,
353353 " cloudwatch:DescribeAlarmHistory" ,
354354 " cloudwatch:DescribeAlarms" ,
355355 " cloudwatch:ListMetrics" ,
356356 " cloudwatch:GetMetricData" ,
357357 " cloudwatch:GetInsightRuleReport"
358358 ],
359- " Resource" : " *"
359+ " Resource" : " *"
360360 },
361361 {
362- " Sid" : " AllowReadingResourceMetricsFromPerformanceInsights" ,
363- " Effect" : " Allow" ,
364- " Action" : " pi:GetResourceMetrics" ,
365- " Resource" : " *"
362+ " Sid" : " AllowReadingResourceMetricsFromPerformanceInsights" ,
363+ " Effect" : " Allow" ,
364+ " Action" : " pi:GetResourceMetrics" ,
365+ " Resource" : " *"
366366 },
367367 {
368- " Sid" : " AllowReadingLogsFromCloudWatch" ,
369- " Effect" : " Allow" ,
370- " Action" : [
368+ " Sid" : " AllowReadingLogsFromCloudWatch" ,
369+ " Effect" : " Allow" ,
370+ " Action" : [
371371 " logs:DescribeLogGroups" ,
372372 " logs:DescribeLogStreams" ,
373373 " logs:GetLogEvents" ,
@@ -377,23 +377,23 @@ resource "aws_iam_role_policy" "monitoring_policy" {
377377 " logs:StopQuery" ,
378378 " logs:GetQueryResults"
379379 ],
380- " Resource" : " *"
380+ " Resource" : " *"
381381 },
382382 {
383- " Sid" : " AllowReadingTagsInstancesRegionsFromEC2" ,
384- " Effect" : " Allow" ,
385- " Action" : [
383+ " Sid" : " AllowReadingTagsInstancesRegionsFromEC2" ,
384+ " Effect" : " Allow" ,
385+ " Action" : [
386386 " ec2:DescribeTags" ,
387387 " ec2:DescribeInstances" ,
388388 " ec2:DescribeRegions"
389389 ],
390- " Resource" : " *"
390+ " Resource" : " *"
391391 },
392392 {
393- " Sid" : " AllowReadingResourcesForTags" ,
394- " Effect" : " Allow" ,
395- " Action" : " tag:GetResources" ,
396- " Resource" : " *"
393+ " Sid" : " AllowReadingResourcesForTags" ,
394+ " Effect" : " Allow" ,
395+ " Action" : " tag:GetResources" ,
396+ " Resource" : " *"
397397 }
398398 ]
399399 })
@@ -405,79 +405,79 @@ resource "aws_iam_role_policy" "monitoring_policy" {
405405data "aws_availability_zones" "available" {}
406406
407407resource "aws_vpc" "grafana_main" {
408- cidr_block = var. cidr_block
409- // enable dns resolution
410- enable_dns_support = true
411- enable_dns_hostnames = true
412- tags = {
413- Name = " ${ local . prefix } -vpc"
414- }
408+ cidr_block = var. cidr_block
409+ // enable dns resolution
410+ enable_dns_support = true
411+ enable_dns_hostnames = true
412+ tags = {
413+ Name = " ${ local . prefix } -vpc"
414+ }
415415}
416416
417417
418418# Create var.az_count private subnets, each in a different AZ
419419resource "aws_subnet" "grafana_private" {
420- count = var. az_count
421- cidr_block = cidrsubnet (aws_vpc. grafana_main . cidr_block , 8 , count. index )
422- availability_zone = data. aws_availability_zones . available . names [count . index ]
423- vpc_id = aws_vpc. grafana_main . id
424- tags = merge (var. tags , {
425- Name = " ${ local . prefix } -private-subnet-${ count . index } "
426- })
420+ count = var. az_count
421+ cidr_block = cidrsubnet (aws_vpc. grafana_main . cidr_block , 8 , count. index )
422+ availability_zone = data. aws_availability_zones . available . names [count . index ]
423+ vpc_id = aws_vpc. grafana_main . id
424+ tags = merge (var. tags , {
425+ Name = " ${ local . prefix } -private-subnet-${ count . index } "
426+ })
427427}
428428
429429
430430# Create var.az_count public subnets, each in a different AZ
431431resource "aws_subnet" "grafana_public" {
432- count = var. az_count
433- cidr_block = cidrsubnet (aws_vpc. grafana_main . cidr_block , 8 , var. az_count + count. index )
434- availability_zone = data. aws_availability_zones . available . names [count . index ]
435- vpc_id = aws_vpc. grafana_main . id
436- map_public_ip_on_launch = true
437- tags = merge (var. tags , {
438- Name = " ${ local . prefix } -public-subnet-${ count . index } "
439- })
432+ count = var. az_count
433+ cidr_block = cidrsubnet (aws_vpc. grafana_main . cidr_block , 8 , var. az_count + count. index )
434+ availability_zone = data. aws_availability_zones . available . names [count . index ]
435+ vpc_id = aws_vpc. grafana_main . id
436+ map_public_ip_on_launch = true
437+ tags = merge (var. tags , {
438+ Name = " ${ local . prefix } -public-subnet-${ count . index } "
439+ })
440440}
441441
442442
443443# Internet Gateway for the public subnet
444444resource "aws_internet_gateway" "gw" {
445- vpc_id = aws_vpc. grafana_main . id
446- tags = merge (var. tags , {
447- Name = " ${ local . prefix } -igw"
448- })
445+ vpc_id = aws_vpc. grafana_main . id
446+ tags = merge (var. tags , {
447+ Name = " ${ local . prefix } -igw"
448+ })
449449}
450450
451451# Route the public subnet traffic through the IGW
452452resource "aws_route" "internet_access" {
453- route_table_id = aws_vpc. grafana_main . main_route_table_id
454- destination_cidr_block = " 0.0.0.0/0"
455- gateway_id = aws_internet_gateway. gw . id
453+ route_table_id = aws_vpc. grafana_main . main_route_table_id
454+ destination_cidr_block = " 0.0.0.0/0"
455+ gateway_id = aws_internet_gateway. gw . id
456456}
457457
458458# Create a new route table for the private subnets
459459resource "aws_route_table" "private" {
460- count = var. az_count
461- vpc_id = aws_vpc. grafana_main . id
462- tags = merge (var. tags , {
463- Name = " ${ local . prefix } -private-rt-${ count . index } "
464- })
460+ count = var. az_count
461+ vpc_id = aws_vpc. grafana_main . id
462+ tags = merge (var. tags , {
463+ Name = " ${ local . prefix } -private-rt-${ count . index } "
464+ })
465465}
466466
467467# Route the private subnet traffic through the NAT Gateway
468468resource "aws_route" "private_nat_gateway" {
469- count = var. az_count
470- route_table_id = element (aws_route_table. private [* ]. id , count. index )
469+ count = var. az_count
470+ route_table_id = element (aws_route_table. private [* ]. id , count. index )
471471 destination_cidr_block = " 0.0.0.0/0"
472472 nat_gateway_id = aws_nat_gateway. nat . id
473473}
474474
475475
476476# Explicitly associate the newly created route tables to the private subnets (so they don't default to the main route table)
477477resource "aws_route_table_association" "private" {
478- count = var. az_count
479- subnet_id = element (aws_subnet. grafana_private [* ]. id , count. index )
480- route_table_id = element (aws_route_table. private [* ]. id , count. index )
478+ count = var. az_count
479+ subnet_id = element (aws_subnet. grafana_private [* ]. id , count. index )
480+ route_table_id = element (aws_route_table. private [* ]. id , count. index )
481481}
482482
483483
@@ -544,7 +544,7 @@ resource "aws_eip" "nat" {
544544
545545resource "aws_nat_gateway" "nat" {
546546 allocation_id = aws_eip. nat . id
547- subnet_id = element (aws_subnet. grafana_public [* ]. id , 0 )
547+ subnet_id = element (aws_subnet. grafana_public [* ]. id , 0 )
548548 tags = merge (var. tags , {
549549 Name = " ${ local . prefix } -nat-gw"
550550 })
0 commit comments