Conditional creation of a resource based on a variable in .tfvars.json

Viewed 33

I have to create RDS with data encryption enabled in the multi-region. When RDS creating I need to enable "storage_encrypted": true, and when encryption is enabled AWS required multi-region support kms_key_id to create global RDS.

There are two scenarios I have to validate before creating the RDS.

  1. When storage_encrypted": true, get the "kms_key_id" : "arn:aws:kms:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", create a RDS.
  2. If storage_encrypted": false, get the default kms_key_id key and create RDS with validating a condition.

How can I make conditions for the above scenario?

In the variables.tf

variable "storage_encrypted" {
  type        = bool
  default     = false
}

variable "kms_key_id" {
  type        = string
  default     = null
}

In the vars.tfvars.json file I have two parameters.

"storage_encrypted" : true,   
"kms_key_id" : "arn:aws:kms:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

This is the current aurora.tf module for secondary

module "aurora_secondary" {
  source  = "terraform-aws-modules/rds-aurora/aws"
  version = "7.3.0"

  apply_immediately   = var.apply_immediately
  create_cluster = var.create_secondary_cluster
  providers = { aws = aws.secondary }

  is_primary_cluster = false  
  source_region             = var.primary_region

  name           = var.name

  engine                    = var.create_global_cluster ? aws_rds_global_cluster.this.*.engine[0] : null
  engine_version            = var.create_global_cluster ? aws_rds_global_cluster.this.*.engine_version[0] : null
  global_cluster_identifier = var.create_global_cluster ? aws_rds_global_cluster.this.*.id[0] : null
  
  *********************************************
  storage_encrypted     = var.storage_encrypted
  *********************************************

  create_random_password              = var.create_random_password

  instance_class = var.instance_class
  instances = var.secondary_instances


  vpc_id                 = var.vpc_id_us_east_2
  create_db_subnet_group = var.create_secondary_db_subnet_group
  subnets  = var.private_subnets_us_east_2
  create_security_group  = var.create_secondary_security_group
  allowed_cidr_blocks    = var.allowed_cidr_blocks

  
  create_monitoring_role                = var.create_monitoring_role
  monitoring_interval                   = var.monitoring_interval
  monitoring_role_arn                   = var.monitoring_role_arn

  backup_retention_period = var.backup_retention_period
  preferred_backup_window = var.preferred_backup_window
  preferred_maintenance_window = var.preferred_maintenance_window

  depends_on = [
    module.aurora
  ]

  tags = local.cluster_tags
}
0 Answers
Related