我正在尝试为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}
是否有特殊之处(也许包括路由信息?)使其与众不同。普通的硬编码字符串或变量,还是只有定时语言?
谢谢!