Home > Net >  Multiple aws_rds_cluster_instance using for_each
Multiple aws_rds_cluster_instance using for_each

Time:11-08

I want to create a module

In my route variable.tfvars file , i am passing below input.

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 file

 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
}

I am trying to convert variable in below form so that in can use it in the module.

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"
  }

Below expression doesn't seem to be working .Please help. I want to use for-each only so during modification/removing any block , I don't face the variable block order issue.

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

CodePudding user response:

This is not a straightforward transformation, your rds_aurora is an array of objects, and we have a count in in one of the properties of that object, we are going to need an intermediary variable to transform that and that will require a few loops...

Here is what I would do:

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
}

Let's break down what is happening there we have 3 loops

  • for i in flatten([
    this one we put the final object together
  • for x in local.rds_aurora
    just a loop over the elements in rds_aurora
  • for y in range(x.cluster_instance_count)
    this is the one that gets that id (0, 1) that we will latter concatenate for the final objects
  • Related