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

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

详细错误为:

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

我在从 terraform 创建 rds_cluster 时遇到上述错误。 我的 main.tf 文件包含以下代码:

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
}

variable.tf 文件是:

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

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

variable "db_name" {
  default = "mydb"
}

variable "username" {}

variable "password" {}

variable "azs" {}

调用这个“rds_cluster”的模块文件是:

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
}

注意事项:

  1. 我正在使用 terraform 0.14
  2. 当我执行 terraform validate 时,我得到“成功!配置有效。”
  3. 但是,当我执行 terraform plan 时,出现上述错误

terraform validate 只检查配置是否为

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.

在您的情况下,代码看起来不错,资源相互正确交互,类型正确等。但是实际值不正确,特别是 var.identifier 与预期模式不匹配但是 validate 根本不检查。

解决方案:修复您的 var.identifier 以匹配 aws_rds_cluster 对其 cluster_identifier 要求的模式,即 "mysql_aurora_cluster" 应该是 "mysql-aurora-cluster"