Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Gives you:
- A DB subnet group
- An Aurora DB cluster
- An Aurora DB instance + 'n' number of additional instances
- Optionally cross-region DB subnet group
- Optionally cross-region Aurora DB read replica instance
- Optionally RDS 'Enhanced Monitoring' + associated required IAM role/policy (by simply setting the `monitoring_interval` param to > `0`
- Optionally sensible alarms to SNS (high CPU, high connections, slow replication)
- Optionally configure autoscaling for read replicas
Expand Down Expand Up @@ -198,11 +200,15 @@ resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres96_parameter_
| replica_scale_max | Maximum number of replicas to allow scaling for | string | `0` | no |
| replica_scale_min | Maximum number of replicas to allow scaling for | string | `2` | no |
| replica_scale_out_cooldown | Cooldown in seconds before allowing further scaling operations after a scale out | string | `300` | no |
| replica_region_enabled | Whether to create a cross region read replica instance | string | `false` | no |
| db_region_parameter_group_name | The name of a DB parameter group to use in a different region | string | `default.aurora5.6` | no |
| replicate_source_db | DB identifier of another Amazon RDS Database to replicate | string | `` | no |
| security_groups | VPC Security Group IDs | list | - | yes |
| skip_final_snapshot | Should a final snapshot be created on cluster destroy | string | `false` | no |
| snapshot_identifier | DB snapshot to create this database from | string | `` | no |
| storage_encrypted | Specifies whether the underlying storage layer should be encrypted | string | `true` | no |
| subnets | List of subnet IDs to use | list | - | yes |
| subnets_region | List of subnet IDs to use in a different region | list | - | yes |
| username | Master DB username | string | `root` | no |

## Outputs
Expand All @@ -212,4 +218,3 @@ resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres96_parameter_
| all_instance_endpoints_list | Comma separated list of all DB instance endpoints running in cluster |
| cluster_endpoint | The 'writer' endpoint for the cluster |
| reader_endpoint | A read-only endpoint for the Aurora cluster, automatically load-balanced across replicas |

56 changes: 48 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* - A DB subnet group
* - An Aurora DB cluster
* - An Aurora DB instance + 'n' number of additional instances
* - Optionally cross-region DB subnet group
* - Optionally cross-region Aurora DB read replica instance
* - Optionally RDS 'Enhanced Monitoring' + associated required IAM role/policy (by simply setting the `monitoring_interval` param to > `0`
* - Optionally sensible alarms to SNS (high CPU, high connections, slow replication)
* - Optionally configure autoscaling for read replicas (MySQL clusters only)
Expand All @@ -30,11 +32,11 @@
*
* ### Aurora 1.x (MySQL 5.6)
*
*
*
* resource "aws_sns_topic" "db_alarms_56" {
* name = "aurora-db-alarms-56"
* }
*
*
* module "aurora_db_56" {
* source = "../.."
* name = "test-aurora-db-56"
Expand All @@ -57,13 +59,13 @@
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_56_parameter_group.id}"
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_56_parameter_group.id}"
* }
*
*
* resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
* name = "test-aurora-db-56-parameter-group"
* family = "aurora5.6"
* description = "test-aurora-db-56-parameter-group"
* }
*
*
* resource "aws_rds_cluster_parameter_group" "aurora_cluster_56_parameter_group" {
* name = "test-aurora-56-cluster-parameter-group"
* family = "aurora5.6"
Expand All @@ -76,7 +78,7 @@
* resource "aws_sns_topic" "db_alarms" {
* name = "aurora-db-alarms"
* }
*
*
* module "aurora_db_57" {
* source = "../.."
* engine = "aurora-mysql"
Expand All @@ -101,13 +103,13 @@
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_57_parameter_group.id}"
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_57_cluster_parameter_group.id}"
* }
*
*
* resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
* name = "test-aurora-db-57-parameter-group"
* family = "aurora-mysql5.7"
* description = "test-aurora-db-57-parameter-group"
* }
*
*
* resource "aws_rds_cluster_parameter_group" "aurora_57_cluster_parameter_group" {
* name = "test-aurora-57-cluster-parameter-group"
* family = "aurora-mysql5.7"
Expand Down Expand Up @@ -172,6 +174,19 @@ resource "aws_db_subnet_group" "main" {
}
}

