init: copy from lawndale-infra
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-05-27 22:07:43 +02:00
commit 3ca47a84ce
12 changed files with 420 additions and 0 deletions

86
.drone.yml Normal file
View File

@@ -0,0 +1,86 @@
---
kind: pipeline
type: kubernetes
name: Terraform root module
environment:
TF_IN_AUTOMATION: "1"
GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no -i $${PWD}/id_rsa"
AWS_REGION: eu-central-1
trigger:
ref:
- refs/heads/main
- refs/pull/*/head
steps:
- name: terraform init
image: hashicorp/terraform:1.1.8
commands:
- echo "$${CI_SSH_KEY}" | base64 -d > id_rsa
- chmod 600 id_rsa
- terraform init
environment:
CI_SSH_KEY:
from_secret: ci-ssh-key
AWS_ACCESS_KEY_ID:
from_secret: terraform-aws-key-id
AWS_SECRET_ACCESS_KEY:
from_secret: terraform-aws-secret-access-key
- name: terraform plan
image: hashicorp/terraform:1.1.8
commands:
- terraform plan $([[ $${DRONE_BUILD_EVENT} = cron ]] && echo "--detailed-exit-code") -var-file ci.tfvars -out .tfplan
environment:
AWS_ACCESS_KEY_ID:
from_secret: terraform-aws-key-id
AWS_SECRET_ACCESS_KEY:
from_secret: terraform-aws-secret-access-key
KUBE_TOKEN:
from_secret: lawndale-k8s-ci-token
- name: terraform apply
when:
branch:
- main
event:
- push
image: hashicorp/terraform:1.1.8
commands:
- terraform apply .tfplan
environment:
AWS_ACCESS_KEY_ID:
from_secret: terraform-aws-key-id
AWS_SECRET_ACCESS_KEY:
from_secret: terraform-aws-secret-access-key
KUBE_TOKEN:
from_secret: lawndale-k8s-ci-token
---
kind: pipeline
type: kubernetes
name: Check docs and format
environment:
TF_IN_AUTOMATION: "1"
trigger:
ref:
- refs/pull/*/head
steps:
- name: format and generate docs
image: hashicorp/terraform:1.1.8
commands:
- apk add bash wget
- wget -q https://github.com/terraform-docs/terraform-docs/releases/download/v0.16.0/terraform-docs-v0.16.0-linux-amd64.tar.gz -O - | tar -xz terraform-docs -C /usr/local/bin
- terraform fmt
- terraform-docs .
- git diff --exit-code
---
kind: signature
hmac: e92c995f7f899f06d1320e1e0660cb6cfa28f14d2e3ffe182fd2930534c83e7e
...

34
.gitignore vendored Normal file
View File

@@ -0,0 +1,34 @@
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Crash log files
crash.log
crash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json
# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Include override files you do wish to add to version control using negated pattern
# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# Ignore CLI configuration files
.terraformrc
terraform.rc

5
.terraform-docs.yml Normal file
View File

@@ -0,0 +1,5 @@
formatter: markdown document
output:
mode: inject
file: README.md

62
.terraform.lock.hcl generated Normal file
View File

