Created
July 31, 2022 17:15
-
-
Save nagyv/30f9e92dd2fb2abddc05998f5fdec417 to your computer and use it in GitHub Desktop.
Postgres DB module
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
provider "postgresql" { | |
host = var.server_host | |
port = var.server_port | |
database = "postgres" | |
username = var.username | |
password = var.password | |
sslmode = var.sslmode | |
connect_timeout = 15 | |
superuser = false | |
} | |
module "db_label" { | |
source = "cloudposse/label/null" | |
# Cloud Posse recommends pinning every module to a specific version | |
# version = "x.x.x" | |
namespace = "crossplane" | |
name = var.database_name | |
attributes = [] | |
delimiter = "-" | |
} | |
resource "random_string" "username" { | |
length = 16 | |
special = false | |
} | |
resource "random_password" "password" { | |
length = 16 | |
special = true | |
override_special = "!#$%&*()-_=+[]{}<>:?" | |
} | |
resource "postgresql_role" "this" { | |
name = random_string.username.result | |
login = true | |
password = random_password.password.result | |
} | |
resource "postgresql_database" "this" { | |
name = module.db_label.id | |
owner = random_string.username.result | |
depends_on = [ | |
postgresql_role.this | |
] | |
} |
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
output "DB_HOST" { | |
value = var.server_host | |
} | |
output "DB_PORT" { | |
value = var.server_port | |
} | |
output "DB_NAME" { | |
value = module.db_label.id | |
} | |
output "DB_USER" { | |
value = random_string.username.result | |
} | |
output "DB_PASSWORD" { | |
value = random_password.password.result | |
sensitive = 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
variable "server_host" { | |
type = string | |
description = "Postgres DB server host" | |
} | |
variable "server_port" { | |
type = number | |
description = "Postgres DB server port" | |
} | |
variable "username" { | |
type = string | |
description = "Postgres DB root username" | |
} | |
variable "password" { | |
type = string | |
description = "Postgres DB root password" | |
} | |
variable "sslmode" { | |
type = string | |
default = "disable" | |
description = "Is SSL mode required to connect" | |
} | |
variable "database_name" { | |
type = string | |
description = "The name of the database to create" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment