feat: support multiple environment with workspace
This commit is contained in:
parent
2a54e1d4ab
commit
2a96d470e0
30
main.tf
30
main.tf
@ -12,6 +12,7 @@ terraform {
|
||||
endpoints = {
|
||||
s3 = "http://s3.localdomain"
|
||||
}
|
||||
//@formatter:off
|
||||
key = "postgres.tfstate"
|
||||
bucket = "terraform"
|
||||
region = "FR"
|
||||
@ -22,15 +23,34 @@ terraform {
|
||||
shared_credentials_files = ["~/.aws/credentials"]
|
||||
profile = "minio"
|
||||
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" {
|
||||
host = "database.localdomain"
|
||||
port = 5432
|
||||
database = var.postgres.db
|
||||
username = var.postgres.user
|
||||
password = var.postgres.secret
|
||||
host = local.connection[terraform.workspace].host
|
||||
port = local.connection[terraform.workspace].port
|
||||
database = local.connection[terraform.workspace].db
|
||||
username = local.connection[terraform.workspace].username
|
||||
password = local.connection[terraform.workspace].password
|
||||
sslmode = "disable"
|
||||
connect_timeout = 15
|
||||
}
|
||||
|
22
variables.tf
22
variables.tf
@ -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" {
|
||||
description = "postgres configuration"
|
||||
sensitive = true
|
||||
type = object({
|
||||
user = optional(string, "postgres")
|
||||
secret = string
|
||||
|
Loading…
x
Reference in New Issue
Block a user