Skip to content

Commit

Permalink
upgraded to terraform 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
marciogoda committed Jul 30, 2021
1 parent 0fa55dc commit 51db222
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 148 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.terraform
.cache
61 changes: 31 additions & 30 deletions alarms.tf
Original file line number Diff line number Diff line change
@@ -1,99 +1,100 @@
resource "aws_cloudwatch_metric_alarm" "alarm_rds_DatabaseConnections_writer" {
count = "${var.cw_alarms ? 1 : 0}"
count = var.cw_alarms ? 1 : 0
alarm_name = "${aws_rds_cluster.default.id}-alarm-rds-writer-DatabaseConnections"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "DatabaseConnections"
namespace = "AWS/RDS"
period = "60"
statistic = "Sum"
threshold = "${var.cw_max_conns}"
threshold = var.cw_max_conns
alarm_description = "RDS Maximum connection Alarm for ${aws_rds_cluster.default.id} writer"
alarm_actions = ["${var.cw_sns_topic}"]
ok_actions = ["${var.cw_sns_topic}"]
alarm_actions = [var.cw_sns_topic]
ok_actions = [var.cw_sns_topic]

dimensions {
DBClusterIdentifier = "${aws_rds_cluster.default.id}"
dimensions = {
DBClusterIdentifier = aws_rds_cluster.default.id
Role = "WRITER"
}
}

resource "aws_cloudwatch_metric_alarm" "alarm_rds_DatabaseConnections_reader" {
count = "${var.cw_alarms && var.replica_count > 0 ? 1 : 0}"
count = var.cw_alarms && var.replica_count > 0 ? 1 : 0
alarm_name = "${aws_rds_cluster.default.id}-alarm-rds-reader-DatabaseConnections"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "DatabaseConnections"
namespace = "AWS/RDS"
period = "60"
statistic = "Maximum"
threshold = "${var.cw_max_conns}"
threshold = var.cw_max_conns
alarm_description = "RDS Maximum connection Alarm for ${aws_rds_cluster.default.id} reader(s)"
alarm_actions = ["${var.cw_sns_topic}"]
ok_actions = ["${var.cw_sns_topic}"]
alarm_actions = [var.cw_sns_topic]
ok_actions = [var.cw_sns_topic]

dimensions {
DBClusterIdentifier = "${aws_rds_cluster.default.id}"
dimensions = {
DBClusterIdentifier = aws_rds_cluster.default.id
Role = "READER"
}
}

resource "aws_cloudwatch_metric_alarm" "alarm_rds_CPU_writer" {
count = "${var.cw_alarms ? 1 : 0}"
count = var.cw_alarms ? 1 : 0
alarm_name = "${aws_rds_cluster.default.id}-alarm-rds-writer-CPU"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = "60"
statistic = "Maximum"
threshold = "${var.cw_max_cpu}"
threshold = var.cw_max_cpu
alarm_description = "RDS CPU Alarm for ${aws_rds_cluster.default.id} writer"
alarm_actions = ["${var.cw_sns_topic}"]
ok_actions = ["${var.cw_sns_topic}"]
alarm_actions = [var.cw_sns_topic]
ok_actions = [var.cw_sns_topic]

dimensions {
DBClusterIdentifier = "${aws_rds_cluster.default.id}"
dimensions = {
DBClusterIdentifier = aws_rds_cluster.default.id
Role = "WRITER"
}
}

resource "aws_cloudwatch_metric_alarm" "alarm_rds_CPU_reader" {
count = "${var.cw_alarms && var.replica_count > 0 ? 1 : 0}"
count = var.cw_alarms && var.replica_count > 0 ? 1 : 0
alarm_name = "${aws_rds_cluster.default.id}-alarm-rds-reader-CPU"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = "60"
statistic = "Maximum"
threshold = "${var.cw_max_cpu}"
threshold = var.cw_max_cpu
alarm_description = "RDS CPU Alarm for ${aws_rds_cluster.default.id} reader(s)"
alarm_actions = ["${var.cw_sns_topic}"]
ok_actions = ["${var.cw_sns_topic}"]
alarm_actions = [var.cw_sns_topic]
ok_actions = [var.cw_sns_topic]

dimensions {
DBClusterIdentifier = "${aws_rds_cluster.default.id}"
dimensions = {
DBClusterIdentifier = aws_rds_cluster.default.id
Role = "READER"
}
}

resource "aws_cloudwatch_metric_alarm" "alarm_rds_replica_lag" {
count = "${var.cw_alarms && var.replica_count > 0 ? 1 : 0}"
count = var.cw_alarms && var.replica_count > 0 ? 1 : 0
alarm_name = "${aws_rds_cluster.default.id}-alarm-rds-reader-AuroraReplicaLag"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "5"
metric_name = "AuroraReplicaLag"
namespace = "AWS/RDS"
period = "60"
statistic = "Maximum"
threshold = "${var.cw_max_replica_lag}"
threshold = var.cw_max_replica_lag
alarm_description = "RDS CPU Alarm for ${aws_rds_cluster.default.id}"
alarm_actions = ["${var.cw_sns_topic}"]
ok_actions = ["${var.cw_sns_topic}"]
alarm_actions = [var.cw_sns_topic]
ok_actions = [var.cw_sns_topic]

dimensions {
DBClusterIdentifier = "${aws_rds_cluster.default.id}"
dimensions = {
DBClusterIdentifier = aws_rds_cluster.default.id
Role = "READER"
}
}

147 changes: 74 additions & 73 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -162,94 +162,94 @@

// DB Subnet Group creation
resource "aws_db_subnet_group" "main" {
name = "${var.name}"
name = var.name
description = "Group of DB subnets"
subnet_ids = ["${var.subnets}"]
subnet_ids = var.subnets

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
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)}"
cluster_identifier = "${aws_rds_cluster.default.id}"
engine = "${var.engine}"
engine_version = "${var.engine-version}"
instance_class = "${var.instance_type}"
publicly_accessible = "${var.publicly_accessible}"
db_subnet_group_name = "${aws_db_subnet_group.main.name}"
db_parameter_group_name = "${var.db_parameter_group_name}"
preferred_maintenance_window = "${var.preferred_maintenance_window}"
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}"
identifier = var.identifier_prefix != "" ? format("%s-node-0", var.identifier_prefix) : format("%s-aurora-node-0", var.envname)
cluster_identifier = aws_rds_cluster.default.id
engine = var.engine
engine_version = var.engine-version
instance_class = var.instance_type
publicly_accessible = var.publicly_accessible
db_subnet_group_name = aws_db_subnet_group.main.name
db_parameter_group_name = var.db_parameter_group_name
preferred_maintenance_window = var.preferred_maintenance_window
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
promotion_tier = "0"
performance_insights_enabled = "${var.performance_insights_enabled}"
performance_insights_enabled = var.performance_insights_enabled

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

// Create 'n' number of additional DB instance(s) in same cluster
resource "aws_rds_cluster_instance" "cluster_instance_n" {
depends_on = ["aws_rds_cluster_instance.cluster_instance_0"]
count = "${var.replica_scale_enabled ? var.replica_scale_min : var.replica_count}"
engine = "${var.engine}"
engine_version = "${var.engine-version}"
identifier = "${var.identifier_prefix != "" ? format("%s-node-%d", var.identifier_prefix, count.index + 1) : format("%s-aurora-node-%d", var.envname, count.index + 1)}"
cluster_identifier = "${aws_rds_cluster.default.id}"
instance_class = "${var.instance_type}"
publicly_accessible = "${var.publicly_accessible}"
db_subnet_group_name = "${aws_db_subnet_group.main.name}"
db_parameter_group_name = "${var.db_parameter_group_name}"
preferred_maintenance_window = "${var.preferred_maintenance_window}"
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}"
promotion_tier = "${count.index + 1}"
performance_insights_enabled = "${var.performance_insights_enabled}"
depends_on = [aws_rds_cluster_instance.cluster_instance_0]
count = var.replica_scale_enabled ? var.replica_scale_min : var.replica_count
engine = var.engine
engine_version = var.engine-version
identifier = var.identifier_prefix != "" ? format("%s-node-%d", var.identifier_prefix, count.index + 1) : format("%s-aurora-node-%d", var.envname, count.index + 1)
cluster_identifier = aws_rds_cluster.default.id
instance_class = var.instance_type
publicly_accessible = var.publicly_accessible
db_subnet_group_name = aws_db_subnet_group.main.name
db_parameter_group_name = var.db_parameter_group_name
preferred_maintenance_window = var.preferred_maintenance_window
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
promotion_tier = count.index + 1
performance_insights_enabled = var.performance_insights_enabled

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
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)}"
availability_zones = ["${var.azs}"]
engine = "${var.engine}"
cluster_identifier = var.identifier_prefix != "" ? format("%s-cluster", var.identifier_prefix) : format("%s-aurora-cluster", var.envname)
availability_zones = var.azs
engine = var.engine

engine_version = "${var.engine-version}"
master_username = "${var.username}"
master_password = "${var.password}"
engine_version = var.engine-version
master_username = var.username
master_password = var.password
final_snapshot_identifier = "${var.final_snapshot_identifier}-${random_id.server.hex}"
skip_final_snapshot = "${var.skip_final_snapshot}"
backup_retention_period = "${var.backup_retention_period}"
preferred_backup_window = "${var.preferred_backup_window}"
preferred_maintenance_window = "${var.preferred_maintenance_window}"
port = "${var.port}"
db_subnet_group_name = "${aws_db_subnet_group.main.name}"
vpc_security_group_ids = ["${var.security_groups}"]
snapshot_identifier = "${var.snapshot_identifier}"
storage_encrypted = "${var.storage_encrypted}"
apply_immediately = "${var.apply_immediately}"
db_cluster_parameter_group_name = "${var.db_cluster_parameter_group_name}"
deletion_protection = "${var.deletion_protection}"
skip_final_snapshot = var.skip_final_snapshot
backup_retention_period = var.backup_retention_period
preferred_backup_window = var.preferred_backup_window
preferred_maintenance_window = var.preferred_maintenance_window
port = var.port
db_subnet_group_name = aws_db_subnet_group.main.name
vpc_security_group_ids = var.security_groups
snapshot_identifier = var.snapshot_identifier
storage_encrypted = var.storage_encrypted
apply_immediately = var.apply_immediately
db_cluster_parameter_group_name = var.db_cluster_parameter_group_name
deletion_protection = var.deletion_protection
}

