Cale Cale - 2 months ago 52
MySQL Question

Creating a route53 entry for RDS using Terraform

I am attempting to create a Route53 entry for a MySQL RDS entry but having issues with the :3306 at the end of the RDS-ENDPOINT from terraform.

resource "aws_db_instance" "mydb" {
allocated_storage = 10
engine = "mysql"
engine_version = "5.6.17"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "bar"
db_subnet_group_name = "my_database_subnet_group"
parameter_group_name = "default.mysql5.6"
}

resource "aws_route53_record" "database" {
zone_id = "${aws_route53_zone.primary.zone_id}"
name = "database.example.com"
type = "CNAME"
ttl = "300"
records = ["${aws_db_instance.default.endpoint}"]
}


terraform puts a :3306 at the end of the endpoint and that gets entered into the route53 Value of the CNAME

so when I try to connect to cname database.example.com with the mysql client I get:

ERROR 2005 (HY000): Unknown MySQL server host 'database.example.com' (0)


once I remove the :3306 via the AWS route53 console It seems work just fine.
Question is: How Do I strip the :3306 from the terraform rds endpoint

Answer

As well as an endpoint output, Terraform's aws_db_instance resource also outputs address that provides the FQDN of the instance.

So all you need to do is change your aws_route53_record resource to use address instead:

resource "aws_db_instance" "mydb" {
  allocated_storage    = 10
  engine               = "mysql"
  engine_version       = "5.6.17"
  instance_class       = "db.t2.micro"
  name                 = "mydb"
  username             = "foo"
  password             = "bar"
  db_subnet_group_name = "my_database_subnet_group"
  parameter_group_name = "default.mysql5.6"
}

resource "aws_route53_record" "database" {
  zone_id = "${aws_route53_zone.primary.zone_id}"
  name = "database.example.com"
  type = "CNAME"
  ttl = "300"
  records = ["${aws_db_instance.mydb.address}"]
}