@@ -0,0 +1,62 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "4.9.0"
constraints = "~> 4.9.0"
hashes = [
"h1:GtmIOZMkKmr9tMLWouHWiGXmKEL/diOTNar5XfOVLjs=",
"zh:084b83aef3335ad4f5e4b8323c6fe43c1ff55e17a7647c6a5cad6af519f72b42",
"zh:132e47ce69f14de4523b84b213cedf7173398acda14245b1ffe7747aac50f050",
"zh:2068baef7dfce3613f3b4f27314175e971f8db68d9cde9ec30b5659f80c68c6c",
"zh:63c6f489683d5f1ac55e82a0df387143ed22701d5f22c109a4d5c9924dd4e437",
"zh:8115fd21965954fa4568c09331e05bb29da967fab8d077419aed09954378e216",
"zh:8efdc95fde108f777ed9c79ae25dc17aea9771903250f5c5c8a4c726b90a345f",
"zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
"zh:9d42a7bc34d84b70c1d1bcc215cabd63abbcbd0352b70bd84da6c3916634932f",
"zh:aacbcceb241aa475888c0869e87593182edeced3170c76a0c960dd9c905df449",
"zh:c7fe7904511052e4102870256819a1917177572cf684f0611ebf767f9c1fbaa8",
"zh:c8e07c3424663d1d0e7e32f4ade8099c19f6326d37c6da98104d90c986ff66fc",
"zh:e47cafbd38b56ef14fd8d727b4ffea847c166b1c684f585ee5fb78983b537248",
]
}
provider "registry.terraform.io/hashicorp/helm" {
version = "2.5.1"
constraints = "~> 2.5.1"
hashes = [
"h1:NasRPC0qqlpGqcF3dsSoOFu7uc5hM+zJm+okd8FgrnQ=",
"zh:140b9748f0ad193a20d69e59d672f3c4eda8a56cede56a92f931bd3af020e2e9",
"zh:17ae319466ed6538ad49e011998bb86565fe0e97bc8b9ad7c8dda46a20f90669",
"zh:3a8bd723c21ba70e19f0395ed7096fc8e08bfc23366f1c3f06a9107eb37c572c",
"zh:3aae3b82adbe6dca52f1a1c8cf51575446e6b0f01f1b1f3b30de578c9af4a933",
"zh:3f65221f40148df57d2888e4f31ef3bf430b8c5af41de0db39a2b964e1826d7c",
"zh:650c74c4f46f5eb01df11d8392bdb7ebee3bba59ac0721000a6ad731ff0e61e2",
"zh:930fb8ab4cd6634472dfd6aa3123f109ef5b32cbe6ef7b4695fae6751353e83f",
"zh:ae57cd4b0be4b9ca252bc5d347bc925e35b0ed74d3dcdebf06c11362c1ac3436",
"zh:d15b1732a8602b6726eac22628b2f72f72d98b75b9c6aabceec9fd696fda696a",
"zh:d730ede1656bd193e2aea5302acec47c4905fe30b96f550196be4a0ed5f41936",
"zh:f010d4f9d8cd15936be4df12bf256cb2175ca1dedb728bd3a866c03d2ee7591f",
"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c",
]
}
provider "registry.terraform.io/hashicorp/kubernetes" {
version = "2.11.0"
constraints = "~> 2.11.0"
hashes = [
"h1:pJiAJwZKUaoAJ4x+3ONJkwEVkjrwGROCGFgj7noPO58=",
"zh:143a19dd0ea3b07fc5e3d9231f3c2d01f92894385c98a67327de74c76c715843",
"zh:1fc757d209e09c3cf7848e4274daa32408c07743698fbed10ee52a4a479b62b6",
"zh:22dfebd0685749c51a8f765d51a1090a259778960ac1cd4f32021a325b2b9b72",
"zh:3039b3b76e870cd8fc404cf75a29c66b171c6ba9b6182e131b6ae2ca648ec7c0",
"zh:3af0a15562fcab4b5684b18802e0239371b2b8ff9197ed069ff4827f795a002b",
"zh:50aaf20336d1296a73315adb66f7687f75bd5c6b1f93a894b95c75cc142810ec",
"zh:682064fabff895ec351860b4fe0321290bbbb17c2a410b62c9bea0039400650e",
"zh:70ac914d5830b3371a2679d8f77cc20c419a6e12925145afae6c977c8eb90934",
"zh:710aa02cccf7b0f3fb50880d6d2a7a8b8c9435248666616844ba71f74648cddc",
"zh:88e418118cd5afbdec4984944c7ab36950bf48e8d3e09e090232e55eecfb470b",
"zh:9cef159377bf23fa331f8724fdc6ce27ad39a217a4bae6df3b1ca408fc643da6",
"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c",
]
}

55
README.md Normal file
View File

