forked from cloudposse/terraform-aws-dynamic-subnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.tf
101 lines (83 loc) · 3.03 KB
/
public.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module "public_label" {
source = "cloudposse/label/null"
version = "0.24.1"
attributes = ["public"]
tags = merge(
var.public_subnets_additional_tags,
{ (var.subnet_type_tag_key) = format(var.subnet_type_tag_value_format, "public") }
)
context = module.this.context
}
locals {
public_subnet_count = local.enabled && var.max_subnet_count == 0 ? length(flatten(data.aws_availability_zones.available.*.names)) : var.max_subnet_count
public_route_expr_enabled = local.enabled && signum(length(var.vpc_default_route_table_id)) == 1
public_network_acl_enabled = local.enabled && signum(length(var.public_network_acl_id)) == 0 ? 1 : 0
vpc_default_route_table_id = local.enabled ? signum(length(var.vpc_default_route_table_id)) : 0
}
resource "aws_subnet" "public" {
count = local.availability_zones_count
vpc_id = join("", data.aws_vpc.default.*.id)
availability_zone = element(var.availability_zones, count.index)
cidr_block = cidrsubnet(
signum(length(var.cidr_block)) == 1 ? var.cidr_block : join("", data.aws_vpc.default.*.cidr_block),
ceil(log(local.public_subnet_count * 2, 2)),
local.public_subnet_count + count.index
)
map_public_ip_on_launch = var.map_public_ip_on_launch
tags = merge(
module.public_label.tags,
{
"Name" = format("%s%s%s", module.public_label.id, local.delimiter, local.az_map[element(var.availability_zones, count.index)])
}
)
lifecycle {
ignore_changes = [tags.kubernetes, tags.SubnetType]
}
}
resource "aws_route_table" "public" {
count = local.public_route_expr_enabled ? 0 : local.enabled_count
vpc_id = join("", data.aws_vpc.default.*.id)
tags = module.public_label.tags
}
resource "aws_route" "public" {
count = local.public_route_expr_enabled ? 0 : local.enabled_count
route_table_id = join("", aws_route_table.public.*.id)
destination_cidr_block = "0.0.0.0/0"
gateway_id = var.igw_id
timeouts {
create = var.aws_route_create_timeout
delete = var.aws_route_delete_timeout
}
}
resource "aws_route_table_association" "public" {
count = local.public_route_expr_enabled ? 0 : local.availability_zones_count
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_route_table.public[0].id
}
resource "aws_route_table_association" "public_default" {
count = local.public_route_expr_enabled ? local.availability_zones_count : 0
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = var.vpc_default_route_table_id
}
resource "aws_network_acl" "public" {
count = local.public_network_acl_enabled
vpc_id = var.vpc_id
subnet_ids = aws_subnet.public.*.id
egress {
rule_no = 100
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
protocol = "-1"
}
ingress {
rule_no = 100
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
protocol = "-1"
}
tags = module.public_label.tags
}