diff --git a/cloud/aws/rds_serverless/admin_user.tf b/cloud/aws/rds_serverless/admin_user.tf new file mode 100644 index 0000000..6fbf397 --- /dev/null +++ b/cloud/aws/rds_serverless/admin_user.tf @@ -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 + } +} \ No newline at end of file diff --git a/cloud/aws/rds_serverless/inputs.tf b/cloud/aws/rds_serverless/inputs.tf new file mode 100644 index 0000000..ffdadfe --- /dev/null +++ b/cloud/aws/rds_serverless/inputs.tf @@ -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 + } +} \ No newline at end of file diff --git a/cloud/aws/rds_serverless/rds.cluster.tf b/cloud/aws/rds_serverless/rds.cluster.tf new file mode 100644 index 0000000..5da538b --- /dev/null +++ b/cloud/aws/rds_serverless/rds.cluster.tf @@ -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, + {} + ) +} diff --git a/cloud/aws/rds_serverless/rds.instance.tf b/cloud/aws/rds_serverless/rds.instance.tf new file mode 100644 index 0000000..a59839e --- /dev/null +++ b/cloud/aws/rds_serverless/rds.instance.tf @@ -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, + {} + ) +} \ No newline at end of file diff --git a/cloud/aws/rds_serverless/terraform.tf b/cloud/aws/rds_serverless/terraform.tf new file mode 100644 index 0000000..850b974 --- /dev/null +++ b/cloud/aws/rds_serverless/terraform.tf @@ -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" + } + } +}