forked from cloudposse/terraform-aws-dynamic-subnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nat.tf
41 lines (35 loc) · 1.2 KB
/
nat.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module "nat_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
delimiter = "${var.delimiter}"
tags = "${var.tags}"
}
locals {
nat_gateways_count = "${var.nat_gateway_enabled == "true" ? length(var.availability_zones) : 0}"
}
resource "aws_eip" "default" {
count = "${local.nat_gateways_count}"
vpc = true
tags = "${module.private_label.tags}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_nat_gateway" "default" {
count = "${local.nat_gateways_count}"
allocation_id = "${element(aws_eip.default.*.id, count.index)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
tags = "${module.nat_label.tags}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "default" {
count = "${local.nat_gateways_count}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
nat_gateway_id = "${element(aws_nat_gateway.default.*.id, count.index)}"
destination_cidr_block = "0.0.0.0/0"
depends_on = ["aws_route_table.private"]
}