1

详细的错误是:

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 时,出现上述错误
4

1 回答 1

2

terraform validate只检查配置是否

无论提供的任何变量或现有状态如何,语法上有效且内部一致。因此,它主要用于可重用模块的一般验证,包括属性名称和值类型的正确性。

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

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

于 2020-12-15T08:19:57.553 回答