4

I am trying to run modules conditionally. Below is the code. It works fine if the values are provided but if var.accounts[*].vpc_ids is blank, it fails saying var.vpc_id can't be empty. But that is basically the condition based on which the modules should run. If the vpc_id count is 0, then the modules should not run. Please help.

resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
   transit_gateway_id = var.transit_gateway_id
  vpc_id = var.vpc_id
  subnet_ids = var.subnet_ids
  dns_support                                     = "disable"
  ipv6_support                                    = "disable"
  transit_gateway_default_route_table_association = false
  transit_gateway_default_route_table_propagation = false
}

locals {
  create_tgw_attach = var.accounts[*].vpc_ids != "" ? true : false
}

module "tgw_peer2" {
  source = "../modules/tgw"
    count = length(var.accounts[2].vpc_ids)
  providers  = {
    aws = aws.accepter2
  }
  create_tgw_attach      = local.create_tgw_attach
  transit_gateway_id = aws_ec2_transit_gateway.this.id
  vpc_id = var.accounts[2].vpc_ids[count.index]
  subnet_ids = var.accounts[2].vpc_subnets[count.index].subnet_ids
  destination_cidr_block = var.destination_cidr_block_route

  share_tgw                             = true
  create_tgw                            = false
}

module "tgw_peer3" {
  source = "../modules/tgw"
  create_tgw_attach      = local.create_tgw_attach
  count = length(var.accounts[3].vpc_ids)
  providers  = {
    aws = aws.accepter3
  }
  transit_gateway_id = aws_ec2_transit_gateway.this.id
  vpc_id = var.accounts[3].vpc_ids[count.index]
  subnet_ids = var.accounts[3].vpc_subnets[count.index].subnet_ids

  share_tgw                             = true
  create_tgw                            = false  
}
Maya Ray
  • 523
  • 1
  • 7
  • 21
  • 3
    Add a `count` on the module that is `1` whenever some condition is met and `0` in all other situations. – luk2302 Jun 23 '21 at 10:45
  • I tried the same, for some reason even if count is 0, it's validating the vpc_id. I added the below count. As I want the module to run that many times. count = var.accounts[0].vpc_ids != [""] ? length(var.accounts[0].vpc_ids) : 0 – Maya Ray Jun 23 '21 at 11:07
  • Could you please share the real error message? Also, I would expect this to fail because of the `locals` block, and unrelated to conditional module declaration. – Matthew Schuchard Jun 23 '21 at 12:35
  • I removed the locals when I added the new count statement. Error: \nSTDERR: \nError: vpc_id must not be empty, got \n\n on ../modules/tgw/main.tf line 39, in resource \"aws_ec2_transit_gateway_vpc_attachment\" \"this\":\n 39: vpc_id = var.vpc_id – Maya Ray Jun 23 '21 at 12:43
  • This got solved. I modified the condition to check if the account_id is null and it worked. Thanks a lot for the help @luk2302 – Maya Ray Jun 23 '21 at 13:21

1 Answers1

1

I was trying to put the condition of vpc_id which I was using as a value in the module. I modified the condition and it worked. Below is the code.

module "tgw_peer1" {
  source = "./modules/tgw"
  providers  = {
    aws = aws.accepter1
  }
  count = var.accounts[1].account_id != "" ? length(var.accounts[1].vpc_ids) : 0
  transit_gateway_id = aws_ec2_transit_gateway.this.id
  vpc_id = var.accounts[1].vpc_ids[count.index]
  subnet_ids = var.accounts[1].vpc_subnets[count.index].subnet_ids
}
Maya Ray
  • 523
  • 1
  • 7
  • 21