-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinfrastructure.tf
More file actions
290 lines (234 loc) · 7.31 KB
/
infrastructure.tf
File metadata and controls
290 lines (234 loc) · 7.31 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Query for VPC id if user does not provide one
# for an existing VPC created outside of this deployment
data "aws_vpc" "current-vpc" {
count = var.vpc_id != "" ? 1 : 0
id = var.vpc_id
}
data "aws_internet_gateway" "current-gateway" {
count = var.vpc_id != "" ? 1 : 0
filter {
name = "attachment.vpc-id"
values = [var.vpc_id]
}
}
locals {
deployment_name = var.deployment_name != "" ? var.deployment_name : terraform.workspace
vpc_id = var.vpc_id == "" ? aws_vpc.vpc.0.id : var.vpc_id
internet_gateway = var.vpc_id == "" ? aws_internet_gateway.igw.0.id : data.aws_internet_gateway.current-gateway.0.internet_gateway_id
security_group_id = var.security_group_id != "" ? var.security_group_id : aws_security_group.secgroup.0.id
}
# AWS key pair
resource "aws_key_pair" "key-pair" {
key_name = "${local.deployment_name} - terraform"
public_key = module.common_variables.configuration["public_key"]
}
# AWS availability zones
data "aws_availability_zones" "available" {
state = "available"
}
# Network resources: VPC, Internet Gateways, Security Groups for the EC2 instances and for the EFS file system
resource "aws_vpc" "vpc" {
count = var.vpc_id == "" ? 1 : 0
cidr_block = local.vpc_address_range
enable_dns_hostnames = true
enable_dns_support = true
tags = {
name = "${local.deployment_name}-vpc"
workspace = local.deployment_name
}
}
resource "aws_internet_gateway" "igw" {
count = var.vpc_id == "" ? 1 : 0
vpc_id = local.vpc_id
tags = {
name = "${local.deployment_name}-igw"
workspace = local.deployment_name
}
}
resource "aws_subnet" "infra-subnet" {
vpc_id = local.vpc_id
cidr_block = local.infra_subnet_address_range
availability_zone = element(data.aws_availability_zones.available.names, 0)
tags = {
name = "${local.deployment_name}-infra-subnet"
workspace = local.deployment_name
}
}
resource "aws_route_table" "route-table" {
vpc_id = local.vpc_id
tags = {
name = "${local.deployment_name}-hana-route-table"
workspace = local.deployment_name
}
}
resource "aws_route_table_association" "infra-subnet-route-association" {
subnet_id = aws_subnet.infra-subnet.id
route_table_id = aws_route_table.route-table.id
}
resource "aws_route" "public" {
route_table_id = aws_route_table.route-table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = local.internet_gateway
}
locals {
create_security_group = var.security_group_id == "" ? 1 : 0
create_security_group_monitoring = var.security_group_id == "" && var.monitoring_enabled == true ? 1 : 0
}
resource "aws_security_group" "secgroup" {
count = local.create_security_group
name = "${local.deployment_name}-sg"
vpc_id = local.vpc_id
tags = {
name = "${local.deployment_name}-sg"
workspace = local.deployment_name
}
}
resource "aws_security_group_rule" "outall" {
count = local.create_security_group
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "local" {
count = local.create_security_group
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [local.vpc_address_range]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "http" {
count = local.create_security_group
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "https" {
count = local.create_security_group
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "hawk" {
count = local.create_security_group
type = "ingress"
from_port = 7630
to_port = 7630
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "ssh" {
count = local.create_security_group
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
# Monitoring rules
resource "aws_security_group_rule" "hanadb_exporter" {
count = local.create_security_group_monitoring
type = "ingress"
from_port = 9668
to_port = 9668
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "node_exporter" {
count = local.create_security_group_monitoring
type = "ingress"
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "ha_exporter" {
count = local.create_security_group_monitoring
type = "ingress"
from_port = 9664
to_port = 9664
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "saphost_exporter" {
count = local.create_security_group_monitoring
type = "ingress"
from_port = 9680
to_port = 9680
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "prometheus_server" {
count = local.create_security_group_monitoring
type = "ingress"
from_port = 9090
to_port = 9090
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
resource "aws_security_group_rule" "grafana_server" {
count = local.create_security_group_monitoring
type = "ingress"
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = local.security_group_id
}
# IBSM Peering resources
data "aws_vpc" "ibsm" {
count = var.ibsm_vpc_id != "" ? 1 : 0
id = var.ibsm_vpc_id
}
data "aws_route_tables" "ibsm" {
count = var.ibsm_vpc_id != "" ? 1 : 0
filter {
name = "vpc-id"
values = [var.ibsm_vpc_id]
}
}
resource "aws_vpc_peering_connection" "ibsm" {
# created only when ibsm_vpc_id is provided
count = var.ibsm_vpc_id != "" ? 1 : 0
vpc_id = local.vpc_id
peer_vpc_id = var.ibsm_vpc_id
auto_accept = true
tags = {
Name = "${local.deployment_name}-ibsm-peer"
workspace = local.deployment_name
}
}
# Route to IBSM
resource "aws_route" "to_ibsm" {
count = var.ibsm_vpc_id != "" ? 1 : 0
route_table_id = aws_route_table.route-table.id
destination_cidr_block = data.aws_vpc.ibsm[0].cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.ibsm[0].id
depends_on = [aws_vpc_peering_connection.ibsm]
}
resource "aws_route" "from_ibsm_" {
for_each = var.ibsm_vpc_id != "" ? toset(data.aws_route_tables.ibsm[0].ids) : toset([])
route_table_id = each.value
destination_cidr_block = local.vpc_address_range
vpc_peering_connection_id = aws_vpc_peering_connection.ibsm[0].id
lifecycle {
create_before_destroy = true
}
}