Consistent type in terraform true and false expressions

Viewed 89

I have configuration module with AmazonMQ

module "amazonmq_cluster" {
      source = "git::https://github.com/cloudposse/terraform-aws-mq-broker.git?ref=0.15.0"
    
      namespace                    = module.amazonmq_cluster_label.namespace
      stage                        = module.amazonmq_cluster_label.stage
      name                         = module.amazonmq_cluster_label.id
      apply_immediately            = true
      auto_minor_version_upgrade   = false
      deployment_mode              = var.cluster_deployment_mode[local.stage]
      engine_type                  = var.cluster_engine_type[local.stage]
      engine_version               = var.cluster_engine_version[local.stage]
      host_instance_type           = var.cluster_host_instance_type[local.stage]
      publicly_accessible          = false
      general_log_enabled          = true
      audit_log_enabled            = false
      encryption_enabled           = true
      use_aws_owned_key            = true
      vpc_id                       = module.vpc.vpc_id
      subnet_ids                   = var.cluster_deployment_mode[local.stage] != "ACTIVE_STANDBY_MULTI_AZ" ? module.vpc.private_subnet_ids[0] : module.vpc.private_subnet_ids
      security_groups               = [module.sg.groups]
    }

And I need descrive subnet_ids conditionally by variable map of string. Map of string is set for terraform workspaces. But when I try create module terraform give me error with Inconsistent conditional result types.

Error: Inconsistent conditional result types

  on amazonmq.tf line 31, in module "amazonmq_cluster":
  31:   subnet_ids                   = var.cluster_deployment_mode[local.stage] != "ACTIVE_STANDBY_MULTI_AZ" ? module.vpc.private_subnet_ids[0] : module.vpc.private_subnet_ids
    |----------------
    | local.stage is "staging"
    | module.vpc.private_subnet_ids is tuple with 2 elements
    | module.vpc.private_subnet_ids[0] is "XXX"
    | var.cluster_deployment_mode is map of string with 3 elements

How I can correct set this condition? If !=ACTIVE_STANDBY_MULTI_AZ pass one VPC ID else pass all from output.

1 Answers

How I can correct set this condition? If !=ACTIVE_STANDBY_MULTI_AZ pass one VPC ID else pass all from output.

Try this as an expression:

var.cluster_deployment_mode[local.stage] != "ACTIVE_STANDBY_MULTI_AZ" ? [module.vpc.private_subnet_ids[0]] : module.vpc.private_subnet_ids

It builds a single-element list for the first option.

Related