Skip to content

Instantly share code, notes, and snippets.

@nagyv
Created July 31, 2022 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nagyv/30f9e92dd2fb2abddc05998f5fdec417 to your computer and use it in GitHub Desktop.
Save nagyv/30f9e92dd2fb2abddc05998f5fdec417 to your computer and use it in GitHub Desktop.
Postgres DB module
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
]
}
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
}
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