-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvpc.tf
106 lines (81 loc) · 2.89 KB
/
vpc.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
# SEE: https://www.terraform.io/docs/providers/aws/r/vpc.html
resource "aws_vpc" "vpc" {
cidr_block = var.cidr_block
instance_tenancy = var.tenancy
enable_dns_support = var.dns_sup_hn
enable_dns_hostnames = var.dns_sup_hn
tags = var.vpc_tags
}
resource "aws_subnet" "subnet-public" {
count = length(var.availability_zones)
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(
var.cidr_block,
ceil(log(length(var.availability_zones) * 2, 2)),
(count.index)
)
availability_zone = var.availability_zones[count.index]
tags = {
Name = "subnet-public-${count.index}"
}
}
resource "aws_subnet" "subnet-private" {
count = length(var.availability_zones)
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(
var.cidr_block,
ceil(log(length(var.availability_zones) * 2, 2)),
(count.index + length(var.availability_zones))
)
availability_zone = var.availability_zones[count.index]
tags = {
Name = "subnet-private-${count.index}"
}
}
#com.amazonaws.eu-central-1.ssm
#com.amazonaws.eu-central-1.ssmmessages
resource "aws_vpc_endpoint" "ep-if" {
count = length(var.ep_if_list)
vpc_id = aws_vpc.vpc.id
service_name = replace(var.ep_if_list[count.index], "/region/", var.region)
vpc_endpoint_type = "Interface"
security_group_ids = [ aws_security_group.sg-ep[count.index].id ]
subnet_ids = var.sn_pub_priv == "private" ? aws_subnet.subnet-private.*.id : aws_subnet.subnet-private.*.id
private_dns_enabled = var.ep_priv_dns
}
resource "aws_vpc_endpoint" "ep-gw" {
count = length(var.ep_gw_list)
vpc_id = aws_vpc.vpc.id
service_name = replace(var.ep_gw_list[count.index], "/region/", var.region)
vpc_endpoint_type = "Gateway"
route_table_ids = [ aws_route_table.route-ep[count.index].id ]
}
resource "aws_internet_gateway" "igw" {
count = var.enable_igw == true ? 1 : 0
vpc_id = aws_vpc.vpc.id
}
resource "aws_route_table" "route" {
count = var.enable_igw == true ? 1 : 0
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw[count.index].id
}
}
resource "aws_route_table_association" "rt-assoc-gw" {
count = var.enable_igw == true ? length(aws_subnet.subnet-public) : 0
subnet_id = aws_subnet.subnet-public[ count.index ].id
route_table_id = aws_route_table.route[count.index].id
}
resource "aws_route_table" "route-ep" {
count = var.enable_igw == true ? 1 : 0
vpc_id = aws_vpc.vpc.id
tags = {
Name = "main"
}
}
resource "aws_route_table_association" "rt-assoc" {
count = var.enable_igw == true ? length(aws_subnet.subnet-private) : 0
subnet_id = aws_subnet.subnet-private[ count.index ].id
route_table_id = aws_route_table.route-ep[count.index].id
}