Skip to content

Commit d5c8868

Browse files
committed
feat: root module to deploy grafana onto ecs
1 parent bf0e523 commit d5c8868

File tree

4 files changed

+1058
-0
lines changed

4 files changed

+1058
-0
lines changed

main.tf

Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
locals {
2+
grafana_port = 3000
3+
4+
# ECS Task Definition
5+
task_cpu = 512
6+
task_memory = 512
7+
grafana_container_name = "grafana"
8+
grafana_container_image = "grafana/grafana:${var.task_definition_grafana_image_version}"
9+
grafana_container_cpu = 512
10+
grafana_container_memory = 512
11+
grafana_container_environment = {
12+
"GF_DATABASE_TYPE" = "postgres"
13+
"GF_DATABASE_HOST" = module.grafana_backend_rds.endpoint
14+
"GF_INSTALL_PLUGINS" = "grafana-athena-datasource"
15+
}
16+
grafana_container_secrets = {
17+
"GF_DATABASE_USER" = "${module.grafana_backend_rds.master_user_secret.secret_arn}:username::"
18+
"GF_DATABASE_PASSWORD" = "${module.grafana_backend_rds.master_user_secret.secret_arn}:password::"
19+
}
20+
21+
# ALB Target Groups
22+
grafana_alb_target_group_key_name = "grafana-tg"
23+
24+
# ALB Listeners
25+
grafana_alb_listener_key_name = "grafana-https"
26+
27+
# ACM Certificates
28+
grafana_acm_certificate_key_name = "grafana"
29+
30+
# RDS Sub-module
31+
rds_port = 5432
32+
rds_engine = "postgres"
33+
rds_db_name = "grafana"
34+
rds_storage_type = "gp2"
35+
}
36+
37+
module "grafana_ecs_deployment" {
38+
source = "infraspecdev/ecs-deployment/aws"
39+
version = "4.3.4"
40+
41+
vpc_id = var.vpc_id
42+
cluster_name = var.cluster_name
43+
44+
# ECS Service
45+
service = {
46+
name = var.service_name
47+
desired_count = var.service_desired_count
48+
49+
load_balancer = [
50+
{
51+
target_group = local.grafana_alb_target_group_key_name
52+
container_name = local.grafana_container_name
53+
container_port = local.grafana_port
54+
}
55+
]
56+
57+
network_configuration = {
58+
subnets = var.service_subnet_ids
59+
security_groups = [module.ecs_service_security_group.security_group_id]
60+
}
61+
62+
deployment_circuit_breaker = {
63+
enable = true
64+
rollback = true
65+
}
66+
67+
tags = var.service_tags
68+
}
69+
70+
# ECS Task Definition
71+
task_definition = {
72+
family = var.task_definition_family
73+
cpu = local.task_cpu
74+
memory = local.task_memory
75+
track_latest = true
76+
77+
task_role_arn = module.grafana_task_iam_role.arn
78+
execution_role_arn = module.grafana_execution_iam_role.arn
79+
80+
runtime_platform = {
81+
operating_system_family = "LINUX"
82+
cpu_architecture = "X86_64"
83+
}
84+
85+
container_definitions = [
86+
{
87+
name = local.grafana_container_name
88+
image = local.grafana_container_image
89+
cpu = local.grafana_container_cpu
90+
memory = local.grafana_container_memory
91+
essential = true
92+
readonlyRootFilesystem = false
93+
94+
environment = [
95+
for name, value in local.grafana_container_environment : {
96+
name = name
97+
value = value
98+
}
99+
]
100+
secrets = [
101+
for name, valueFrom in local.grafana_container_secrets : {
102+
name = name
103+
valueFrom = valueFrom
104+
}
105+
]
106+
107+
portMappings = [
108+
{
109+
name = "server"
110+
containerPort = local.grafana_port
111+
protocol = "tcp"
112+
}
113+
]
114+
}
115+
]
116+
117+
tags = var.task_definition_tags
118+
}
119+
120+
# Capacity Provider
121+
create_capacity_provider = false
122+
123+
# Application Load Balancer
124+
load_balancer = {
125+
name = var.alb_name
126+
internal = false
127+
subnets_ids = var.alb_subnet_ids
128+
security_groups_ids = [module.grafana_alb_security_group.security_group_id]
129+
preserve_host_header = true
130+
131+
target_groups = {
132+
(local.grafana_alb_target_group_key_name) = {
133+
name = var.alb_target_group_name
134+
port = local.grafana_port
135+
protocol = "HTTP"
136+
target_type = "ip"
137+
138+
health_check = {
139+
path = "/api/health"
140+
}
141+
142+
tags = var.alb_target_group_tags
143+
}
144+
}
145+
146+
listeners = {
147+
(local.grafana_alb_listener_key_name) = {
148+
port = 443
149+
protocol = "HTTPS"
150+
certificate = local.grafana_acm_certificate_key_name
151+
152+
default_action = [
153+
{
154+
type = "forward"
155+
target_group = local.grafana_alb_target_group_key_name
156+
}
157+
]
158+
159+
tags = var.alb_listener_tags
160+
}
161+
}
162+
163+
tags = var.alb_tags
164+
}
165+
166+
# S3 Bucket
167+
s3_bucket_name = var.s3_bucket_name
168+
s3_bucket_force_destroy = true
169+
s3_bucket_tags = var.s3_bucket_tags
170+
171+
# ACM
172+
create_acm = true
173+
acm_certificates = {
174+
(local.grafana_acm_certificate_key_name) = {
175+
domain_name = var.acm_grafana_domain_name
176+
key_algorithm = "RSA_2048"
177+
validation_option = {
178+
domain_name = var.acm_grafana_domain_name
179+
validation_domain = var.acm_grafana_domain_name
180+
}
181+
182+
record_zone_id = var.acm_record_zone_id
183+
184+
tags = var.acm_certificate_tags
185+
}
186+
}
187+
}
188+
189+
################################################################################
190+
# IAM Role Sub-module
191+
################################################################################
192+
193+
module "grafana_task_iam_role" {
194+
source = "./modules/iam-role"
195+
196+
name = var.grafana_task_role_name
197+
description = var.grafana_task_role_description
198+
assume_role_policy = {
199+
Version = "2012-10-17"
200+
Statement = [
201+
{
202+
Effect = "Allow"
203+
Action = "sts:AssumeRole"
204+
Principal = {
205+
Service = "ecs-tasks.amazonaws.com"
206+
}
207+
}
208+
]
209+
}
210+
211+
iam_policies = var.grafana_task_role_policies
212+
213+
tags = var.grafana_task_role_tags
214+
}
215+
216+
module "grafana_execution_iam_role" {
217+
source = "./modules/iam-role"
218+
219+
name = var.grafana_execution_role_name
220+
description = var.grafana_execution_role_description
221+
assume_role_policy = {
222+
Version = "2012-10-17"
223+
Statement = [
224+
{
225+
Effect = "Allow"
226+
Action = "sts:AssumeRole"
227+
Principal = {
228+
Service = "ecs-tasks.amazonaws.com"
229+
}
230+
}
231+
]
232+
}
233+
234+
iam_policies = var.grafana_execution_role_policies
235+
236+
tags = var.grafana_execution_role_tags
237+
}
238+
239+
################################################################################
240+
# RDS Sub-module
241+
################################################################################
242+
243+
module "grafana_backend_rds" {
244+
source = "./modules/rds"
245+
246+
identifier = var.rds_identifier
247+
248+
instance_class = var.rds_instance_class
249+
storage_type = local.rds_storage_type
250+
allocated_storage = var.rds_allocated_storage
251+
252+
engine = local.rds_engine
253+
engine_version = var.rds_postgres_engine_version
254+
vpc_security_group_ids = [module.grafana_backend_rds_security_group.security_group_id]
255+
db_name = local.rds_db_name
256+
username = var.rds_username
257+
258+
db_subnet_group_name = var.rds_db_subnet_group_name
259+
db_subnet_group_description = var.rds_db_subnet_group_description
260+
db_subnet_group_subnet_ids = var.rds_db_subnet_group_subnet_ids
261+
db_subnet_group_tags = var.rds_db_subnet_group_tags
262+
263+
db_parameter_group_name = var.rds_db_parameter_group_name
264+
db_parameter_group_description = var.rds_db_parameter_group_description
265+
db_parameter_group_family = var.rds_db_parameter_group_family
266+
db_parameter_group_parameters = var.rds_db_parameter_group_parameters
267+
db_parameter_group_tags = var.rds_db_parameter_group_tags
268+
269+
tags = var.rds_tags
270+
}
271+
272+
################################################################################
273+
# Supporting Resources
274+
################################################################################
275+
276+
################################################################################
277+
# # Security Groups
278+
################################################################################
279+
280+
data "aws_vpc" "this" {
281+
id = var.vpc_id
282+
}
283+
284+
module "ecs_service_security_group" {
285+
source = "terraform-aws-modules/security-group/aws"
286+
version = "~> 5.1.2"
287+
288+
name = "grafana-service"
289+
description = "Defines ingress and egress rules for ECS Grafana Services"
290+
vpc_id = var.vpc_id
291+
292+
ingress_with_source_security_group_id = [
293+
{
294+
description = "Allow ingress on Grafana port from ALB"
295+
from_port = local.grafana_port
296+
to_port = local.grafana_port
297+
protocol = "tcp"
298+
source_security_group_id = module.grafana_alb_security_group.security_group_id
299+
}
300+
]
301+
302+
egress_with_cidr_blocks = [
303+
{
304+
description = "Allow all egress"
305+
from_port = 0
306+
to_port = 0
307+
protocol = "-1"
308+
cidr_blocks = "0.0.0.0/0"
309+
}
310+
]
311+
}
312+
313+
module "grafana_alb_security_group" {
314+
source = "terraform-aws-modules/security-group/aws"
315+
version = "~> 5.1.2"
316+
317+
name = "grafana-alb"
318+
description = "Defines ingress and egress rules for Grafana ALB."
319+
vpc_id = var.vpc_id
320+
321+
ingress_with_cidr_blocks = [
322+
{
323+
description = "Allow all ingress"
324+
from_port = 0
325+
to_port = 0
326+
protocol = "-1"
327+
cidr_blocks = "0.0.0.0/0"
328+
}
329+
]
330+
331+
egress_with_cidr_blocks = [
332+
{
333+
description = "Allow all egress on Grafana port within VPC"
334+
from_port = local.grafana_port
335+
to_port = local.grafana_port
336+
protocol = "tcp"
337+
cidr_blocks = data.aws_vpc.this.cidr_block
338+
}
339+
]
340+
}
341+
342+
module "grafana_backend_rds_security_group" {
343+
source = "terraform-aws-modules/security-group/aws"
344+
version = "~> 5.1.2"
345+
346+
name = "grafana-backend"
347+
description = "Defines ingress and egress rules for Grafana RDS Backend instance."
348+
vpc_id = var.vpc_id
349+
350+
ingress_with_source_security_group_id = [
351+
{
352+
description = "Allow ingress on Postgres port from ECS Grafana Services"
353+
from_port = local.rds_port
354+
to_port = local.rds_port
355+
protocol = "tcp"
356+
source_security_group_id = module.ecs_service_security_group.security_group_id
357+
}
358+
]
359+
360+
egress_with_cidr_blocks = [
361+
{
362+
description = "Allow all egress within VPC"
363+
from_port = 0
364+
to_port = 0
365+
protocol = "-1"
366+
cidr_blocks = data.aws_vpc.this.cidr_block
367+
}
368+
]
369+
}
370+
371+
################################################################################
372+
# # Route53 Record
373+
################################################################################
374+
375+
module "grafana_dns_record" {
376+
source = "./modules/route-53-record"
377+
378+
domain = var.acm_grafana_domain_name
379+
alb_dns_name = module.grafana_ecs_deployment.alb_dns_name
380+
alb_zone_id = module.grafana_ecs_deployment.alb_zone_id
381+
}

0 commit comments

Comments
 (0)