-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpc.tf
More file actions
179 lines (153 loc) · 5.82 KB
/
vpc.tf
File metadata and controls
179 lines (153 loc) · 5.82 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#tfsec:ignore:aws-ec2-require-vpc-flow-logs-for-all-vpcs VPC Flow Logs are optional and can be enabled by users if required
resource "aws_vpc" "vpc" {
count = local.create_vpc ? 1 : 0
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
tags = local.common_tags
}
resource "aws_internet_gateway" "gateway" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.vpc[0].id
tags = merge(local.common_tags, {
Name = local.stack_name_full
})
}
resource "aws_subnet" "subnet0" {
count = local.create_vpc ? 1 : 0
availability_zone = local.use_custom_azs ? element(split(",", var.availability_zones), 0) : element(data.aws_availability_zones.available.names, 0)
cidr_block = "10.0.1.0/24"
vpc_id = aws_vpc.vpc[0].id
tags = merge(local.common_tags, {
Name = "${local.stack_name_full}-subnet0"
})
lifecycle {
ignore_changes = [availability_zone]
}
}
resource "aws_subnet" "subnet1" {
count = local.create_vpc ? 1 : 0
availability_zone = local.use_custom_azs ? element(split(",", var.availability_zones), 1) : element(data.aws_availability_zones.available.names, 1)
cidr_block = "10.0.2.0/24"
vpc_id = aws_vpc.vpc[0].id
tags = merge(local.common_tags, {
Name = "${local.stack_name_full}-subnet1"
})
lifecycle {
ignore_changes = [availability_zone]
}
}
resource "aws_route_table" "routes" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.vpc[0].id
tags = local.common_tags
}
resource "aws_route" "route_default" {
count = local.create_vpc ? 1 : 0
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gateway[0].id
route_table_id = aws_route_table.routes[0].id
}
resource "aws_route_table_association" "subnet0_routes" {
count = local.create_vpc ? 1 : 0
subnet_id = aws_subnet.subnet0[0].id
route_table_id = aws_route_table.routes[0].id
}
resource "aws_route_table_association" "subnet1_routes" {
count = local.create_vpc ? 1 : 0
subnet_id = aws_subnet.subnet1[0].id
route_table_id = aws_route_table.routes[0].id
}
resource "aws_security_group" "security_group" {
count = local.create_security_group ? 1 : 0
name = "${local.stack_name_full}-agent-sg"
description = "Enable access to agents"
vpc_id = local.create_vpc ? aws_vpc.vpc[0].id : var.vpc_id
tags = local.common_tags
# Allow all outbound traffic (required for agents to connect to Buildkite, download artifacts, etc.)
#tfsec:ignore:aws-ec2-no-public-egress-sgr Buildkite agents require internet access to connect to Buildkite API and download artifacts
#tfsec:ignore:aws-ec2-add-description-to-security-group-rule Description provided in comment above
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_vpc_security_group_ingress_rule" "security_group_ssh_ingress" {
count = local.enable_ssh_ingress ? 1 : 0
security_group_id = aws_security_group.security_group[0].id
ip_protocol = "tcp"
from_port = 22
to_port = 22
cidr_ipv4 = "0.0.0.0/0"
}
# VPC Endpoints for SSM connectivity
resource "aws_vpc_endpoint" "ssm" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.vpc[0].id
service_name = "com.amazonaws.${data.aws_region.current.id}.ssm"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnet0[0].id, aws_subnet.subnet1[0].id]
security_group_ids = [aws_security_group.vpc_endpoint_sg[0].id]
private_dns_enabled = true
tags = merge(local.common_tags, {
Name = "${local.stack_name_full}-ssm-endpoint"
})
}
resource "aws_vpc_endpoint" "ssmmessages" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.vpc[0].id
service_name = "com.amazonaws.${data.aws_region.current.id}.ssmmessages"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnet0[0].id, aws_subnet.subnet1[0].id]
security_group_ids = [aws_security_group.vpc_endpoint_sg[0].id]
private_dns_enabled = true
tags = merge(local.common_tags, {
Name = "${local.stack_name_full}-ssmmessages-endpoint"
})
}
resource "aws_vpc_endpoint" "ec2messages" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.vpc[0].id
service_name = "com.amazonaws.${data.aws_region.current.id}.ec2messages"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnet0[0].id, aws_subnet.subnet1[0].id]
security_group_ids = [aws_security_group.vpc_endpoint_sg[0].id]
private_dns_enabled = true
tags = merge(local.common_tags, {
Name = "${local.stack_name_full}-ec2messages-endpoint"
})
}
# Security group for VPC endpoints
resource "aws_security_group" "vpc_endpoint_sg" {
count = local.create_vpc ? 1 : 0
name = "${local.stack_name_full}-vpc-endpoints"
description = "Security group for VPC endpoints"
vpc_id = aws_vpc.vpc[0].id
#tfsec:ignore:aws-ec2-add-description-to-security-group-rule HTTPS ingress from VPC for endpoint access
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [aws_vpc.vpc[0].cidr_block]
}
#tfsec:ignore:aws-ec2-no-public-egress-sgr VPC endpoints require egress for AWS service communication
#tfsec:ignore:aws-ec2-add-description-to-security-group-rule Egress required for VPC endpoint responses
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(local.common_tags, {
Name = "${local.stack_name_full}-vpc-endpoints-sg"
})
lifecycle {
create_before_destroy = true
}
}