Skip to content

Commit 22cfd70

Browse files
authored
chore: grafana dash (#471)
* chore: grafana dash add grafana dashboard for otp server * chore: grafana switch add switch to make grafana optional * chore: tf config keep config flexible
1 parent 12461dc commit 22cfd70

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

.github/workflows/publish-otp-provider-image.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
CORS_ORIGINS=https://dev.sandbox.loginproxy.gov.bc.ca,https://test.sandbox.loginproxy.gov.bc.ca,https://sandbox.loginproxy.gov.bc.ca,https://sso-playground.apps.gold.devops.gov.bc.ca
2929
NODE_ENV=production
3030
HASH_SALT=${{ secrets.DEV_HASH_SALT }}
31+
GRAFANA_ADMIN_PASS=${{secrets.DEV_GRAFANA_ADMIN_PASS}}
3132
3233
EOF
3334
- name: Checkout repository
@@ -83,6 +84,8 @@ jobs:
8384
otp_attempts_allowed="5"
8485
otp_resends_allowed_per_day="4"
8586
otp_resend_interval_minutes="[1,2,5,60]"
87+
grafana_admin_password="${{env.GRAFANA_ADMIN_PASS}}"
88+
enable_grafana=true
8689
EOF
8790
8891
working-directory: ./docker/otp-provider/terraform

docker/otp-provider/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ node_modules/
66
/playwright-report/
77
/blob-report/
88
/playwright/.cache/
9+
10+
# terraform
11+
.terraform
12+
**/*.tfvars

docker/otp-provider/terraform/alb.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,40 @@ resource "aws_alb_target_group" "this" {
4141
}
4242
tags = var.app_tags
4343
}
44+
45+
# Grafana
46+
47+
resource "aws_lb_target_group" "grafana" {
48+
count = var.enable_grafana ? 1 : 0
49+
name = "${var.app_name}-grafana"
50+
port = 3000
51+
protocol = "HTTP"
52+
vpc_id = data.aws_vpc.selected.id
53+
target_type = "ip"
54+
55+
health_check {
56+
path = "/"
57+
matcher = "200-399"
58+
interval = 30
59+
timeout = 5
60+
healthy_threshold = 2
61+
unhealthy_threshold = 2
62+
}
63+
}
64+
65+
resource "aws_alb_listener_rule" "grafana" {
66+
count = var.enable_grafana ? 1 : 0
67+
listener_arn = aws_alb_listener.this.arn
68+
priority = 100
69+
70+
action {
71+
type = "forward"
72+
target_group_arn = aws_lb_target_group.grafana[0].arn
73+
}
74+
75+
condition {
76+
path_pattern {
77+
values = ["/grafana/*"]
78+
}
79+
}
80+
}

docker/otp-provider/terraform/api-gateway.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,22 @@ resource "aws_apigatewayv2_api_mapping" "this" {
4949
domain_name = aws_apigatewayv2_domain_name.this.id
5050
stage = "$default"
5151
}
52+
53+
# Grafana
54+
55+
resource "aws_apigatewayv2_integration" "grafana" {
56+
count = var.enable_grafana ? 1 : 0
57+
api_id = aws_apigatewayv2_api.this.id
58+
integration_type = "HTTP_PROXY"
59+
connection_id = aws_apigatewayv2_vpc_link.this.id
60+
connection_type = "VPC_LINK"
61+
integration_method = "ANY"
62+
integration_uri = aws_alb_listener.this.arn
63+
}
64+
65+
resource "aws_apigatewayv2_route" "grafana" {
66+
count = var.enable_grafana ? 1 : 0
67+
api_id = aws_apigatewayv2_api.this.id
68+
route_key = "ANY /grafana/{proxy+}"
69+
target = "integrations/${aws_apigatewayv2_integration.grafana[0].id}"
70+
}

docker/otp-provider/terraform/ecs.tf

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,95 @@ resource "aws_ecs_service" "this" {
213213
data "aws_secretsmanager_secret_version" "this" {
214214
secret_id = aws_secretsmanager_secret.this.id
215215
}
216+
217+
# Grafana
218+
219+
resource "aws_ecs_cluster" "grafana" {
220+
count = var.enable_grafana ? 1 : 0
221+
name = "grafana-cluster"
222+
}
223+
224+
resource "aws_iam_role" "ecs_task_execution" {
225+
count = var.enable_grafana ? 1 : 0
226+
name = "ecsTaskExecutionRole-grafana"
227+
228+
assume_role_policy = jsonencode({
229+
Version = "2012-10-17",
230+
Statement = [{
231+
Action = "sts:AssumeRole",
232+
Effect = "Allow",
233+
Principal = {
234+
Service = "ecs-tasks.amazonaws.com"
235+
}
236+
}]
237+
})
238+
}
239+
240+
resource "aws_iam_role_policy_attachment" "ecs_task_execution_attach" {
241+
count = var.enable_grafana ? 1 : 0
242+
role = aws_iam_role.ecs_task_execution[0].name
243+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
244+
}
245+
246+
resource "aws_ecs_task_definition" "grafana" {
247+
count = var.enable_grafana ? 1 : 0
248+
family = "grafana-task"
249+
network_mode = "awsvpc"
250+
requires_compatibilities = ["FARGATE"]
251+
# This is a lightweight image so running the minimum allowed fargate container
252+
cpu = "256"
253+
memory = "512"
254+
255+
execution_role_arn = aws_iam_role.ecs_task_execution[0].arn
256+
257+
container_definitions = jsonencode([
258+
{
259+
name = "grafana"
260+
image = "grafana/grafana:latest"
261+
essential = true
262+
portMappings = [
263+
{
264+
containerPort = 3000
265+
protocol = "tcp"
266+
}
267+
]
268+
environment = [
269+
{
270+
name = "GF_SECURITY_ADMIN_PASSWORD"
271+
value = var.grafana_admin_password
272+
},
273+
{
274+
name = "GF_SERVER_ROOT_URL"
275+
value = "https://${var.custom_domain_name}/grafana/"
276+
},
277+
{
278+
name = "GF_SERVER_SERVE_FROM_SUB_PATH"
279+
value = "true"
280+
}
281+
]
282+
}
283+
])
284+
}
285+
286+
resource "aws_ecs_service" "grafana" {
287+
count = var.enable_grafana ? 1 : 0
288+
name = "grafana-service"
289+
cluster = aws_ecs_cluster.grafana[0].id
290+
task_definition = aws_ecs_task_definition.grafana[0].arn
291+
desired_count = 1
292+
launch_type = "FARGATE"
293+
294+
network_configuration {
295+
security_groups = [data.aws_security_group.app.id]
296+
subnets = [data.aws_subnet.a.id, data.aws_subnet.b.id]
297+
assign_public_ip = false
298+
}
299+
300+
load_balancer {
301+
target_group_arn = aws_lb_target_group.grafana[0].arn
302+
container_name = "grafana"
303+
container_port = 3000
304+
}
305+
306+
depends_on = [aws_alb_listener_rule.grafana]
307+
}

docker/otp-provider/terraform/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,13 @@ variable "otp_resend_interval_minutes" {
185185
type = string
186186
default = ""
187187
}
188+
189+
variable "grafana_admin_password" {
190+
sensitive = true
191+
type = string
192+
}
193+
194+
variable "enable_grafana" {
195+
type = bool
196+
default = true
197+
}

0 commit comments

Comments
 (0)