-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
143 lines (124 loc) · 3.91 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
resource aws_cloudwatch_event_rule slowlog_check {
name_prefix = "slowlog_check_every_minute"
description = "Check for slowlogs every minute"
schedule_expression = "rate(1 minute)"
tags = var.tags
}
resource aws_cloudwatch_event_target slowlog_check {
rule = aws_cloudwatch_event_rule.slowlog_check.name
arn = aws_lambda_function.slowlog_check.arn
}
resource aws_lambda_permission slowlog_check {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.slowlog_check.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.slowlog_check.arn
}
resource aws_iam_role slowlog_check {
name_prefix = "slowlog_check"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource aws_iam_policy slowlog_check {
name_prefix = "slowlog_check"
path = "/"
description = "This IAM policy allows the slowlog_check to run"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ssm:GetParameter",
"ssm:GetParametersByPath"
],
"Resource": [
"arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/${var.ssm_path}",
"arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/${var.ssm_path}/*"
],
"Effect": "Allow"
}
]
}
EOF
}
resource aws_iam_role_policy_attachment "lambda_vpc_0" {
role = aws_iam_role.slowlog_check.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
resource aws_iam_role_policy_attachment "lambda_vpc_1" {
role = aws_iam_role.slowlog_check.name
policy_arn = aws_iam_policy.slowlog_check.arn
}
resource aws_security_group egress {
name_prefix = "egress-https"
description = "Allow outbound https calls"
vpc_id = var.vpc_id
# https://github.com/hashicorp/terraform/issues/8617#issuecomment-343973544-permalink
lifecycle {
create_before_destroy = true
}
egress {
description = "outbound https"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
resource aws_ssm_parameter datadog_api_key {
name = "/${var.ssm_path}/${local.replication_group}/DATADOG_API_KEY"
description = "Datadog API Key"
tags = var.tags
type = "SecureString"
value = var.datadog_api_key
}
resource aws_ssm_parameter datadog_app_key {
name = "/${var.ssm_path}/${local.replication_group}/DATADOG_APP_KEY"
description = "Datadog App Key"
tags = var.tags
type = "SecureString"
value = var.datadog_app_key
}
resource "aws_lambda_function" "slowlog_check" {
function_name = "slowlog_check_for_${local.replication_group}"
filename = local.slowlog_check_archive_path
source_code_hash = local.slowlog_check_archive_hash
role = aws_iam_role.slowlog_check.arn
handler = "lambda_function.lambda_handler"
runtime = "ruby3.3"
vpc_config {
subnet_ids = var.subnet_ids
security_group_ids = concat([aws_security_group.egress.id], var.elasticache_security_groups)
}
timeout = 600
environment {
variables = {
REDIS_HOST = var.elasticache_endpoint
SSM_PATH = "${var.ssm_path}/${local.replication_group}"
NAMESPACE = var.namespace
ENV = var.env
METRICNAME = var.metric_name
}
}
tags = var.tags
}
resource aws_lambda_function_event_invoke_config slowlog_check {
function_name = aws_lambda_function.slowlog_check.function_name
maximum_retry_attempts = 0
}