terraformterraform-provider-awsamazon-auroraterraform0.12+terraform-modules

Terraform Error: only lowercase alphanumeric characters and hyphens allowed in "cluster_identifier" rds_cluster


The detailed error is:

Error: only lowercase alphanumeric characters and hyphens allowed in "cluster_identifier"

  on ../rds_cluster/main.tf line 6, in resource "aws_rds_cluster" "mysql-cluster":
   6:   cluster_identifier = var.identifier

I get the above error while creating a rds_cluster from terraform. My main.tf file has the below code:

provider "aws"{
  region = var.region
}

resource "aws_rds_cluster" "mysql-cluster" {
  cluster_identifier = var.identifier
  availability_zones = var.azs
  database_name      = var.db_name
  master_username    = var.username
  master_password    = var.password
}

The variable.tf file is:

variable "region"{
 default = "us-east-1"
}

variable "identifier" {
  default = "aurora-cluster"
}

variable "db_name" {
  default = "mydb"
}

variable "username" {}

variable "password" {}

variable "azs" {}

And the module file that is calling this "rds_cluster" is:

provider "aws"{
  region = "us-east-1"
}

data "aws_availability_zones" "azs" {

}

data "aws_secretsmanager_secret_version" "creds" {
  secret_id = "db-creds"
}

module "mysql_aurora_instances" {
  source               =  "../rds_instance/"
  my_count             =  2
  identifier           =  "aurora-cluster"
  cluster_identifier   =  "mysql_aurora_cluster"
  instance_class       =  "db.t2.micro"
  engine               =  "aurora-mysql"
  engine_version       =  "5.7"
}

module "mysql_aurora_cluster" {
  source               =  "../rds_cluster/"
  identifier           =  "mysql_aurora_cluster" 
  azs                  =  data.aws_availability_zones.azs.names
  db_name              =  "my_db"
  username             =  "demo"
  password             =  data.aws_secretsmanager_secret_version.creds.secret_id
}

Points to note:

  1. I am using terraform 0.14
  2. When I perform terraform validate I get "Success! The configuration is valid."
  3. However, when I perform terraform plan, I get the above error

Solution

  • terraform validate only checks if the config is

    syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including correctness of attribute names and value types.

    That is in your case the code looks good, the resources correctly interact with each other, the types are correct etc. But the actual values are incorrect, in particular the var.identifier does not match the expected pattern but validate simply does not check that.

    Solution: fix your var.identifier to match the pattern the aws_rds_cluster demands for its cluster_identifier, namely "mysql_aurora_cluster" should be "mysql-aurora-cluster"