| 
 | 1 | +locals {  | 
 | 2 | +  name                = "metabase-example"  | 
 | 3 | +  db_postgres_version = "15"  | 
 | 4 | +  base_tags           = ["metabase", "vpc"]  | 
 | 5 | +}  | 
 | 6 | + | 
 | 7 | +resource "scaleway_vpc" "main" {  | 
 | 8 | +  name = local.name  | 
 | 9 | +}  | 
 | 10 | + | 
 | 11 | +resource "scaleway_vpc_private_network" "main" {  | 
 | 12 | +  name   = local.name  | 
 | 13 | +  vpc_id = scaleway_vpc.main.id  | 
 | 14 | +}  | 
 | 15 | + | 
 | 16 | +resource "scaleway_rdb_instance" "main" {  | 
 | 17 | +  name = "db-${local.name}"  | 
 | 18 | +  tags = concat(local.base_tags, ["pg-${local.db_postgres_version}"])  | 
 | 19 | + | 
 | 20 | +  node_type = "db-play2-nano"  | 
 | 21 | + | 
 | 22 | +  is_ha_cluster = false  | 
 | 23 | +  private_network {  | 
 | 24 | +    pn_id       = scaleway_vpc_private_network.main.id  | 
 | 25 | +    enable_ipam = true  | 
 | 26 | +  }  | 
 | 27 | + | 
 | 28 | +  encryption_at_rest = true  | 
 | 29 | +  volume_size_in_gb  = 10  | 
 | 30 | +  volume_type        = "sbs_5k"  | 
 | 31 | + | 
 | 32 | +  engine = "PostgreSQL-${local.db_postgres_version}"  | 
 | 33 | + | 
 | 34 | +  user_name = var.db_admin_username  | 
 | 35 | +  password  = var.db_admin_password  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +resource "scaleway_rdb_database" "main" {  | 
 | 39 | +  instance_id = scaleway_rdb_instance.main.id  | 
 | 40 | +  name        = local.name  | 
 | 41 | +}  | 
 | 42 | + | 
 | 43 | +resource "scaleway_rdb_user" "main" {  | 
 | 44 | +  instance_id = scaleway_rdb_instance.main.id  | 
 | 45 | + | 
 | 46 | +  name     = var.db_username  | 
 | 47 | +  password = var.db_password  | 
 | 48 | +}  | 
 | 49 | + | 
 | 50 | +resource "scaleway_rdb_privilege" "main" {  | 
 | 51 | +  instance_id   = scaleway_rdb_instance.main.id  | 
 | 52 | +  user_name     = scaleway_rdb_user.main.name  | 
 | 53 | +  database_name = scaleway_rdb_database.main.name  | 
 | 54 | +  permission    = "all"  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +resource "scaleway_container_namespace" "main" {  | 
 | 58 | +  name                     = local.name  | 
 | 59 | +  description              = "Namespace for the Metabase container"  | 
 | 60 | +  tags                     = local.base_tags  | 
 | 61 | +    | 
 | 62 | +  activate_vpc_integration = true  | 
 | 63 | +}  | 
 | 64 | + | 
 | 65 | +locals {  | 
 | 66 | +  db_endpoint     = scaleway_rdb_instance.main.private_network[0]  | 
 | 67 | +  rdb_instance_id = split("/", scaleway_rdb_instance.main.id)[1] # To remove the `<region>/` prefix  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +resource "scaleway_container" "main" {  | 
 | 71 | +  name        = local.name  | 
 | 72 | +  description = "Metabase container running in VPC"  | 
 | 73 | +  tags        = local.base_tags  | 
 | 74 | + | 
 | 75 | +  namespace_id   = scaleway_container_namespace.main.id  | 
 | 76 | +  registry_image = "metabase/metabase:v0.55.x"  | 
 | 77 | + | 
 | 78 | +  private_network_id = scaleway_vpc_private_network.main.id  | 
 | 79 | + | 
 | 80 | +  cpu_limit    = 4000  | 
 | 81 | +  memory_limit = 4000  | 
 | 82 | +  sandbox      = "v1"  | 
 | 83 | + | 
 | 84 | +  http_option = "redirected" # Only allow HTTPs traffic  | 
 | 85 | +  port        = 3000  | 
 | 86 | + | 
 | 87 | +  max_scale = 1 # No real need to have more than one instance running  | 
 | 88 | +  deploy    = true  | 
 | 89 | + | 
 | 90 | +  environment_variables = {  | 
 | 91 | +    MB_ANON_TRACKING_ENABLED : "false"  | 
 | 92 | +    MB_CHECK_FOR_UPDATES : "false"  | 
 | 93 | + | 
 | 94 | +    MB_JETTY_HOST : "0.0.0.0"  | 
 | 95 | + | 
 | 96 | +    MB_DB_TYPE : "postgres"  | 
 | 97 | +    MB_DB_CONNECTION_TIMEOUT_MS : "2000" # Down from 10s for faster feedback loop  | 
 | 98 | + | 
 | 99 | +    # Within a private network, we can refer to resources using their internal hostname  | 
 | 100 | +    # The format is `<resource_id>.<private_network_name>.internal` or `<resource_name>.<private_network_name>.internal`  | 
 | 101 | +    MB_DB_HOST : "${local.rdb_instance_id}.${scaleway_vpc_private_network.main.name}.internal"  | 
 | 102 | +    MB_DB_PORT : local.db_endpoint.port  | 
 | 103 | + | 
 | 104 | +    MB_DB_DBNAME : scaleway_rdb_database.main.name  | 
 | 105 | +    MB_DB_USER : scaleway_rdb_user.main.name # Referencing the user directly to create a Terraform dependency  | 
 | 106 | +  }  | 
 | 107 | + | 
 | 108 | +  secret_environment_variables = {  | 
 | 109 | +    MB_DB_PASS : var.db_password  | 
 | 110 | +  }  | 
 | 111 | +}  | 
0 commit comments