-
-
Save sandcastle/4e7b979c480690044bd8 to your computer and use it in GitHub Desktop.
######################## | |
## Variables | |
######################## | |
variable "environment_name" { | |
description = "The name of the environment" | |
} | |
variable "vpc_id" { | |
description = "The ID of the VPC that the RDS cluster will be created in" | |
} | |
variable "vpc_name" { | |
description = "The name of the VPC that the RDS cluster will be created in" | |
} | |
variable "vpc_rds_subnet_ids" { | |
description = "The ID's of the VPC subnets that the RDS cluster instances will be created in" | |
} | |
variable "vpc_rds_security_group_id" { | |
description = "The ID of the security group that should be used for the RDS cluster instances" | |
} | |
variable "rds_master_username" { | |
description = "The ID's of the VPC subnets that the RDS cluster instances will be created in" | |
} | |
variable "rds_master_password" { | |
description = "The ID's of the VPC subnets that the RDS cluster instances will be created in" | |
} | |
######################## | |
## Cluster | |
######################## | |
resource "aws_rds_cluster" "aurora_cluster" { | |
cluster_identifier = "${var.environment_name}_aurora_cluster" | |
database_name = "mydb" | |
master_username = "${var.rds_master_username}" | |
master_password = "${var.rds_master_password}" | |
backup_retention_period = 14 | |
preferred_backup_window = "02:00-03:00" | |
preferred_maintenance_window = "wed:03:00-wed:04:00" | |
db_subnet_group_name = "${aws_db_subnet_group.aurora_subnet_group.name}" | |
final_snapshot_identifier = "${var.environment_name}_aurora_cluster" | |
vpc_security_group_ids = [ | |
"${var.vpc_rds_security_group_id}" | |
] | |
tags { | |
Name = "${var.environment_name}-Aurora-DB-Cluster" | |
VPC = "${var.vpc_name}" | |
ManagedBy = "terraform" | |
Environment = "${var.environment_name}" | |
} | |
lifecycle { | |
create_before_destroy = true | |
} | |
} | |
resource "aws_rds_cluster_instance" "aurora_cluster_instance" { | |
count = "${length(split(",", var.vpc_rds_subnet_ids))}" | |
identifier = "${var.environment_name}_aurora_instance_${count.index}" | |
cluster_identifier = "${aws_rds_cluster.aurora_cluster.id}" | |
instance_class = "db.t2.small" | |
db_subnet_group_name = "${aws_db_subnet_group.aurora_subnet_group.name}" | |
publicly_accessible = true | |
tags { | |
Name = "${var.environment_name}-Aurora-DB-Instance-${count.index}" | |
VPC = "${var.vpc_name}" | |
ManagedBy = "terraform" | |
Environment = "${var.environment_name}" | |
} | |
lifecycle { | |
create_before_destroy = true | |
} | |
} | |
resource "aws_db_subnet_group" "aurora_subnet_group" { | |
name = "${var.environment_name}_aurora_db_subnet_group" | |
description = "Allowed subnets for Aurora DB cluster instances" | |
subnet_ids = [ | |
"${split(",", var.vpc_rds_subnet_ids)}" | |
] | |
tags { | |
Name = "${var.environment_name}-Aurora-DB-Subnet-Group" | |
VPC = "${var.vpc_name}" | |
ManagedBy = "terraform" | |
Environment = "${var.environment_name}" | |
} | |
} | |
######################## | |
## Output | |
######################## | |
output "cluster_address" { | |
value = "${aws_rds_cluster.aurora_cluster.address}" | |
} |
One caveat with the AZ data is that Aurora accepts up to 3 AZs, and you'd get an error if you throw more AZ in that list.
If you don't care about which one you'd want (ideally the same than your EC2 instances, but if you always start with the first ones you'll be fine), then it gives this:
availability_zones = ["${slice(data.aws_availability_zones.available.names, 0, 2)}"]
There is also the issue that there is now way to determine from the api-provided az list whether or not its members are actually alive.
small update:
Error: aws_rds_cluster.aurora_cluster: only alphanumeric characters and hyphens allowed in "final_snapshot_identifier" Error: aws_rds_cluster.aurora_cluster: only lowercase alphanumeric characters and hyphens allowed in "cluster_identifier" Error: aws_rds_cluster_instance.aurora_cluster_instance: only lowercase alphanumeric characters and hyphens allowed in "identifier"
may want to change the underscores in the values to hyphens
Hi, did we happen to resolve this "final_snapshot_identifier" error?
just change the "_" to "-" so that they are hyphens which it will accept. ( for those who come later to this thread)
every variable must have a default value at least , but in this code there is no default value for any variable
I have to change to
subnet_ids = split(",", var.vpc_rds_subnet_ids)
@sandcastle, thanks for sharing this gist with the community. 👍
Just a little suggestion, Terraform provides you with a way to list the current availability zones within the region you're accessing. Having said that, you could set the
availability_zones
with that values, so resources would be created in different zones instead of a single one. (current behaviour)This would look like something akin to it: