Skip to content

Commit

Permalink
Add support for Aurora Serverless V2 clusters (#23)
Browse files Browse the repository at this point in the history
* Add support for Aurora Serverless V2 clusters

* remove duplicated terraform docs section

* Update variables.tf

Co-authored-by: Max Williams <8859277+max-rocket-internet@users.noreply.github.com>

* Update variables.tf

Co-authored-by: Max Williams <8859277+max-rocket-internet@users.noreply.github.com>

* remove duplicated terraform docs section

---------

Co-authored-by: Max Williams <8859277+max-rocket-internet@users.noreply.github.com>
  • Loading branch information
xdrive and max-rocket-internet authored Jan 7, 2025
1 parent 47a1b21 commit 8825f3b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module "db" {
## Examples

- [PostgreSQL](examples/postgres): A simple example with VPC and PostgreSQL cluster.
- [PostgreSQL Serverless V2](examples/postgresql-serverlessv2): A simple example with VPC and PostgreSQL Serverless V2 cluster.
- [MySQL](examples/mysql): A simple example with VPC and MySQL cluster.
- [Production](examples/production): A production ready PostgreSQL cluster with enhanced monitoring, autoscaling and cloudwatch alarms.

Expand All @@ -60,13 +61,13 @@ terraform-docs md ./ | cat -s | perl -e "print reverse(<>)" | tail -n +2 | perl

| Name | Version |
|------|---------|
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.12.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.12.1 |
| <a name="provider_random"></a> [random](#provider\_random) | n/a |

## Modules
Expand Down Expand Up @@ -138,6 +139,7 @@ No modules.
| <a name="input_deletion_protection"></a> [deletion\_protection](#input\_deletion\_protection) | The database can't be deleted when this value is set to true. | `bool` | `true` | no |
| <a name="input_enabled_cloudwatch_logs_exports"></a> [enabled\_cloudwatch\_logs\_exports](#input\_enabled\_cloudwatch\_logs\_exports) | Set of log types to export to cloudwatch. If omitted, no logs will be exported. The following log types are supported: audit, error, general, slowquery, postgresql (PostgreSQL). | `list(any)` | `[]` | no |
| <a name="input_engine"></a> [engine](#input\_engine) | Aurora database engine type, currently aurora, aurora-mysql or aurora-postgresql | `string` | `"aurora"` | no |
| <a name="input_engine_mode"></a> [engine\_mode](#input\_engine\_mode) | Aurora database engine mode. | `string` | `"provisioned"` | no |
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | Aurora database engine version. | `string` | `"5.6.10a"` | no |
| <a name="input_extra_security_groups"></a> [extra\_security\_groups](#input\_extra\_security\_groups) | A list of Security Group IDs to add to the cluster | `list` | `[]` | no |
| <a name="input_final_snapshot_identifier_prefix"></a> [final\_snapshot\_identifier\_prefix](#input\_final\_snapshot\_identifier\_prefix) | The prefix name to use when creating a final snapshot on cluster destroy, appends a random 8 digits to name to ensure it's unique too. | `string` | `"final-"` | no |
Expand Down Expand Up @@ -168,6 +170,8 @@ No modules.
| <a name="input_route53_record_ttl"></a> [route53\_record\_ttl](#input\_route53\_record\_ttl) | TTL of route53 record. Only used if route53\_zone\_id is passed also | `string` | `60` | no |
| <a name="input_route53_zone_id"></a> [route53\_zone\_id](#input\_route53\_zone\_id) | If specified a route53 record will be created | `string` | `""` | no |
| <a name="input_security_group_name_prefix"></a> [security\_group\_name\_prefix](#input\_security\_group\_name\_prefix) | Prefix for security group name | `string` | `"aurora-"` | no |
| <a name="input_serverlessv2_max_capacity"></a> [serverlessv2\_max\_capacity](#input\_serverlessv2\_max\_capacity) | Maximum capacity for an Aurora DB cluster in provisioned(serverless v2) DB engine mode | `number` | `1` | no |
| <a name="input_serverlessv2_min_capacity"></a> [serverlessv2\_min\_capacity](#input\_serverlessv2\_min\_capacity) | Minimum capacity for an Aurora DB cluster in provisioned(serverless v2) DB engine mode | `number` | `0.5` | no |
| <a name="input_skip_final_snapshot"></a> [skip\_final\_snapshot](#input\_skip\_final\_snapshot) | Should a final snapshot be created on cluster destroy | `bool` | `false` | no |
| <a name="input_snapshot_identifier"></a> [snapshot\_identifier](#input\_snapshot\_identifier) | DB snapshot to create this database from | `string` | `""` | no |
| <a name="input_storage_encrypted"></a> [storage\_encrypted](#input\_storage\_encrypted) | Specifies whether the underlying storage layer should be encrypted | `bool` | `false` | no |
Expand Down
9 changes: 9 additions & 0 deletions examples/postgresql-serverlessv2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A simple example

This example will show the bare minimum parameters to create a Serverless V2 PostgreSQL Aurora cluster.

In general setup of the PostgreSQL serverless v2 cluster is very similar to creation of a regular PostgreSQL cluster.
The only crucial differences are the following:
* `engine_mode` needs to be specified and set to `provisioned`
* `instance_type` is to be set to `db.serverless`
* Scaling params are to be set in `serverlessv2_min_capacity` and `serverlessv2_max_capacity` params.
76 changes: 76 additions & 0 deletions examples/postgresql-serverlessv2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
provider "aws" {
region = "eu-west-1"
}

data "aws_availability_zones" "available" {}

module "aurora" {
source = "../../"
name = "aurora-example-postgresql"
engine = "aurora-postgresql"
engine_mode = "provisioned"
engine_version = "16.4"
subnet_ids = ["${module.vpc.database_subnets}"]
vpc_id = "${module.vpc.vpc_id}"
replica_count = 1
instance_type = "db.serverless"
apply_immediately = true
skip_final_snapshot = true
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_postgres164_parameter_group.id}"
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_postgres164_parameter_group.id}"
serverlessv2_min_capacity = 0.5
serverlessv2_max_capacity = 2
}

resource "aws_db_parameter_group" "aurora_db_postgres164_parameter_group" {
name = "test-aurora-db-postgres164-parameter-group"
family = "aurora-postgresql16.4"
description = "test-aurora-db-postgres164-parameter-group"
}

resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres164_parameter_group" {
name = "test-aurora-postgres164-cluster-parameter-group"
family = "aurora-postgresql16.4"
description = "test-aurora-postgres164-cluster-parameter-group"
}

resource "aws_security_group" "app_servers" {
name = "app-servers"
description = "For application servers"
vpc_id = "${module.vpc.vpc_id}"
}

resource "aws_security_group_rule" "allow_access" {
type = "ingress"
from_port = "${module.aurora.cluster_port}"
to_port = "${module.aurora.cluster_port}"
protocol = "tcp"
source_security_group_id = "${aws_security_group.app_servers.id}"
security_group_id = "${module.aurora.security_group_id}"
}

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "1.46.0"
name = "example-postgres"
cidr = "10.0.0.0/16"
azs = ["${data.aws_availability_zones.available.names}"]

private_subnets = [
"10.0.1.0/24",
"10.0.2.0/24",
"10.0.3.0/25",
]

public_subnets = [
"10.0.4.0/24",
"10.0.5.0/24",
"10.0.6.0/25",
]

database_subnets = [
"10.0.7.0/24",
"10.0.8.0/24",
"10.0.9.0/25",
]
}
9 changes: 9 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ resource "aws_rds_cluster" "main" {
cluster_identifier = "${var.identifier_prefix}${var.name}"
engine = var.engine
engine_version = var.engine_version
engine_mode = var.engine_mode
kms_key_id = var.kms_key_id
master_username = var.username
master_password = local.master_password
Expand All @@ -71,6 +72,14 @@ resource "aws_rds_cluster" "main" {
update = var.update_timeout
delete = var.delete_timeout
}

dynamic "serverlessv2_scaling_configuration" {
for_each = var.instance_type == "db.serverless" ? [1] : []
content {
max_capacity = var.serverlessv2_max_capacity
min_capacity = var.serverlessv2_min_capacity
}
}
}

resource "aws_rds_cluster_instance" "instance" {
Expand Down
17 changes: 17 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ variable "engine_version" {
default = "5.6.10a"
}

variable "engine_mode" {
description = "Aurora database engine mode."
default = "provisioned"
}

variable "replica_autoscaling" {
type = string
default = false
Expand Down Expand Up @@ -357,3 +362,15 @@ variable "cloudwatch_log_group_retention_in_days" {
type = number
default = 1
}

variable "serverlessv2_min_capacity" {
description = "Minimum capacity for an Aurora DB cluster in provisioned (serverless v2) DB engine mode"
type = number
default = 0.5
}

variable "serverlessv2_max_capacity" {
description = "Maximum capacity for an Aurora DB cluster in provisioned (serverless v2) DB engine mode"
type = number
default = 1
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.63.0"
version = ">= 4.12.1"
}
}
}

0 comments on commit 8825f3b

Please sign in to comment.