-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
161 lines (112 loc) · 3.83 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
data "aws_region" "current" {}
# VPC
resource "aws_vpc" "this" {
cidr_block = var.cidr_block
# required for VPC endpoints with type `Interface`
enable_dns_support = true
enable_dns_hostnames = true
tags = merge({
Name = var.name
}, var.default_tags, var.vpc_tags)
}
# Internet Gateway
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = merge({
Name = var.name
}, var.default_tags, var.internet_gateway_tags)
}
# Pairs of Public-Private Subnets per Availability Zone
module "availability_zone" {
source = "./availability-zone"
count = var.size
vpc = aws_vpc.this
internet_gateway = aws_internet_gateway.this
subnet_bits = var.subnet_bits
subnet_index = count.index
nat_gateway_eip_tags = var.nat_gateway_eip_tags
nat_gateway_tags = var.nat_gateway_tags
private_route_table_tags = var.private_route_table_tags
private_subnet_tags = var.private_subnet_tags
public_route_table_tags = var.public_route_table_tags
public_subnet_tags = var.public_subnet_tags
default_tags = var.default_tags
}
# Subnet Groups (RDS, ElastiCache)
resource "aws_db_subnet_group" "this" {
count = var.create_db_subnet_group ? 1 : 0
name = var.name
description = var.name
subnet_ids = module.availability_zone[*].private_subnet.id
tags = merge(var.default_tags, var.db_subnet_group_tags)
}
resource "aws_elasticache_subnet_group" "this" {
count = var.create_elasticache_subnet_group ? 1 : 0
name = var.name
description = var.name
subnet_ids = module.availability_zone[*].private_subnet.id
}
# VPC Endpoints: type `Gateway`
resource "aws_vpc_endpoint" "gateway" {
for_each = toset(var.vpc_endpoints.gateway)
vpc_id = aws_vpc.this.id
service_name = "com.amazonaws.${data.aws_region.current.name}.${each.key}"
vpc_endpoint_type = "Gateway"
route_table_ids = module.availability_zone[*].private_route_table.id
tags = merge({
Name = each.key
}, var.default_tags, var.gateway_vpc_endpoint_tags)
}
# VPC Endpoints: type `Interface`
resource "aws_vpc_endpoint" "interface" {
for_each = toset(var.vpc_endpoints.interface)
vpc_id = aws_vpc.this.id
service_name = "com.amazonaws.${data.aws_region.current.name}.${each.key}"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
subnet_ids = module.availability_zone[*].private_subnet.id
security_group_ids = [aws_security_group.vpc-endpoints-interface[0].id]
tags = merge({
Name = each.key
}, var.default_tags, var.interface_vpc_endpoint_tags)
depends_on = [
aws_security_group_rule.vpc-endpoints-interface-ingress,
aws_security_group_rule.vpc-endpoints-interface-egress,
]
}
resource "aws_security_group" "vpc-endpoints-interface" {
count = length(var.vpc_endpoints.interface) > 0 ? 1 : 0
vpc_id = aws_vpc.this.id
name = "vpc-endpoints-interface"
description = "VPC Endpoints Interface"
tags = merge({
Name = "VPC Endpoints Interface"
}, var.default_tags, var.interface_vpc_endpoint_security_group_tags)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "vpc-endpoints-interface-ingress" {
count = length(var.vpc_endpoints.interface) > 0 ? 1 : 0
security_group_id = aws_security_group.vpc-endpoints-interface[count.index].id
cidr_blocks = [aws_vpc.this.cidr_block]
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "vpc-endpoints-interface-egress" {
count = length(var.vpc_endpoints.interface) > 0 ? 1 : 0
security_group_id = aws_security_group.vpc-endpoints-interface[count.index].id
cidr_blocks = ["0.0.0.0/0"]
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
lifecycle {
create_before_destroy = true
}
}