Skip to content

Commit

Permalink
Remove subnet and parameter groups
Browse files Browse the repository at this point in the history
Instead of defining RDS subnet groups and parameter groups, emit the
necessary IDs so that the module consumer can specify their own
customized resources.
  • Loading branch information
Hector Castro committed Jul 25, 2016
1 parent acc0000 commit a288187
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 73 deletions.
24 changes: 16 additions & 8 deletions README.md
Expand Up @@ -9,12 +9,12 @@ module "postgresql_rds" {
source = "github.com/azavea/terraform-aws-postgresql-rds"

vpc_id = "vpc-20f74844"
vpc_cidr_block = "10.0.0.0/16"

allocated_storage = "32"
engine_version = "9.4.4"
instance_type = "db.t2.micro"
storage_type = "gp2"
database_identifier = "jl23kj32sdf"
database_name = "hector"
database_username = "hector"
database_password = "secret"
Expand All @@ -24,11 +24,15 @@ module "postgresql_rds" {
auto_minor_version_upgrade = false
multi_availability_zone = true
storage_encrypted = false
subnet_group = "${aws_db_subnet_group.default.name}"
parameter_group = "${aws_db_parameter_group.default.name}"

private_subnet_ids = "subnet-4a887f3c,subnet-76dae35d"
parameter_group_family = "postgres9.4"

alarm_cpu_threshold = 75
alarm_disk_queue_threshold = 10
alarm_free_disk_threshold = 5000000000
alarm_free_memory_threshold = 128000000
alarm_actions = "arn:aws:sns..."

project = "Something"
environment = "Staging"
}
Expand All @@ -39,11 +43,11 @@ module "postgresql_rds" {
- `project` - Name of project this VPC is meant to house (default: `Unknown`)
- `environment` - Name of environment this VPC is targeting (default: `Unknown`)
- `vpc_id` - ID of VPC meant to house database
- `vpc_cidr_block` - CIDR block of VPC
- `allocated_storage` - Storage allocated to database instance (default: `32`)
- `engine_version` - Database engine version (default: `9.4.4`)
- `instance_type` - Instance type for database instance (default: `db.t2.micro`)
- `storage_type` - Type of underlying storage for database (default: `gp2`)
- `database_identifier` - Identifier for RDS instance
- `database_name` - Name of database inside storage engine
- `database_username` - Name of user inside storage engine
- `database_password` - Database password inside storage engine
Expand All @@ -58,9 +62,13 @@ module "postgresql_rds" {
- `multi_availability_zone` - Flag to enable hot standby in another availability
zone (default: `false`)
- `storage_encrypted` - Flag to enable storage encryption (default: `false`)
- `private_subnet_ids` - Comma delimited list of private subnet IDs
- `parameter_group_family` - Database engine parameter group family (default:
`postgres9.4`)
- `subnet_group` - Database subnet group
- `parameter_group` - Database engine parameter group (default:
`default.postgres9.4`)
- `alarm_cpu_threshold` - CPU alarm threshold as a percentage (default: `75`)
- `alarm_disk_queue_threshold` - Disk queue alarm threshold (default: `10`)
- `alarm_free_disk_threshold` - Free disk alarm threshold in bytes (default: `5000000000`)
- `alarm_free_memory_threshold` - Free memory alarm threshold in bytes (default: `128000000`)
- `alarm_actions` - Comma delimited list of ARNs to be notified via CloudWatch

## Outputs
Expand Down
93 changes: 33 additions & 60 deletions main.tf
Expand Up @@ -17,66 +17,46 @@ resource "aws_security_group" "postgresql" {
#

resource "aws_db_instance" "postgresql" {
allocated_storage = "${var.allocated_storage}"
engine = "postgres"
engine_version = "${var.engine_version}"
identifier = "${var.database_name}"
instance_class = "${var.instance_type}"
storage_type = "${var.storage_type}"
name = "${var.database_name}"
password = "${var.database_password}"
username = "${var.database_username}"
backup_retention_period = "${var.backup_retention_period}"
backup_window = "${var.backup_window}"
maintenance_window = "${var.maintenance_window}"
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
multi_az = "${var.multi_availability_zone}"
port = "5432"
vpc_security_group_ids = ["${aws_security_group.postgresql.id}"]
db_subnet_group_name = "${aws_db_subnet_group.default.name}"
parameter_group_name = "${aws_db_parameter_group.default.name}"
storage_encrypted = "${var.storage_encrypted}"
allocated_storage = "${var.allocated_storage}"
engine = "postgres"
engine_version = "${var.engine_version}"
identifier = "${var.database_identifier}"
instance_class = "${var.instance_type}"
storage_type = "${var.storage_type}"
name = "${var.database_name}"
password = "${var.database_password}"
username = "${var.database_username}"
backup_retention_period = "${var.backup_retention_period}"
backup_window = "${var.backup_window}"
maintenance_window = "${var.maintenance_window}"
multi_az = "${var.multi_availability_zone}"
port = "5432"
vpc_security_group_ids = ["${aws_security_group.postgresql.id}"]
db_subnet_group_name = "${var.subnet_group}"
parameter_group_name = "${var.parameter_group}"
storage_encrypted = "${var.storage_encrypted}"

tags {
Name = "DatabaseServer"
}
}

resource "aws_db_subnet_group" "default" {
name = "${var.database_name}-subnet-group"
description = "Private subnets for the RDS instances"
subnet_ids = ["${split(",", var.private_subnet_ids)}"]

tags {
Name = "dbsngDatabaseServer"
}
}

resource "aws_db_parameter_group" "default" {
name = "${var.database_name}-parameter-group"
description = "Parameter group for the RDS instances"
family = "${var.parameter_group_family}"

parameter {
name = "log_min_duration_statement"
value = "500"
Name = "DatabaseServer"
Project = "${var.project}"
Environment = "${var.environment}"
}
}

#
# CloudWatch resources
#

resource "aws_cloudwatch_metric_alarm" "cpu" {
alarm_name = "alarmDatabaseServerCPUUtilization-${var.database_name}"
resource "aws_cloudwatch_metric_alarm" "database_cpu" {
alarm_name = "alarm${var.environment}DatabaseServerCPUUtilization"
alarm_description = "Database server CPU utilization"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = "300"
statistic = "Average"
threshold = "75"
threshold = "${var.alarm_cpu_threshold}"

dimensions {
DBInstanceIdentifier = "${aws_db_instance.postgresql.id}"
Expand All @@ -85,16 +65,16 @@ resource "aws_cloudwatch_metric_alarm" "cpu" {
alarm_actions = ["${split(",", var.alarm_actions)}"]
}

resource "aws_cloudwatch_metric_alarm" "disk_queue" {
alarm_name = "alarmDatabaseServerDiskQueueDepth-${var.database_name}"
resource "aws_cloudwatch_metric_alarm" "database_disk_queue" {
alarm_name = "alarm${var.environment}DatabaseServerDiskQueueDepth"
alarm_description = "Database server disk queue depth"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "DiskQueueDepth"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"
threshold = "10"
threshold = "${var.alarm_disk_queue_threshold}"

dimensions {
DBInstanceIdentifier = "${aws_db_instance.postgresql.id}"
Expand All @@ -103,18 +83,16 @@ resource "aws_cloudwatch_metric_alarm" "disk_queue" {
alarm_actions = ["${split(",", var.alarm_actions)}"]
}

resource "aws_cloudwatch_metric_alarm" "disk_free" {
alarm_name = "alarmDatabaseServerFreeStorageSpace-${var.database_name}"
resource "aws_cloudwatch_metric_alarm" "database_disk_free" {
alarm_name = "alarm${var.environment}DatabaseServerFreeStorageSpace"
alarm_description = "Database server free storage space"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"

# 5GB in bytes
threshold = "5000000000"
threshold = "${var.alarm_free_disk_threshold}"

dimensions {
DBInstanceIdentifier = "${aws_db_instance.postgresql.id}"
Expand All @@ -123,24 +101,19 @@ resource "aws_cloudwatch_metric_alarm" "disk_free" {
alarm_actions = ["${split(",", var.alarm_actions)}"]
}

resource "aws_cloudwatch_metric_alarm" "memory_free" {
alarm_name = "alarmDatabaseServerFreeableMemory-${var.database_name}"
resource "aws_cloudwatch_metric_alarm" "database_memory_free" {
alarm_name = "alarm${var.environment}DatabaseServerFreeableMemory"
alarm_description = "Database server freeable memory"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeableMemory"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"

# 128MB in bytes
threshold = "128000000"
threshold = "${var.alarm_free_memory_threshold}"

dimensions {
DBInstanceIdentifier = "${aws_db_instance.postgresql.id}"
Name = "DatabaseServer"
Project = "${var.project}"
Environment = "${var.environment}"
}

alarm_actions = ["${split(",", var.alarm_actions)}"]
Expand Down
28 changes: 23 additions & 5 deletions variables.tf
Expand Up @@ -8,8 +8,6 @@ variable "environment" {

variable "vpc_id" {}

variable "vpc_cidr_block" {}

variable "allocated_storage" {
default = "32"
}
Expand All @@ -26,6 +24,8 @@ variable "storage_type" {
default = "gp2"
}

variable "database_identifier" {}

variable "database_name" {}

variable "database_password" {}
Expand Down Expand Up @@ -58,10 +58,28 @@ variable "storage_encrypted" {
default = false
}

variable "private_subnet_ids" {}
variable "subnet_group" {}

variable "parameter_group" {
default = "default.postgres9.4"
}

variable "alarm_cpu_threshold" {
default = 75
}

variable "alarm_disk_queue_threshold" {
default = 10
}

variable "alarm_free_disk_threshold" {
# 5GB
default = 5000000000
}

variable "parameter_group_family" {
default = "postgres9.5"
variable "alarm_free_memory_threshold" {
# 128MB
default = 128000000
}

variable "alarm_actions" {}

0 comments on commit a288187

Please sign in to comment.