使用硬编码的RDS集群ID创建数据库实例时,Terraform AWS RDS失败

时间:2018-07-20 12:20:51

标签: amazon-web-services amazon-rds terraform

我正在尝试为Terraform AWS RDS集群创建if / else语句。我有这样的工作方式:

module "aws" {
  # false = "0" and true = "1"
  create_from_snapshot = "1"
  snapshot_identifier = "db-final-snapshot"
}

# Normal deployment
resource "aws_rds_cluster" "cluster" {
  count = "${1 - var.create_from_snapshot}"
  cluster_identifier = "${var.db_name}"
}

# Creation from a snapshot
resource "aws_rds_cluster" "cluster_from_snapshot" {
  count = "${var.create_from_snapshot}"
  cluster_identifier = "${var.db_name}"
  snapshot_identifier = "${var.snapshot_identifier}"
}

但是,在尝试在集群实例内部定义cluster_identifier时遇到问题,第一种情况不能同时适用于两种情况,而第二种情况需要两个terraform apply语句:

# Everything works fine, but then 'cluster' isn't defined 
# if using the 'cluster_from_snapshot' resource
resource "aws_rds_cluster_instance" "cluster_instance" {
  cluster_identifier = "${aws_rds_cluster.cluster.id}"
}

# This throws and error on 'terraform apply'
resource "aws_rds_cluster_instance" "cluster_instance" {
  cluster_identifier = "${var.db_name}"
}

稍后在命令行上...

$> terraform apply
.... logs...
module.aurora.aws_rds_cluster.cluster_from_snapshot: 
    Creation complete after 50s (ID: testing)

Error: Error applying plan:
aws_rds_cluster_instance.cluster_instance.0: error creating RDS DB 
    Instance: DBClusterNotFound, could not find DB Cluster: testing
aws_rds_cluster_instance.cluster_instance.1: error creating RDS DB 
    Instance: DBClusterNotFound, could not find DB Cluster: testing

$> terraform apply # instance 0 & 1 get created no problem

为在两种情况下都适用的实例创建资源标识符的建议将不胜感激,但是我真的很好奇${aws_rds_cluster.cluster.id}是否有特殊之处(也许包括路由信息?)使其与众不同。普通的硬编码字符串或变量,还是只有定时语言?

谢谢!

0 个答案:

没有答案