forked from cloudposse/terraform-aws-dynamic-subnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat-gateway.tf
77 lines (68 loc) · 2.1 KB
/
nat-gateway.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module "nat_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.17.0"
enabled = var.enabled
context = module.label.context
attributes = distinct(compact(concat(module.label.attributes, ["nat"])))
}
locals {
nat_gateway_eip_count = local.use_existing_eips ? 0 : local.nat_gateways_count
gateway_eip_allocations = local.use_existing_eips ? data.aws_eip.nat_ips.*.id : aws_eip.default.*.id
eips_allocations = local.use_existing_eips ? data.aws_eip.nat_ips.*.id : aws_eip.default.*.id
nat_gateways_count = var.nat_gateway_enabled && ! local.use_existing_eips ? length(var.availability_zones) : 0
}
resource "aws_eip" "default" {
count = var.enabled ? local.nat_gateway_eip_count : 0
vpc = true
tags = merge(
module.private_label.tags,
{
"Name" = format(
"%s%s%s",
module.private_label.id,
var.delimiter,
replace(
element(var.availability_zones, count.index),
"-",
var.delimiter
)
)
}
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_nat_gateway" "default" {
count = var.enabled ? local.nat_gateways_count : 0
allocation_id = element(local.gateway_eip_allocations, count.index)
subnet_id = element(aws_subnet.public.*.id, count.index)
tags = merge(
module.nat_label.tags,
{
"Name" = format(
"%s%s%s",
module.nat_label.id,
var.delimiter,
replace(
element(var.availability_zones, count.index),
"-",
var.delimiter
)
)
}
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "default" {
count = var.enabled ? local.nat_gateways_count : 0
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]
timeouts {
create = var.aws_route_create_timeout
delete = var.aws_route_delete_timeout
}
}