Search code examples
amazon-web-servicesconditional-statementsterraformamazon-rdsterraform-provider-aws

Terraform : How to map option settings with an option name dynamically?


I am newbee to terraform and trying to add multiple options into db_option_group dynamically. Using the dynamic block, I can loop thru each option name but that would add the same settings that I would mention in the content section for each option_name.

How would I map one option_name with it's particular option_setting dynamically?

For ex. I am trying to add below options to the option group using variables and not hard code into the module code,

# Creating DB Option Group
resource "aws_db_option_group" "db-opt-grp" {
  name                     = var.db_option_group_name
  option_group_description = "DB Option Group managed by Terraform"
  engine_name              = var.db_server_engine_type
  major_engine_version     = var.db_major_engine_version

  option {
    option_name = "Timezone"

    option_settings {
      name  = "TIME_ZONE"
      value = "Europe/Berlin"
    }
  }

  option {
    option_name = "SSL"
    vpc_security_group_memberships = [aws_security_group.db-sg.id]

    option_settings {
      name  = "FIPS.SSLFIPS_140"
      value = "FALSE"
    }
    option_settings {
      name  = "SQLNET.CIPHER_SUITE"
      value = "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"
    }
    option_settings {
      name  = "SQLNET.SSL_VERSION"
      value = "1.2"
    }
  }

Do I need to pass in the option_name & it's settings together in a variable of type map(object)? I am bit lost here. Can someone help and throw some light?

Does it require a nested Dynamic block inside a dynamic block? If yes, can someone to explain it's implementation and variable declaration?

I tried to loop thru each option_name but the problem is the content sections remains static for each option which is I don't want.

I am expecting, each option should pick it's own setting that I will pass into another variable or something. And this is where I am exactly lost on how to achieve that.


Solution

  • Use a local block

    locals {
      timezone_option = {
        option_name = "Timezone"
      
        option_settings {
          name  = "TIME_ZONE"
          value = "Europe/Berlin"
        }
      }
    }
    

    You can now use

    option {
       local.timezone_option
    }
    

    in your Terraform configuration to reference this local value anywhere in the code.