I want to create a chained/nested variable for multiple attributes.
For example, I have this resource block:
resource "aws_rds_cluster" "default" {
cluster_identifier = "aurora-cluster-demo"
engine = var.db_engine
engine_version = var.db_engine_version
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
database_name = "pgdb"
master_username = "foo"
master_password = "bar"
backup_retention_period = 5
preferred_backup_window = "07:00-09:00"
db_cluster_parameter_group_name = "rds-cluster-pg"
}
resource "aws_rds_cluster_parameter_group" "default" {
name = "rds-cluster-pg"
family = var.db_engine_family
description = "RDS default cluster parameter group"
parameter {
name = "character_set_server"
value = "utf8"
}
parameter {
name = "character_set_client"
value = "utf8"
}
}
I have three variables related to each other: db_engine, db_engine_family and db_engine_version.
I would like to know if and how it is possible to chain the three, so if I set db_engine=postgresql, the other variables will match it.
For example:
db_engine = PostgreSQL, db_engine_version= 13.6 and db_engine_family=aurora-postgresql9.6
.
If db_engine=mysql, db_engine_version=5.7.mysql_aurora.2.03.2 and db_engine_family=aurora-mysql5.7.
Thank you
CodePudding user response:
The best way would be to create map:
variable "db" {
default = {
PostgreSQL = {
db_engine_version = 13.6
db_engine_family = "aurora-postgresql9.6"
},
mysql = {
db_engine_version = "5.7.mysql_aurora.2.03.2"
db_engine_family = "aurora-mysql5.7"
}
}
}
then
resource "aws_rds_cluster" "default" {
cluster_identifier = "aurora-cluster-demo"
engine = var.db_engine
engine_version = var.db[var.db_engine].db_engine_version
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
database_name = "pgdb"
master_username = "foo"
master_password = "bar"
backup_retention_period = 5
preferred_backup_window = "07:00-09:00"
db_cluster_parameter_group_name = "rds-cluster-pg"
}
resource "aws_rds_cluster_parameter_group" "default" {
name = "rds-cluster-pg"
family = var.db[var.db_engine].db_engine_family
description = "RDS default cluster parameter group"
parameter {
name = "character_set_server"
value = "utf8"
}
parameter {
name = "character_set_client"
value = "utf8"
}
}