forked from snowplow-devops/terraform-aws-iglu-server-ec2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
269 lines (222 loc) · 7.55 KB
/
main.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
262
263
264
265
266
267
268
269
locals {
module_name = "iglu-server-ec2"
module_version = "0.4.3"
app_name = "iglu-server"
app_version = "0.10.0"
local_tags = {
Name = var.name
app_name = local.app_name
app_version = local.app_version
module_name = local.module_name
module_version = local.module_version
}
tags = merge(
var.tags,
local.local_tags
)
cloudwatch_log_group_name = "/aws/ec2/${var.name}"
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
module "telemetry" {
source = "snowplow-devops/telemetry/snowplow"
version = "0.4.0"
count = var.telemetry_enabled ? 1 : 0
user_provided_id = var.user_provided_id
cloud = "AWS"
region = data.aws_region.current.name
app_name = local.app_name
app_version = local.app_version
module_name = local.module_name
module_version = local.module_version
}
# --- CloudWatch: Logging
resource "aws_cloudwatch_log_group" "log_group" {
count = var.cloudwatch_logs_enabled ? 1 : 0
name = local.cloudwatch_log_group_name
retention_in_days = var.cloudwatch_logs_retention_days
tags = local.tags
}
# --- IAM: Roles & Permissions
resource "aws_iam_role" "iam_role" {
name = var.name
description = "Allows the Iglu Server nodes to access required services"
tags = local.tags
assume_role_policy = <<EOF
{
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}
]
}
EOF
permissions_boundary = var.iam_permissions_boundary
}
resource "aws_iam_policy" "iam_policy" {
name = var.name
policy = <<EOF
{
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:DescribeLogStreams"
],
"Resource": [
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:${local.cloudwatch_log_group_name}:*"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "policy_attachment" {
role = aws_iam_role.iam_role.name
policy_arn = aws_iam_policy.iam_policy.arn
}
resource "aws_iam_instance_profile" "instance_profile" {
name = var.name
role = aws_iam_role.iam_role.name
}
# --- EC2: Security Group Rules
resource "aws_security_group" "sg" {
name = var.name
vpc_id = var.vpc_id
tags = local.tags
}
resource "aws_security_group_rule" "ingress_tcp_22" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.ssh_ip_allowlist
security_group_id = aws_security_group.sg.id
}
# Allows ingress from the load balancer to the webserver
resource "aws_security_group_rule" "ingress_tcp_webserver" {
type = "ingress"
from_port = var.ingress_port
to_port = var.ingress_port
protocol = "tcp"
source_security_group_id = var.iglu_server_lb_sg_id
security_group_id = aws_security_group.sg.id
}
# Allows egress from the webserver to the RDS instance
resource "aws_security_group_rule" "egress_tcp_webserver_rds" {
type = "egress"
from_port = var.db_port
to_port = var.db_port
protocol = "tcp"
source_security_group_id = var.db_sg_id
security_group_id = aws_security_group.sg.id
}
resource "aws_security_group_rule" "egress_tcp_80" {
type = "egress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.sg.id
}
resource "aws_security_group_rule" "egress_tcp_443" {
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.sg.id
}
# Needed for clock synchronization
resource "aws_security_group_rule" "egress_udp_123" {
type = "egress"
from_port = 123
to_port = 123
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.sg.id
}
# --- EC2: Security Group Rules for the Load Balancer
# Allows egress from the load balancer to the webserver
resource "aws_security_group_rule" "lb_egress_tcp_webserver" {
type = "egress"
from_port = var.ingress_port
to_port = var.ingress_port
protocol = "tcp"
source_security_group_id = aws_security_group.sg.id
security_group_id = var.iglu_server_lb_sg_id
}
# --- EC2: Security Group Rules for the RDS Instance
# Allows ingress from the webserver to the RDS Instance
resource "aws_security_group_rule" "rds_egress_tcp_webserver" {
type = "ingress"
from_port = var.db_port
to_port = var.db_port
protocol = "tcp"
source_security_group_id = aws_security_group.sg.id
security_group_id = var.db_sg_id
}
# --- EC2: Auto-scaling group & Launch Configurations
module "instance_type_metrics" {
source = "snowplow-devops/ec2-instance-type-metrics/aws"
version = "0.1.2"
instance_type = var.instance_type
}
locals {
iglu_server_hocon = templatefile("${path.module}/templates/config.hocon.tmpl", {
port = var.ingress_port
db_host = var.db_host
db_port = var.db_port
db_name = var.db_name
db_username = var.db_username
db_password = var.db_password
patches_allowed = var.patches_allowed
super_api_key = lower(var.super_api_key)
})
user_data = templatefile("${path.module}/templates/user-data.sh.tmpl", {
port = var.ingress_port
config = local.iglu_server_hocon
version = local.app_version
db_host = var.db_host
db_port = var.db_port
db_name = var.db_name
db_username = var.db_username
db_password = var.db_password
telemetry_script = join("", module.telemetry.*.amazon_linux_2_user_data)
cloudwatch_logs_enabled = var.cloudwatch_logs_enabled
cloudwatch_log_group_name = local.cloudwatch_log_group_name
container_memory = "${module.instance_type_metrics.memory_application_mb}m"
java_opts = var.java_opts
})
}
module "service" {
source = "snowplow-devops/service-ec2/aws"
version = "0.2.0"
user_supplied_script = local.user_data
name = var.name
tags = local.tags
amazon_linux_2_ami_id = var.amazon_linux_2_ami_id
instance_type = var.instance_type
ssh_key_name = var.ssh_key_name
iam_instance_profile_name = aws_iam_instance_profile.instance_profile.name
associate_public_ip_address = var.associate_public_ip_address
security_groups = [aws_security_group.sg.id]
min_size = var.min_size
max_size = var.max_size
subnet_ids = var.subnet_ids
target_group_arns = [var.iglu_server_lb_tg_id]
health_check_type = "ELB"
enable_auto_scaling = var.enable_auto_scaling
scale_up_cooldown_sec = var.scale_up_cooldown_sec
scale_up_cpu_threshold_percentage = var.scale_up_cpu_threshold_percentage
scale_up_eval_minutes = var.scale_up_eval_minutes
scale_down_cooldown_sec = var.scale_down_cooldown_sec
scale_down_cpu_threshold_percentage = var.scale_down_cpu_threshold_percentage
scale_down_eval_minutes = var.scale_down_eval_minutes
}