Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Created March 25, 2016 09:44
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save sandcastle/4e7b979c480690044bd8 to your computer and use it in GitHub Desktop.
Save sandcastle/4e7b979c480690044bd8 to your computer and use it in GitHub Desktop.
Creates a AWS RDS Aurora Cluster with Terraform
########################
## 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}"
}
@PauloMigAlmeida
Copy link

@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:

# Declare the data source
data "aws_availability_zones" "available" {}

resource "aws_rds_cluster" "database" {
  cluster_identifier = "${lower("${var.tag_env}${var.tag_app}db")}"
  master_username    = "${data.aws_ssm_parameter.rds_username.value}"
  master_password    = "${data.aws_ssm_parameter.rds_password.value}"

  database_name                   = "${lower("${var.tag_env}${var.tag_app}db")}"
  backup_retention_period         = 35
  db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.default.name}"

  availability_zones = ["${data.aws_availability_zones.available.names}"]
.....

@sportebois
Copy link

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)}"]

@gladiatr72
Copy link

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.

@BeardedCloudWalker
Copy link

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

@kmangla9
Copy link

Hi, did we happen to resolve this "final_snapshot_identifier" error?

@dealyb
Copy link

dealyb commented Jun 29, 2020

just change the "_" to "-" so that they are hyphens which it will accept. ( for those who come later to this thread)

@mrtayyabpwc
Copy link

every variable must have a default value at least , but in this code there is no default value for any variable

@phatnguyentan
Copy link

I have to change to
subnet_ids = split(",", var.vpc_rds_subnet_ids)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment