-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
183 lines (165 loc) · 5.67 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
# A random identifier used for naming resources
resource "random_id" "id" {
byte_length = 8
}
# The SNS topic to send notifications to
resource "aws_sns_topic" "fsx_health_sns_topic" {
name = "fsx-health-topic-${random_id.id.hex}"
kms_master_key_id = "alias/aws/sns"
tags = var.tags
}
# SNS subscriptions
resource "aws_sns_topic_subscription" "fsx_health_sns_topic_email_target" {
for_each = toset(var.email)
topic_arn = aws_sns_topic.fsx_health_sns_topic.arn
protocol = "email"
endpoint = each.value
}
# iam policy for lambda role
resource "aws_iam_policy" "fsx_health_lambda_role_policy" {
name = "fsx-health-lambda-role-policy-${random_id.id.hex}"
path = "/"
description = "IAM policy for fsx health solution lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:log-group:/aws/lambda/*",
"Effect": "Allow"
},
{
"Action": [
"fsx:DescribeFileSystems"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"cloudwatch:PutMetricData"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"sns:Publish"
],
"Resource": "${aws_sns_topic.fsx_health_sns_topic.arn}",
"Effect": "Allow"
}
]
}
EOF
tags = var.tags
}
# Log group for the Lambda function
resource "aws_cloudwatch_log_group" "fsx_health_lambda_log_groups" {
name = "/aws/lambda/fsx-health-lambda-function-${random_id.id.hex}"
retention_in_days = var.log_retion_period_in_days
tags = var.tags
}
# IAM role
resource "aws_iam_role" "fsx_health_lambda_role" {
name = "fsx-health-lambda-role-${random_id.id.hex}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "fsx_health_lambda_insights" {
count = var.lambda_insights_layers_arn == null ? 0 : 1
role = aws_iam_role.fsx_health_lambda_role.name
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy"
}
resource "aws_iam_role_policy_attachment" "fsx_health_permissions" {
role = aws_iam_role.fsx_health_lambda_role.name
policy_arn = aws_iam_policy.fsx_health_lambda_role_policy.arn
depends_on = [aws_iam_policy.fsx_health_lambda_role_policy,
aws_iam_role.fsx_health_lambda_role]
}
# Lambda function
resource "aws_lambda_function" "fsx_health_lambda" {
filename = data.archive_file.status_checker_code.output_path
function_name = "fsx-health-lambda-function-${random_id.id.hex}"
description = "Monitor the FSx lifecycle status"
role = aws_iam_role.fsx_health_lambda_role.arn
handler = "index.lambda_handler"
runtime = "python3.12"
memory_size = var.memory_size
timeout = var.timeout
layers = var.lambda_insights_layers_arn == null ? [] : [var.lambda_insights_layers_arn]
tracing_config {
mode = "Active"
}
environment {
variables = {
ENABLE_CLOUDWATCH_METRICS = var.enable_cloudwatch_alarms
ENABLE_SNS_NOTIFICATIONS = var.enable_sns_notifications
FILESYSTEM_IDS = join(",", var.filesystem_ids)
LambdaSNSTopic = aws_sns_topic.fsx_health_sns_topic.arn
SUPPRESS_STATES = join(",", var.ignore_states)
}
}
source_code_hash = data.archive_file.status_checker_code.output_base64sha256
tags = var.tags
}
# eventbridge rule
resource "aws_cloudwatch_event_rule" "fsx_health_lambda_schedule" {
name = "fsx-health-eventbridge-rule-${random_id.id.hex}"
description = "Scheduled execution of the FSx monitor"
schedule_expression = var.schedule_expression
state = "ENABLED"
tags = var.tags
}
resource "aws_cloudwatch_event_target" "fsx_health_lambda_target" {
arn = aws_lambda_function.fsx_health_lambda.arn
rule = aws_cloudwatch_event_rule.fsx_health_lambda_schedule.name
}
resource "aws_lambda_permission" "allow_cw_call_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.fsx_health_lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.fsx_health_lambda_schedule.arn
}
resource "aws_cloudwatch_metric_alarm" "this" {
for_each = toset(local.filesystem_ids)
namespace = "Custom/FSx"
period = 300
metric_name = "Status"
alarm_name = "fsx-status-monitor-${each.key}-${random_id.id.hex}"
comparison_operator = "GreaterThanThreshold"
alarm_description = "This alarm triggers on FSx filesystem status"
evaluation_periods = 2
statistic = "Average"
threshold = 0
treat_missing_data = var.cloudwatch_alarms_treat_missing_data
alarm_actions = var.alarm_actions
insufficient_data_actions = var.insufficient_data_actions
ok_actions = var.ok_actions
dimensions = {
FileSystemId = each.key
}
tags = var.tags
}
locals {
filesystem_ids = var.enable_cloudwatch_alarms ? var.filesystem_ids : []
}