是否存在Terraform创建多个Aurora数据库集群的方法?我试图使cluster_name成为列表,但看起来“ cluster_identifier”资源需要一个String而不是一个字符串列表。我可以为一个集群创建多个实例,但是看不到在Terraform中创建多个数据库集群的方法。
# The Aurora cluster (which contains database instances created below).
resource "aws_rds_cluster" "aurora_cluster" {
engine = var.rds_engine
engine_version = var.rds_engine_version
cluster_identifier = var.rds_cluster_name
master_username = local.aurora_creds.username
master_password = local.aurora_creds.password
db_subnet_group_name = var.subnet_group_name
iam_roles = var.rds_iam_roles
vpc_security_group_ids = var.security_groups
lifecycle {
create_before_destroy = true
}
}
resource "aws_rds_cluster_instance" "aurora_instance" {
engine = var.rds_engine
engine_version = var.rds_engine_version
count = var.instance_count
identifier = "${var.rds_cluster_name}-${count.index}"
cluster_identifier = var.rds_cluster_name
instance_class = var.rds_instance_class
db_subnet_group_name = var.subnet_group_name
availability_zone = lookup(element(var.subnet_zones, count.index), "zone")
publicly_accessible = false
lifecycle {
create_before_destroy = true
}
}
答案 0 :(得分:0)
有几种方法可以在Terraform中配置x
的资源量。
您可以在资源本身中使用count属性。
locals {
instance_count = 2
}
resource "aws_rds_cluster_instance" "aurora_instance" {
count = local.instance_count
engine = var.rds_engine
engine_version = var.rds_engine_version
identifier = "${var.rds_cluster_name}-${count.index}"
cluster_identifier = var.rds_cluster_name
instance_class = var.rds_instance_class
db_subnet_group_name = var.subnet_group_name
availability_zone = lookup(element(var.subnet_zones, count.index), "zone")
publicly_accessible = false
lifecycle {
create_before_destroy = true
}
}
或者您可以使用for_each属性。
locals {
zones = ["us-west-2a", "us-west-2b"]
}
resource "aws_rds_cluster_instance" "aurora_instance" {
for_each = toset(local.zones)
engine = var.rds_engine
engine_version = var.rds_engine_version
identifier = "${var.rds_cluster_name}-${each.key}"
cluster_identifier = var.rds_cluster_name
instance_class = var.rds_instance_class
db_subnet_group_name = var.subnet_group_name
availability_zone = each.key
publicly_accessible = false
lifecycle {
create_before_destroy = true
}
}