terraform-community-modules / tf_aws_vpc

[DEPRECATED] Use https://github.com/terraform-aws-modules/terraform-aws-vpc
Other
210 stars 203 forks source link

Nat gateway doesn't get created even when its set to true #47

Closed johnjackson17 closed 7 years ago

johnjackson17 commented 7 years ago

I entered all the values in variables.tf file and set nat gateway to true and still doesn't get created when i run terraform apply, everything else gets created. No failures or error messages

antonbabenko commented 7 years ago

Could you please make a gist with content of your variables.tf and I will give it a try. I am not using this module myself, so can't judge if something has been broken lately. Thanks!

NeckBeardPrince commented 7 years ago

+1

Same thing

resource "aws_nat_gateway" "natgw" {
  allocation_id = "${element(aws_eip.nateip.*.id, count.index)}"
  subnet_id     = "${element(aws_subnet.public.*.id, count.index)}"
  count         = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 1)}"

  depends_on = ["aws_internet_gateway.default"]
}

If I set "true", 1 instead of 0 it works Every option that has "true", 0 needs to be 1 and then it will create the NAT GW

ashb commented 7 years ago

Ah okay, I found out the problem.

Its a difference between true and "true". I (and I guess @NeckBeardPrince and @johnjackson17 ) put this:

module "vpc" {
  source = "github.com/terraform-community-modules/tf_aws_vpc?ref=v1.0.4"
  enable_dns_hostnames = true
  enable_dns_support = false
  enable_nat_gateway = true
  ...
}

However the problem is that enable_nat_gateway needs to be a string.

This example works for instance:

module "vpc" {
  source = "github.com/terraform-community-modules/tf_aws_vpc?ref=v1.0.4"
  enable_dns_hostnames = true
  enable_dns_support = false
  enable_nat_gateway = "true"
  ...
}

I will see if there's a way around this in terraform.

ashb commented 7 years ago

It looks like using conditionals which are new in TF v0.8 we can get it to work both ways. @antonbabenko What is the support policy for this repo? (I think the conditional version is more understandable, but if we want to support older than 0.8 it's not an option)

Given this mian.tf:

variable "enable_nat_gateway_string" {
  default = "true"
}
variable "enable_nat_gateway_bool" {
  default = true
}
variable "enable_nat_gateway_empty_string" {
  default = ""
}
> "${var.enable_nat_gateway_empty_string != "" ? 1 : 0}"
0
> "${var.enable_nat_gateway_bool != "" ? 1 : 0}"
1
> "${var.enable_nat_gateway_string != "" ? 1 : 0}"
1

(We need the != "" because otherwise we get this error:

> "${var.enable_nat_gateway_empty_string ? 1 : 0}"
__builtin_StringToBool: strconv.ParseBool: parsing "": invalid syntax in:

${"${var.enable_nat_gateway_empty_string ? 1 : 0}"}
ashb commented 7 years ago

(The 0.7 compat way of fixing this is to change it from:

lookup(map(var.enable_nat_gateway, 1), "true", 0)

to

lookup(map("true", 1, "1", 1), "${var.enable_nat_gateway}", 0)

which looks more correct to me anywayx = {"$enable" : 1 } seems odd, x = {"true": 1} )

n8io commented 7 years ago

@antonbabenko I believe this issue can be closed now.