// DB Subnet Cross Region Group creation
resource "aws_db_subnet_group" "region" {
count = "${var.replica_region_enabled ? 1 : 0}"
name = "${var.name}-region"
description = "Group of DB subnets in this region"
subnet_ids = ["${var.subnets_region}"]

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
}
}

// Create single DB instance
resource "aws_rds_cluster_instance" "cluster_instance_0" {
identifier = "${var.identifier_prefix != "" ? format("%s-node-0", var.identifier_prefix) : format("%s-aurora-node-0", var.envname)}"
Expand Down Expand Up @@ -222,6 +237,31 @@ resource "aws_rds_cluster_instance" "cluster_instance_n" {
}
}

// Create cross region replica instance
resource "aws_db_instance" "replica_region_0" {
depends_on = ["aws_rds_cluster_instance.cluster_instance_0"]
count = "${var.replica_region_enabled ? 1 : 0}"
engine = "${var.engine}"
engine_version = "${var.engine-version}"
identifier = "${var.identifier_prefix != "" ? format("%s-replica-0", var.identifier_prefix) : format("%s-aurora-replica-0", var.envname)}"
instance_class = "${var.instance_type}"
publicly_accessible = "${var.publicly_accessible}"
db_subnet_group_name = "${aws_db_subnet_group.region.name}"
parameter_group_name = "${var.db_region_parameter_group_name}"
apply_immediately = "${var.apply_immediately}"
monitoring_role_arn = "${join("", aws_iam_role.rds-enhanced-monitoring.*.arn)}"
monitoring_interval = "${var.monitoring_interval}"
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
performance_insights_enabled = "${var.performance_insights_enabled}"
replicate_source_db = "${var.replicate_source_db}"

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
}
}


// Create DB Cluster
resource "aws_rds_cluster" "default" {
cluster_identifier = "${var.identifier_prefix != "" ? format("%s-cluster", var.identifier_prefix) : format("%s-aurora-cluster", var.envname)}"
Expand All @@ -245,7 +285,7 @@ resource "aws_rds_cluster" "default" {
db_cluster_parameter_group_name = "${var.db_cluster_parameter_group_name}"
}

// Geneate an ID when an environment is initialised
// Generate an ID when an environment is initialised
resource "random_id" "server" {
keepers = {
id = "${aws_db_subnet_group.main.name}"
Expand Down
23 changes: 23 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ variable "subnets" {
description = "List of subnet IDs to use"
}

variable "subnets_region" {
type = "list"
description = "List of subnet IDs to use in a different region"
}

variable "envname" {
type = "string"
description = "Environment name (eg,test, stage or prod)"
Expand Down Expand Up @@ -122,6 +127,12 @@ variable "db_parameter_group_name" {
description = "The name of a DB parameter group to use"
}

variable "db_region_parameter_group_name" {
type = "string"
default = "default.aurora5.6"
description = "The name of a DB parameter group to use in a different region"
}

variable "db_cluster_parameter_group_name" {
type = "string"
default = "default.aurora5.6"
Expand Down Expand Up @@ -182,6 +193,18 @@ variable "engine-version" {
description = "Aurora database engine version."
}

variable "replica_region_enabled" {
type = "string"
default = false
description = "Whether to create a cross region read replica instance"
}

variable "replicate_source_db" {
type = "string"
default = ""
description = "DB identifier of another Amazon RDS Database to replicate"
}

variable "replica_scale_enabled" {
type = "string"
default = false
Expand Down