Skip to content

Commit a8d8f13

Browse files
author
Alexander Wiechert
committed
ad provider config
1 parent c24c7db commit a8d8f13

File tree

5 files changed

+255
-83
lines changed

5 files changed

+255
-83
lines changed

main.tf

Lines changed: 64 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,80 @@
1-
resource "aws_s3_bucket" "cur_bucket" {
2-
count = var.use_fake_data ? 0 : 1
3-
bucket = var.s3_bucket_name
1+
resource "aws_ecs_cluster" "this" {
2+
name = var.cluster_name
43
}
54

6-
resource "aws_s3_bucket_policy" "cur_bucket_policy" {
7-
count = var.use_fake_data ? 0 : 1
8-
bucket = aws_s3_bucket.cur_bucket[0].id
9-
10-
policy = jsonencode({
11-
Version = "2012-10-17",
12-
Statement = [
13-
{
14-
Sid = "AWSBillingPermissions",
15-
Effect = "Allow",
16-
Principal = { Service = "billingreports.amazonaws.com" },
17-
Action = "s3:GetBucketAcl",
18-
Resource = aws_s3_bucket.cur_bucket[0].arn
19-
},
20-
{
21-
Sid = "AWSBillingPutObject",
22-
Effect = "Allow",
23-
Principal = { Service = "billingreports.amazonaws.com" },
24-
Action = "s3:PutObject",
25-
Resource = "${aws_s3_bucket.cur_bucket[0].arn}/*"
26-
}
27-
]
28-
})
5+
# --- Load Balancer + Target Group ---
6+
resource "aws_lb" "grafana" {
7+
name = "${var.name}-alb"
8+
internal = false
9+
load_balancer_type = "application"
10+
security_groups = [aws_security_group.grafana_sg.id]
11+
subnets = var.subnet_ids
2912
}
3013

31-
resource "aws_cur_report_definition" "ebs_report" {
32-
count = var.use_fake_data ? 0 : 1
33-
report_name = var.report_name
34-
time_unit = "DAILY"
35-
format = "Parquet"
36-
compression = "Parquet"
37-
additional_schema_elements = ["RESOURCES"]
38-
s3_bucket = var.s3_bucket_name
39-
s3_region = var.aws_region
40-
s3_prefix = var.report_prefix
41-
report_versioning = "CREATE_NEW_REPORT"
42-
}
14+
resource "aws_lb_target_group" "grafana_tg" {
15+
name = "${var.name}-tg"
16+
port = 3000
17+
protocol = "HTTP"
18+
target_type = "ip"
19+
vpc_id = var.vpc_id
4320

44-
resource "aws_athena_database" "cur_database" {
45-
count = var.use_fake_data ? 0 : 1
46-
name = var.athena_database_name
47-
bucket = var.s3_bucket_name
48-
comment = "Athena CUR Database"
21+
health_check {
22+
path = "/"
23+
port = "3000"
24+
protocol = "HTTP"
25+
matcher = "200-399"
26+
interval = 30
27+
timeout = 5
28+
healthy_threshold = 2
29+
unhealthy_threshold = 3
30+
}
4931
}
5032

51-
resource "aws_iam_role" "glue_crawler_role" {
52-
count = var.use_fake_data ? 0 : 1
33+
resource "aws_lb_listener" "https" {
34+
load_balancer_arn = aws_lb.grafana.arn
35+
port = 443
36+
protocol = "HTTPS"
37+
ssl_policy = "ELBSecurityPolicy-2016-08"
38+
certificate_arn = var.certificate_arn
5339

54-
name = "glue-crawler-role"
55-
56-
assume_role_policy = jsonencode({
57-
Version = "2012-10-17",
58-
Statement = [{
59-
Effect = "Allow",
60-
Principal = { Service = "glue.amazonaws.com" },
61-
Action = "sts:AssumeRole"
62-
}]
63-
})
40+
default_action {
41+
type = "forward"
42+
target_group_arn = aws_lb_target_group.grafana_tg.arn
43+
}
6444
}
6545

66-
resource "aws_iam_role_policy" "glue_crawler_policy" {
67-
count = var.use_fake_data ? 0 : 1
68-
role = aws_iam_role.glue_crawler_role[0].id
46+
# --- ECS Service ---
47+
resource "aws_ecs_service" "grafana" {
48+
name = "${var.name}-service"
49+
cluster = aws_ecs_cluster.this.id
50+
task_definition = aws_ecs_task_definition.grafana.arn
51+
launch_type = "FARGATE"
52+
desired_count = 1
53+
54+
network_configuration {
55+
subnets = var.subnet_ids
56+
security_groups = [aws_security_group.grafana_sg.id]
57+
assign_public_ip = true
58+
}
59+
60+
load_balancer {
61+
target_group_arn = aws_lb_target_group.grafana_tg.arn
62+
container_name = "grafana"
63+
container_port = 3000
64+
}
6965

70-
policy = jsonencode({
71-
Version = "2012-10-17",
72-
Statement = [
73-
{
74-
Effect = "Allow",
75-
Action = ["s3:GetObject", "s3:ListBucket"],
76-
Resource = [
77-
"arn:aws:s3:::${var.s3_bucket_name}",
78-
"arn:aws:s3:::${var.s3_bucket_name}/*"
79-
]
80-
},
81-
{
82-
Effect = "Allow",
83-
Action = ["glue:*"],
84-
Resource = "*"
85-
}
86-
]
87-
})
66+
depends_on = [aws_iam_role_policy_attachment.ecs_task_exec_policy]
8867
}
8968

90-
resource "aws_glue_crawler" "cur_crawler" {
91-
count = var.use_fake_data ? 0 : 1
92-
name = var.crawler_name
93-
role = aws_iam_role.glue_crawler_role[0].arn
94-
database_name = var.athena_database_name
69+
# --- DNS (optional) ---
70+
resource "aws_route53_record" "grafana_dns" {
71+
zone_id = var.zone_id
72+
name = var.domain_name
73+
type = "A"
9574

96-
s3_target {
97-
path = "s3://${var.s3_bucket_name}/${var.report_prefix}"
75+
alias {
76+
name = aws_lb.grafana.dns_name
77+
zone_id = aws_lb.grafana.zone_id
78+
evaluate_target_health = true
9879
}
9980
}

modules/grafana/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Grafana ECS Task Submodule
2+
3+
This submodule defines the ECS task definition and execution role required to run Grafana in AWS Fargate. It is designed to be used in combination with infrastructure components (like ALB, Route53, ECS service) defined in the parent module.
4+
5+
## Features
6+
7+
- Creates an ECS-compatible task definition for Grafana
8+
- Defines execution role with appropriate permissions
9+
- Configurable CPU, memory, log group and admin credentials
10+
- Outputs task and role ARNs for use in a parent module
11+
12+
## Usage
13+
14+
```hcl
15+
module "grafana" {
16+
source = "./modules/grafana"
17+
18+
grafana_image = "grafana/grafana-oss:latest"
19+
admin_user = "admin"
20+
admin_password = "changeme123"
21+
cpu = 256
22+
memory = 512
23+
log_group_name = "/ecs/grafana"
24+
execution_role_name = "grafana-exec-role"
25+
}
26+
```
27+
28+
## Inputs
29+
30+
| Name | Description | Type | Default |
31+
|-----------------------|------------------------------------------|--------|------------------------|
32+
| `grafana_image` | Docker image to use | string | `grafana/grafana:latest` |
33+
| `admin_user` | Grafana admin username | string | `admin` |
34+
| `admin_password` | Grafana admin password | string | `admin123` |
35+
| `cpu` | CPU units for ECS task | number | 256 |
36+
| `memory` | Memory (MiB) for ECS task | number | 512 |
37+
| `container_port` | Port exposed by Grafana container | number | 3000 |
38+
| `log_group_name` | CloudWatch log group name | string | `/ecs/grafana` |
39+
| `execution_role_name` | Name of the IAM execution role | string | `ecsTaskExecutionRole` |
40+
41+
## Outputs
42+
43+
| Name | Description |
44+
|-----------------------|--------------------------------------------|
45+
| `task_definition_arn` | ARN of the ECS task definition |
46+
| `execution_role_arn` | ARN of the execution role |
47+
| `container_name` | Name of the Grafana container |
48+
| `container_port` | Port exposed by Grafana |
49+
50+
---
51+
52+
> **Note:** This module is intended to be used within a larger Terraform setup. It does not include networking or service-level configuration. See the root module for complete deployment.

modules/grafana/main.tf

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# --- IAM Role for Fargate Task Execution ---
2+
resource "aws_iam_role" "grafana_task_execution" {
3+
name = "${var.name}-task-execution-role"
4+
5+
assume_role_policy = jsonencode({
6+
Version = "2012-10-17",
7+
Statement = [{
8+
Action = "sts:AssumeRole",
9+
Principal = {
10+
Service = "ecs-tasks.amazonaws.com"
11+
},
12+
Effect = "Allow",
13+
Sid = ""
14+
}]
15+
})
16+
}
17+
18+
resource "aws_iam_role_policy_attachment" "ecs_task_exec_policy" {
19+
role = aws_iam_role.grafana_task_execution.name
20+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
21+
}
22+
23+
# --- Security Group ---
24+
resource "aws_security_group" "grafana_sg" {
25+
name = "${var.name}-sg"
26+
description = "Allow HTTP/HTTPS access to Grafana"
27+
vpc_id = var.vpc_id
28+
29+
ingress {
30+
from_port = 443
31+
to_port = 443
32+
protocol = "tcp"
33+
cidr_blocks = var.allowed_cidr_blocks
34+
}
35+
36+
egress {
37+
from_port = 0
38+
to_port = 0
39+
protocol = "-1"
40+
cidr_blocks = ["0.0.0.0/0"]
41+
}
42+
}
43+
44+
# --- Task Definition ---
45+
resource "aws_ecs_task_definition" "grafana" {
46+
family = "${var.name}-task"
47+
network_mode = "awsvpc"
48+
requires_compatibilities = ["FARGATE"]
49+
cpu = "256"
50+
memory = "512"
51+
execution_role_arn = aws_iam_role.grafana_task_execution.arn
52+
53+
container_definitions = jsonencode([
54+
{
55+
name = "grafana"
56+
image = "grafana/grafana-oss:latest"
57+
portMappings = [{
58+
containerPort = 3000
59+
protocol = "tcp"
60+
}]
61+
environment = [
62+
{
63+
name = "GF_SECURITY_ADMIN_PASSWORD"
64+
value = var.admin_password
65+
}
66+
]
67+
}
68+
])
69+
}
70+
71+
72+

modules/grafana/variables.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
variable "grafana_image" {
2+
description = "Docker image for Grafana"
3+
type = string
4+
default = "grafana/grafana:latest"
5+
}
6+
7+
variable "admin_user" {
8+
description = "Grafana admin username"
9+
type = string
10+
default = "admin"
11+
}
12+
13+
variable "admin_password" {
14+
description = "Grafana admin password"
15+
type = string
16+
default = "admin123"
17+
sensitive = true
18+
}
19+
20+
variable "cpu" {
21+
description = "CPU units for the task (e.g. 256 = 0.25 vCPU)"
22+
type = number
23+
default = 256
24+
}
25+
26+
variable "memory" {
27+
description = "Memory in MiB for the task (e.g. 512 = 0.5 GB)"
28+
type = number
29+
default = 512
30+
}
31+
32+
variable "container_port" {
33+
description = "Port exposed by Grafana container"
34+
type = number
35+
default = 3000
36+
}
37+
38+
variable "log_group_name" {
39+
description = "CloudWatch log group for Grafana logs"
40+
type = string
41+
default = "/ecs/grafana"
42+
}
43+
44+
variable "execution_role_name" {
45+
description = "IAM role name for ECS task execution"
46+
type = string
47+
default = "ecsTaskExecutionRole"
48+
}

modules/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "task_definition_arn" {
2+
description = "ARN of the Grafana ECS task definition"
3+
value = aws_ecs_task_definition.grafana.arn
4+
}
5+
6+
output "execution_role_arn" {
7+
description = "ARN of the ECS execution IAM role"
8+
value = aws_iam_role.grafana_task_execution.arn
9+
}
10+
11+
output "container_name" {
12+
description = "Name of the Grafana container in the task definition"
13+
value = "grafana"
14+
}
15+
16+
output "container_port" {
17+
description = "Port exposed by Grafana container"
18+
value = var.container_port
19+
}

0 commit comments

Comments
 (0)