// Geneate an ID when an environment is initialised
resource "random_id" "server" {
keepers = {
id = "${aws_db_subnet_group.main.name}"
id = aws_db_subnet_group.main.name
}

byte_length = 8
Expand All @@ -268,30 +268,30 @@ data "aws_iam_policy_document" "monitoring-rds-assume-role-policy" {
}

resource "aws_iam_role" "rds-enhanced-monitoring" {
count = "${var.monitoring_interval > 0 ? 1 : 0}"
count = var.monitoring_interval > 0 ? 1 : 0
name_prefix = "rds-enhanced-mon-${var.envname}-"
assume_role_policy = "${data.aws_iam_policy_document.monitoring-rds-assume-role-policy.json}"
assume_role_policy = data.aws_iam_policy_document.monitoring-rds-assume-role-policy.json
}

resource "aws_iam_role_policy_attachment" "rds-enhanced-monitoring-policy-attach" {
count = "${var.monitoring_interval > 0 ? 1 : 0}"
role = "${aws_iam_role.rds-enhanced-monitoring.name}"
count = var.monitoring_interval > 0 ? 1 : 0
role = aws_iam_role.rds-enhanced-monitoring[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}

// Autoscaling
resource "aws_appautoscaling_target" "autoscaling" {
count = "${var.replica_scale_enabled ? 1 : 0}"
max_capacity = "${var.replica_scale_max}"
min_capacity = "${var.replica_scale_min}"
count = var.replica_scale_enabled ? 1 : 0
max_capacity = var.replica_scale_max
min_capacity = var.replica_scale_min
resource_id = "cluster:${aws_rds_cluster.default.cluster_identifier}"
scalable_dimension = "rds:cluster:ReadReplicaCount"
service_namespace = "rds"
}

resource "aws_appautoscaling_policy" "autoscaling" {
count = "${var.replica_scale_enabled ? 1 : 0}"
depends_on = ["aws_appautoscaling_target.autoscaling"]
count = var.replica_scale_enabled ? 1 : 0
depends_on = [aws_appautoscaling_target.autoscaling]
name = "target-metric"
policy_type = "TargetTrackingScaling"
resource_id = "cluster:${aws_rds_cluster.default.cluster_identifier}"
Expand All @@ -303,8 +303,9 @@ resource "aws_appautoscaling_policy" "autoscaling" {
predefined_metric_type = "RDSReaderAverageCPUUtilization"
}

scale_in_cooldown = "${var.replica_scale_in_cooldown}"
scale_out_cooldown = "${var.replica_scale_out_cooldown}"
target_value = "${var.replica_scale_cpu}"
scale_in_cooldown = var.replica_scale_in_cooldown
scale_out_cooldown = var.replica_scale_out_cooldown
target_value = var.replica_scale_cpu
}
}

13 changes: 7 additions & 6 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// The 'writer' endpoint for the cluster
output "cluster_endpoint" {
value = "${aws_rds_cluster.default.endpoint}"
value = aws_rds_cluster.default.endpoint
}

// Comma separated list of all DB instance endpoints running in cluster
output "all_instance_endpoints_list" {
value = ["${aws_rds_cluster_instance.cluster_instance_0.endpoint}", "${aws_rds_cluster_instance.cluster_instance_n.*.endpoint}"]
value = [aws_rds_cluster_instance.cluster_instance_0.endpoint, aws_rds_cluster_instance.cluster_instance_n.*.endpoint]
}

// A read-only endpoint for the Aurora cluster, automatically load-balanced across replicas
output "reader_endpoint" {
value = "${aws_rds_cluster.default.reader_endpoint}"
value = aws_rds_cluster.default.reader_endpoint
}

// Cluster ARN - can use when defining centralised backup
output "cluster_arn" {
value = "${aws_rds_cluster.default.arn}"
value = aws_rds_cluster.default.arn
}

// Cluster ID - useful to any resources requiring cluster's ID, e.g. rds_cluster_role_association
output "cluster_id" {
value = "${aws_rds_cluster.default.id}"
}
value = aws_rds_cluster.default.id
}

Loading

0 comments on commit 51db222

Please sign in to comment.