Repository navigation
Add self-hosted Grafana #2287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add self-hosted Grafana #2287
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3329307
Allow certificate module to use a parent zone
theseanything 21e5118
Expose public subnets and ALB logs bucket
theseanything c797c67
Allow deployer role to manage Grafana resources
theseanything c6fcb7a
Add Grafana database and secrets
theseanything c48eab8
Add Grafana IAM roles
theseanything 1fcfb4c
Add Grafana load balancer, networking and logging
theseanything 3aacf8e
Add Grafana ECS service
theseanything 30f9f76
Add Grafana settings for each environment
theseanything 58b6b59
Deploy Grafana from the health root
theseanything f05f0ad
Add Grafana DNS record
theseanything face9f6
Prefix Grafana IAM names with the environment
theseanything 2c371f2
Create the Grafana service after its listener
theseanything File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| resource "aws_route53_record" "grafana" { | ||
| #checkov:skip=CKV2_AWS_23:Not applicable to alias records | ||
| count = var.grafana_settings.enabled ? 1 : 0 | ||
| zone_id = data.terraform_remote_state.account.outputs.route53_hosted_zone_id | ||
| name = "grafana.${var.root_domain}" | ||
| type = "A" | ||
|
|
||
| alias { | ||
| name = data.terraform_remote_state.forms_health.outputs.grafana_alb_dns_name | ||
| zone_id = data.terraform_remote_state.forms_health.outputs.grafana_alb_zone_id | ||
| evaluate_target_health = true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module "grafana" { | ||
| count = var.grafana_settings.enabled ? 1 : 0 | ||
| source = "../../../modules/grafana" | ||
|
|
||
| # The module passes the provider on to the certificate module, which | ||
| # requires it to be passed explicitly here. | ||
| providers = { | ||
| aws = aws | ||
| } | ||
|
|
||
| environment_name = var.environment_name | ||
| root_domain = var.root_domain | ||
|
|
||
| vpc_id = data.terraform_remote_state.forms_environment.outputs.vpc_id | ||
| vpc_cidr_block = data.terraform_remote_state.forms_environment.outputs.vpc_cidr_block | ||
| private_subnet_ids = data.terraform_remote_state.forms_environment.outputs.private_subnet_ids | ||
| public_subnet_ids = data.terraform_remote_state.forms_environment.outputs.public_subnet_ids | ||
| ecs_cluster_arn = data.terraform_remote_state.forms_environment.outputs.ecs_cluster_arn | ||
| alb_access_logs_bucket_name = data.terraform_remote_state.forms_environment.outputs.alb_logs_bucket_name | ||
|
|
||
| kinesis_subscription_role_arn = data.terraform_remote_state.account.outputs.kinesis_subscription_role_arn | ||
|
|
||
| cpu = var.grafana_settings.cpu | ||
| memory = var.grafana_settings.memory | ||
| github_allowed_organizations = var.grafana_settings.github_allowed_organizations | ||
| github_admin_team = var.grafana_settings.github_admin_team | ||
| github_editor_teams = var.grafana_settings.github_editor_teams | ||
| seconds_until_auto_pause = var.grafana_settings.seconds_until_auto_pause | ||
| rds_maintenance_window = var.environmental_settings.rds_maintenance_window | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| output "grafana_alb_dns_name" { | ||
| value = try(module.grafana[0].alb_dns_name, null) | ||
| } | ||
|
|
||
| output "grafana_alb_zone_id" { | ||
| value = try(module.grafana[0].alb_zone_id, null) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Grafana | ||
|
|
||
| Runs Grafana OSS as a single Fargate task behind its own public ALB at | ||
| `grafana.<root_domain>`, backed by its own Aurora Serverless v2 PostgreSQL | ||
| cluster. The load balancer is public and is not behind CloudFront or a WAF; | ||
| GitHub sign in is the only access control. Data sources for CloudWatch and | ||
| X-Ray (Application Signals) are provisioned at start up and authenticate with | ||
| the task role. Grafana alerting is disabled. | ||
|
|
||
| ## Signing in | ||
|
|
||
| Users sign in with GitHub and are sent straight to GitHub when they visit | ||
| Grafana. Access is limited to members of the organisations in | ||
| `github_allowed_organizations`, and a role is assigned from team membership: | ||
| `github_admin_team` members are Grafana server admins, `github_editor_teams` | ||
| members are editors, and anybody else is refused. | ||
|
|
||
| The built-in `admin` user remains for use when GitHub sign in is unavailable. | ||
| Its password is in the SSM parameter `/grafana/admin-password` and the login | ||
| form is reachable at `/login?disableAutoLogin=true`. | ||
|
|
||
| ## Setting up an environment | ||
|
|
||
| 1. Apply `forms/account` (deployer permissions), `forms/environment` (public | ||
| subnet outputs and the ALB logs bucket policy), then `forms/health` (this | ||
| module) and `forms/dns` (the record). | ||
| 2. Create a GitHub OAuth App in the organisation with the homepage URL | ||
| `https://grafana.<root_domain>` and the authorization callback URL | ||
| `https://grafana.<root_domain>/login/github`. If the organisation restricts | ||
| third-party OAuth apps, approve it for the organisation. | ||
| 3. Put the app's client ID and secret in the SSM parameters | ||
| `/grafana/github/client-id` and `/grafana/github/client-secret`. | ||
| 4. Force a new deployment of the `grafana` ECS service so the task picks the | ||
| values up. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The database is allowed to pause after `seconds_until_auto_pause` seconds | ||
| without connections, but Grafana keeps a connection pool open, so in | ||
| practice it only pauses while the service is stopped. | ||
| - The plugins are downloaded from grafana.com when the task starts, so the | ||
| task needs internet egress and a start takes a minute or two. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| module "certificate" { | ||
| source = "../acm-cert-with-dns-validation" | ||
| providers = { | ||
| aws = aws | ||
| aws.certificate = aws | ||
| } | ||
|
|
||
| domain_name = local.host_name | ||
| zone_name = var.root_domain | ||
| subject_alternative_names = [] | ||
| } | ||
|
|
||
| resource "aws_security_group" "alb" { | ||
| #checkov:skip=CKV_AWS_260:Port 80 only redirects to HTTPS | ||
| name = "${local.name}-alb" | ||
| description = "Allows public inbound on 80 and 443 and outbound to the Grafana container" | ||
| vpc_id = var.vpc_id | ||
|
|
||
| ingress { | ||
| description = "Port 80 from public, redirected to 443" | ||
| from_port = 80 | ||
| to_port = 80 | ||
| protocol = "tcp" | ||
| cidr_blocks = ["0.0.0.0/0"] | ||
| } | ||
|
|
||
| ingress { | ||
| description = "Port 443 from public" | ||
| from_port = 443 | ||
| to_port = 443 | ||
| protocol = "tcp" | ||
| cidr_blocks = ["0.0.0.0/0"] | ||
| } | ||
|
|
||
| egress { | ||
| description = "Grafana container port within VPC" | ||
| from_port = local.container_port | ||
| to_port = local.container_port | ||
| protocol = "tcp" | ||
| cidr_blocks = [var.vpc_cidr_block] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_lb" "grafana" { | ||
| #checkov:skip=CKV2_AWS_28:Access is limited to GitHub team members by Grafana, and a WAF is not considered necessary | ||
|
|
||
| name = local.name | ||
| internal = false | ||
| load_balancer_type = "application" | ||
| enable_deletion_protection = true | ||
| drop_invalid_header_fields = true | ||
| security_groups = [aws_security_group.alb.id] | ||
| subnets = var.public_subnet_ids | ||
|
|
||
| access_logs { | ||
| bucket = var.alb_access_logs_bucket_name | ||
| prefix = local.name | ||
| enabled = true | ||
| } | ||
| } | ||
|
|
||
| resource "aws_lb_listener" "http" { | ||
| load_balancer_arn = aws_lb.grafana.arn | ||
| port = "80" | ||
| protocol = "HTTP" | ||
|
|
||
| default_action { | ||
| type = "redirect" | ||
|
|
||
| redirect { | ||
| port = "443" | ||
| protocol = "HTTPS" | ||
| status_code = "HTTP_301" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "aws_lb_listener" "https" { | ||
| load_balancer_arn = aws_lb.grafana.arn | ||
| port = "443" | ||
| protocol = "HTTPS" | ||
| ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06" | ||
| certificate_arn = module.certificate.arn | ||
|
|
||
| default_action { | ||
| type = "forward" | ||
| target_group_arn = aws_lb_target_group.grafana.arn | ||
| } | ||
| } | ||
|
|
||
| resource "aws_lb_target_group" "grafana" { | ||
| #checkov:skip=CKV_AWS_378: We're happy that this is internal traffic within our vpc and we do not want the complexity cost of setting up TLS between the load balancer and application | ||
| name = local.name | ||
| port = local.container_port | ||
| protocol = "HTTP" | ||
| vpc_id = var.vpc_id | ||
| target_type = "ip" | ||
|
|
||
| deregistration_delay = "60" | ||
|
|
||
| health_check { | ||
| path = "/api/health" | ||
| matcher = "200" | ||
| protocol = "HTTP" | ||
|
|
||
| interval = 11 | ||
| timeout = 10 | ||
| unhealthy_threshold = 3 | ||
| healthy_threshold = 2 | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.