@@ -0,0 +1,55 @@
# Terraform module for Grafana deployment
[![Build Status](https://drone.thomasklein.me/api/badges/thomasklein/terraform-app-grafana/status.svg)](https://drone.thomasklein.me/thomasklein/terraform-app-grafana)
This module manages the grafana installation on lawndale.
<!-- BEGIN_TF_DOCS -->
## Requirements
The following requirements are needed by this module:
- <a name="requirement_aws"></a> [aws](#requirement\_aws) (~> 4.9.0)
- <a name="requirement_helm"></a> [helm](#requirement\_helm) (~> 2.5.1)
- <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) (~> 2.11.0)
## Providers
The following providers are used by this module:
- <a name="provider_aws"></a> [aws](#provider\_aws) (4.9.0)
- <a name="provider_helm"></a> [helm](#provider\_helm) (2.5.1)
- <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) (2.11.0)
## Modules
No modules.
## Resources
The following resources are used by this module:
- [aws_cognito_user_pool_client.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool_client) (resource)
- [helm_release.this](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) (resource)
- [kubernetes_namespace.this](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) (resource)
- [kubernetes_persistent_volume.this](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/persistent_volume) (resource)
- [kubernetes_persistent_volume_claim.this](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/persistent_volume_claim) (resource)
- [aws_cognito_user_pools.thomasklein_infra](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cognito_user_pools) (data source)
- [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) (data source)
## Required Inputs
No required inputs.
## Optional Inputs
No optional inputs.
## Outputs
No outputs.
<!-- END_TF_DOCS -->

10
backend.tf Normal file
View File

@@ -0,0 +1,10 @@
terraform {
backend "s3" {
bucket = "thomasklein-global-states"
key = "apps/lawndale/grafana"
region = "eu-central-1"
encrypt = true
kms_key_id = "a767a3ce-c505-44c0-9f93-6f53678c69c7"
dynamodb_table = "thomasklein-global-state-locks"
}
}

63
helm.tf Normal file
View File

@@ -0,0 +1,63 @@
resource "helm_release" "this" {
name = "grafana"
namespace = kubernetes_namespace.this.metadata.0.name
repository = "https://grafana.github.io/helm-charts"
chart = "grafana"
values = [
jsonencode({
ingress = {
enabled = true
hosts = [local.ingress_domain]
annotations = {
"kubernetes.io/ingress.class" = "traefik"
"traefik.ingress.kubernetes.io/router.entrypoints" = "websecure"
"traefik.ingress.kubernetes.io/router.tls" = "true"
"traefik.ingress.kubernetes.io/router.tls.certresolver" = "acme-thomasklein-me"
"traefik.ingress.kubernetes.io/router.tls.domains.0.main" = local.ingress_domain
"traefik.ingress.kubernetes.io/router.middlewares" = "redirect-metrics@file" # hide /metrics from the internet
}
labels = {}
}
}),
jsonencode({
persistence = {
enabled = "true"
type = "pvc"
existingClaim = kubernetes_persistent_volume_claim.this.metadata.0.name
}
}),
jsonencode({
"grafana.ini" = {
server = {
root_url = "https://${local.ingress_domain}"
}
}
}),
jsonencode({
envRenderSecret = {
GF_AUTH_GENERIC_OAUTH_NAME = "Cognito"
GF_AUTH_GENERIC_OAUTH_ENABLED = "true"
GF_AUTH_GENERIC_OAUTH_AUTH_URL = local.cognito_authz_url
GF_AUTH_GENERIC_OAUTH_TOKEN_URL = local.cognito_token_url
// below, the `?` is necessary to fix a bad assumtion on grafana's side
GF_AUTH_GENERIC_OAUTH_API_URL = "${local.cognito_userinfo_url}?"
GF_AUTH_GENERIC_OAUTH_SCOPES = "email openid profile"
}
})
]
set_sensitive {
name = "envRenderSecret.GF_AUTH_GENERIC_OAUTH_CLIENT_ID"
value = aws_cognito_user_pool_client.this.id
}
set_sensitive {
name = "envRenderSecret.GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET"
value = aws_cognito_user_pool_client.this.client_secret
}
}

7
locals.tf Normal file
View File

@@ -0,0 +1,7 @@
locals {
namespace = "grafana"
ingress_domain = "grafana.thomasklein.me"
}
data "aws_region" "current" {}

5
namespace.tf Normal file
View File

@@ -0,0 +1,5 @@
resource "kubernetes_namespace" "this" {
metadata {
name = local.namespace
}
}

29
oidc.tf Normal file
View File

@@ -0,0 +1,29 @@
data "aws_cognito_user_pools" "thomasklein_infra" {
name = "thomasklein-infra"
}
resource "aws_cognito_user_pool_client" "this" {
name = "grafana"
user_pool_id = data.aws_cognito_user_pools.thomasklein_infra.ids[0]
allowed_oauth_flows_user_pool_client = true
allowed_oauth_flows = ["code"]
allowed_oauth_scopes = ["profile", "email", "openid"]
supported_identity_providers = ["COGNITO"]
generate_secret = true
callback_urls = ["https://${local.ingress_domain}/login/generic_oauth"]
}
locals {
# these should be available as an attribute
cognito_base_url = "https://${data.aws_cognito_user_pools.thomasklein_infra.name}.auth.${data.aws_region.current.name}.amazoncognito.com"
cognito_authz_url = "${local.cognito_base_url}/oauth2/authorize"
cognito_token_url = "${local.cognito_base_url}/oauth2/token"
cognito_userinfo_url = "${local.cognito_base_url}/oauth2/userInfo"
}

28
provider.tf Normal file
View File

@@ -0,0 +1,28 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.9.0"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.5.1"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.11.0"
}
}
}
provider "aws" {
# Configuration options
default_tags {
tags = {
"managed-by" = "terraform"
}
}
}

36
volume.tf Normal file
View File

@@ -0,0 +1,36 @@
locals {
volume_storage_capacity = "1Gi"
}
resource "kubernetes_persistent_volume" "this" {
metadata {
name = "pv-p9hostpath-grafana"
}
spec {
capacity = {
storage = local.volume_storage_capacity
}
access_modes = ["ReadWriteMany"]
persistent_volume_source {
host_path {
path = "/mnt/datastore/grafana"
}
}
}
}
resource "kubernetes_persistent_volume_claim" "this" {
metadata {
name = "grafana"
namespace = kubernetes_namespace.this.metadata.0.name
}
spec {
access_modes = ["ReadWriteMany"]
resources {
requests = {
storage = local.volume_storage_capacity
}
}
volume_name = kubernetes_persistent_volume.this.metadata.0.name
}
}