Home > Net >  How to define different values for same variable in different modules in terraform cli?
How to define different values for same variable in different modules in terraform cli?

Time:06-20

This is my main.tf and I want to define cg_ip_address differently for different modules during terraform plan and apply. I tried terraform plan -target=module.us -var "is_second_execution=true" -var "cg_ip_address=152.67.25.238" -target=module.mumbai -var "is_second_execution=true" -var "cg_ip_address=152.67.25.237" but in both modules the cg_ip_address was taken as 152.67.25.237.

module "mumbai" {
  source                    = "./site-to-site-vpn-setup"
  providers                 = { aws = aws.mumbai }
  vpc_cidr                  = local.vpc_cidr
  public_subnets_cidr_list  = local.public_subnets_cidr_list
  private_subnets_cidr_list = local.private_subnets_cidr_list
  oci_vcn_cidr              = local.oci_vcn_cidr
  cg_ip_address             = var.cg_ip_address
  is_second_execution       = var.is_second_execution
}

module "saopaulo" {
  source                    = "./site-to-site-vpn-setup"
  providers                 = {aws = aws.saopaulo}
  vpc_cidr                  = local.vpc_cidr
  public_subnets_cidr_list  = local.public_subnets_cidr_list
  private_subnets_cidr_list = local.private_subnets_cidr_list
  oci_vcn_cidr              = local.oci_vcn_cidr
  cg_ip_address             = var.cg_ip_address
  is_second_execution       = var.is_second_execution
}

module "us" {
  source                    = "./site-to-site-vpn-setup"
  providers                 = {aws = aws.us}
  vpc_cidr                  = local.vpc_cidr
  public_subnets_cidr_list  = local.public_subnets_cidr_list
  private_subnets_cidr_list = local.private_subnets_cidr_list
  oci_vcn_cidr              = local.oci_vcn_cidr
  cg_ip_address             = var.cg_ip_address
  is_second_execution       = var.is_second_execution
}```

CodePudding user response:

You either have to create three different varibles, e.g.:

var.mumbai_cg_ip_address
var.saopaulo_cg_ip_address
var.us_cg_ip_address

or one variable as a map or a list. For example:

cg_ip_address='{"mumbai":"152.67.25.237","saopaulo":"152.67.25.217","us":"152.67.25.27"}'

then

module "mumbai" {
  source                    = "./site-to-site-vpn-setup"
  providers                 = { aws = aws.mumbai }
  vpc_cidr                  = local.vpc_cidr
  public_subnets_cidr_list  = local.public_subnets_cidr_list
  private_subnets_cidr_list = local.private_subnets_cidr_list
  oci_vcn_cidr              = local.oci_vcn_cidr
  cg_ip_address             = var.cg_ip_address["mumbai"]
  is_second_execution       = var.is_second_execution
}

module "saopaulo" {
  source                    = "./site-to-site-vpn-setup"
  providers                 = {aws = aws.saopaulo}
  vpc_cidr                  = local.vpc_cidr
  public_subnets_cidr_list  = local.public_subnets_cidr_list
  private_subnets_cidr_list = local.private_subnets_cidr_list
  oci_vcn_cidr              = local.oci_vcn_cidr
  cg_ip_address             = var.cg_ip_address["saopaulo"]
  is_second_execution       = var.is_second_execution
}

module "us" {
  source                    = "./site-to-site-vpn-setup"
  providers                 = {aws = aws.us}
  vpc_cidr                  = local.vpc_cidr
  public_subnets_cidr_list  = local.public_subnets_cidr_list
  private_subnets_cidr_list = local.private_subnets_cidr_list
  oci_vcn_cidr              = local.oci_vcn_cidr
  cg_ip_address             = var.cg_ip_address["us"]
  is_second_execution       = var.is_second_execution
}
  • Related