-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-vpc.tf
129 lines (101 loc) · 2.87 KB
/
3-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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr_block
tags = {
Name = "${var.prefix}-${var.suffix}"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${var.prefix}-${var.suffix}"
}
}
resource "aws_subnet" "public" {
count = length(var.subnets_public)
vpc_id = aws_vpc.main.id
cidr_block = var.subnets_public[count.index].cidr_block
availability_zone = "${data.aws_region.current.name}${var.subnets_public[count.index].availability_zone_short}"
tags = {
Name = "${var.prefix}-public-${var.subnets_public[count.index].availability_zone_short}-${var.suffix}"
}
}
resource "aws_subnet" "private" {
count = length(var.subnets_private)
vpc_id = aws_vpc.main.id
cidr_block = var.subnets_private[count.index].cidr_block
availability_zone = "${data.aws_region.current.name}${var.subnets_private[count.index].availability_zone_short}"
tags = {
Name = "${var.prefix}-private-${var.subnets_private[count.index].availability_zone_short}-${var.suffix}"
}
}
resource "aws_eip" "nat" {
tags = {
Name = "${var.prefix}-${var.suffix}"
}
}
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public[0].id
tags = {
Name = "${var.prefix}-${var.suffix}"
}
depends_on = [aws_internet_gateway.main]
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = aws_vpc.main.cidr_block
gateway_id = "local"
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "${var.prefix}-public-${var.suffix}"
}
}
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = aws_vpc.main.cidr_block
gateway_id = "local"
}
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main.id
}
tags = {
Name = "${var.prefix}-private-${var.suffix}"
}
}
resource "aws_route_table_association" "public" {
count = length(var.subnets_public)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "private" {
count = length(var.subnets_private)
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private.id
}
resource "aws_s3_bucket" "flow_log" {
bucket = "${var.prefix}-flow-log-${var.suffix}"
}
resource "aws_s3_bucket_lifecycle_configuration" "flow_log" {
bucket = aws_s3_bucket.flow_log.id
rule {
id = "delete-after-ten-years"
status = "Enabled"
expiration {
# This is how CloudWatch defines 10 years
days = 3653
}
}
}
resource "aws_flow_log" "main" {
log_destination = aws_s3_bucket.flow_log.arn
log_destination_type = "s3"
traffic_type = "ALL"
vpc_id = aws_vpc.main.id
}