forked from SUSE/ha-sap-terraform-deployments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infrastructure.tf
261 lines (211 loc) · 7.29 KB
/
infrastructure.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
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
# Configure the AWS Provider
provider "aws" {
version = "~> 3.11.0"
region = var.aws_region
}
terraform {
required_version = ">= 0.13"
}
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
vpc_address_range = var.vpc_id == "" ? var.vpc_address_range : (var.vpc_address_range == "" ? data.aws_vpc.current-vpc.0.cidr_block : var.vpc_address_range)
infra_subnet_address_range = var.infra_subnet_address_range != "" ? var.infra_subnet_address_range : cidrsubnet(local.vpc_address_range, 8, 0)
hana_subnet_address_range = length(var.hana_subnet_address_range) != 0 ? var.hana_subnet_address_range : [
for index in range(var.hana_count) : cidrsubnet(local.vpc_address_range, 8, index + 1)]
# The 2 is hardcoded because we create 2 subnets for NW always
netweaver_subnet_address_range = length(var.netweaver_subnet_address_range) != 0 ? var.netweaver_subnet_address_range : [
for index in range(2) : cidrsubnet(local.vpc_address_range, 8, index + var.hana_count + 1)]
# The 2 is hardcoded considering we create 2 subnets for NW always
drbd_subnet_address_range = length(var.drbd_subnet_address_range) != 0 ? var.drbd_subnet_address_range : [
for index in range(2) : cidrsubnet(local.vpc_address_range, 8, index + var.hana_count + 2 + 1)]
}
# EFS storage for nfs share used by Netweaver for /usr/sap/{sid} and /sapmnt
# It will be created for netweaver only when drbd is disabled
resource "aws_efs_file_system" "netweaver-efs" {
count = var.netweaver_enabled == true && var.drbd_enabled == false ? 1 : 0
creation_token = "${local.deployment_name}-netweaver-efs"
performance_mode = var.netweaver_efs_performance_mode
tags = {
Name = "${local.deployment_name}-efs"
}
}
# 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" "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
}