feat: support multiple environment with workspace

This commit is contained in:
RouxAntoine 2025-03-06 07:52:32 +01:00
parent 2a54e1d4ab
commit 2a96d470e0
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
2 changed files with 55 additions and 15 deletions

30
main.tf
View File

@ -12,6 +12,7 @@ terraform {
endpoints = { endpoints = {
s3 = "http://s3.localdomain" s3 = "http://s3.localdomain"
} }
//@formatter:off
key = "postgres.tfstate" key = "postgres.tfstate"
bucket = "terraform" bucket = "terraform"
region = "FR" region = "FR"
@ -22,15 +23,34 @@ terraform {
shared_credentials_files = ["~/.aws/credentials"] shared_credentials_files = ["~/.aws/credentials"]
profile = "minio" profile = "minio"
use_path_style = true use_path_style = true
//@formatter:on
}
}
locals {
# because merge function didn't support deep merging
# order matter because otherwise typing contained in var.connections is set as value in the result local.connection
connection = {for key, config in var.connections : key => merge(config, local.private_connection[key])}
private_connection = {
"default" = {
db = "postgres"
host = "database.localdomain"
port = 5432
},
"prod" = {
db = "postgres"
host = "database-trusted-primate.localdomain"
port = 5432
}
} }
} }
provider "postgresql" { provider "postgresql" {
host = "database.localdomain" host = local.connection[terraform.workspace].host
port = 5432 port = local.connection[terraform.workspace].port
database = var.postgres.db database = local.connection[terraform.workspace].db
username = var.postgres.user username = local.connection[terraform.workspace].username
password = var.postgres.secret password = local.connection[terraform.workspace].password
sslmode = "disable" sslmode = "disable"
connect_timeout = 15 connect_timeout = 15
} }

View File

@ -1,6 +1,26 @@
variable "connections" {
description = "postgres configuration map by environment"
sensitive = true
type = object({
default : object({
db : optional(string)
host : optional(string)
port : optional(number)
username : optional(string, "postgres")
password : string
})
prod : object({
db : optional(string)
host : optional(string)
port : optional(number)
username : optional(string, "postgres")
password : string
})
})
}
variable "postgres" { variable "postgres" {
description = "postgres configuration" description = "postgres configuration"
sensitive = true
type = object({ type = object({
user = optional(string, "postgres") user = optional(string, "postgres")
secret = string secret = string