使用for_each的多个aws_rds_cluster_instance



我想创建一个模块

在我的route variable.tfvars文件中,我正在传递以下输入。

rds_aurora = [
{
cluster_identifier               = "aurora-cluster-mysql-qa"
cluster_instance_count           = 2
engine                           = "aurora-mysql"
engine_version                   = "5.7.mysql_aurora.2.07.1"
},
{
cluster_identifier               = "aurora-cluster-mysql-dev"
cluster_instance_count           = 1
engine                           = "aurora-mysql"
engine_version                   = "5.7.mysql_aurora.2.07.1"
} 
}

Module.tf文件

resource "aws_rds_cluster_instance" "cluster_instances" {
for_each                            = { for rds_aurora_instance in var.rds_aurora : "${rds_aurora_instance.cluster_identifier}-{cluster_instance_count}" => rds_aurora_instance }
identifier                          = each.value.key
cluster_identifier                  = each.value.cluster_identifier
engine                              = each.value.engine
engine_version                      = each.value.engine_version
}

我正在尝试转换以下形式的变量,以便in可以在模块中使用它。

aurora-cluster-mysql-qa-0  => {
cluster_identifier               = "aurora-cluster-mysql-qa"
cluster_instance_count           = 2
engine                           = "aurora-mysql"
engine_version                   = "5.7.mysql_aurora.2.07.1"
}
aurora-cluster-mysql-qa-1  => {
cluster_identifier               = "aurora-cluster-mysql-qa"
cluster_instance_count           = 2
engine                           = "aurora-mysql"
engine_version                   = "5.7.mysql_aurora.2.07.1"
}

aurora-cluster-mysql-dev-0 => {
cluster_identifier               = "aurora-cluster-mysql-dev"
cluster_instance_count           = 2
engine                           = "aurora-mysql"
engine_version                   = "5.7.mysql_aurora.2.07.1"
}

下面的表达式似乎不起作用。请帮助。我只想使用for each,所以在修改/删除任何块的过程中,我不会面临可变块顺序的问题。

for_each                            = { for rds_aurora_instance in var.rds_aurora : "${rds_aurora_instance.cluster_identifier}-{cluster_instance_count}" => rds_aurora_instance }

这不是一个简单的转换,rds_aurora是一个对象数组,我们在该对象的一个属性中有一个计数,我们需要一个中间变量来转换它,这将需要几个循环。。。

以下是我要做的:

provider "aws" {
region = "us-east-2"
}
locals {
rds_aurora = [
{
cluster_identifier     = "aurora-cluster-mysql-qa"
cluster_instance_count = 2
engine                 = "aurora-mysql"
engine_version         = "5.7.mysql_aurora.2.07.1"
},
{
cluster_identifier     = "aurora-cluster-mysql-dev"
cluster_instance_count = 1
engine                 = "aurora-mysql"
engine_version         = "5.7.mysql_aurora.2.07.1"
}
]
data = { for i in flatten([
for x in local.rds_aurora :
[
for y in range(x.cluster_instance_count) :
{
"id"                     = y
"cluster_identifier"     = x.cluster_identifier
"cluster_instance_count" = 2
"engine"                 = x.engine
"engine_version"         = x.engine_version
}
]
]) : "${i.cluster_identifier}_${i.id}" => i
}
}
output "new_data" {
value = local.data
}

让我们来分析一下那里发生了什么,我们有3个环路

  • for i in flatten([
    这个我们把最后一个对象放在一起
  • for x in local.rds_aurora
    只是rds_aurora中元素的循环
  • for y in range(x.cluster_instance_count)
    这是一个获得id(0,1(的id,我们将在后面为最终对象连接

相关内容

  • 没有找到相关文章