Initial stub of an RDS serverless instance in a can...

This commit is contained in:
Greyscale 2024-12-12 22:36:18 +01:00
parent 774a1c986b
commit 8fb858effc
Signed by: grey
GPG key ID: DDB392AE64B32D89
5 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,21 @@
variable "admin_user" {
type = object({
username = string
password = string
})
default = null
}
resource "random_pet" "admin_user" {
count = var.admin_user == null ? 1 : 0
separator = "-"
}
resource "random_password" "admin_user" {
length = 32
special = false
}
locals {
admin_user = var.admin_user != null ? var.admin_user : {
username = random_pet.admin_user.id
password = random_password.admin_user.result
}
}

View file

@ -0,0 +1,57 @@
variable "instance_name" {
type = string
description = "The name of the RDS serverless instance"
default = "serverless-multitennant"
}
variable "tennants" {
type = map(object({
username = string
password = string
database = string
}))
default = null
}
variable "application" {
description = "The AWS myApplication to be associated with this cluster"
type = object({
arn = string
name = string
description = string
application_tag = map(string)
})
default = null
}
variable "engine" {
type = string
description = "The database engine to use"
default = "aurora-mysql"
validation {
error_message = "Must be either aurora-mysql or aurora-postgresql"
condition = contains(["aurora-mysql", "aurora-postgresql"], var.engine)
}
}
variable "engine_version" {
type = string
default = "13.6"
}
variable "scaling" {
type = object({
max_capacity = optional(number, 0.5)
min_capacity = optional(number, 0)
seconds_until_auto_pause = optional(number, 3600)
})
validation {
error_message = "max_capacity must be greater or equal to min_capacity"
condition = var.scaling.max_capacity >= var.scaling.min_capacity
}
validation {
error_message = "min_capacity must be between 0 and 128 in steps of 0.5"
condition = var.scaling.min_capacity % 0.5 == 0 && var.scaling.min_capacity >= 0 && var.scaling.min_capacity <= 128
}
validation {
error_message = "max_capacity must be between 0 and 128 in steps of 0.5"
condition = var.scaling.max_capacity % 0.5 == 0 && var.scaling.max_capacity >= 0 && var.scaling.max_capacity <= 128
}
}

View file

@ -0,0 +1,20 @@
resource "aws_rds_cluster" "cluster" {
cluster_identifier = var.instance_name
engine = var.engine
engine_mode = "provisioned"
engine_version = var.engine_version
database_name = var.admin_user.username
master_username = var.admin_user.username
manage_master_user_password = true
storage_encrypted = true
serverlessv2_scaling_configuration {
max_capacity = var.scaling.max_capacity
min_capacity = var.scaling.min_capacity
seconds_until_auto_pause = var.scaling.seconds_until_auto_pause
}
tags = merge(
var.application.application_tag,
{}
)
}

View file

@ -0,0 +1,10 @@
resource "aws_rds_cluster_instance" "instance" {
cluster_identifier = aws_rds_cluster.cluster.id
instance_class = "db.serverless"
engine = aws_rds_cluster.cluster.engine
engine_version = aws_rds_cluster.cluster.engine_version
tags = merge(
var.application.application_tag,
{}
)
}

View file

@ -0,0 +1,18 @@
terraform {
required_version = "~> 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
random = {
source = "hashicorp/random"
version = "3.6.2"
}
local = {
source = "hashicorp/local"
version = "~>2.1"
}
}
}