-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor_stage.tf
164 lines (143 loc) · 4.13 KB
/
monitor_stage.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
data "archive_file" "monitor_stage_zip" {
type = "zip"
source_file = "${local.path_module}/lambda/code/monitor_stage.py"
output_path = "${local.path_module}/lambda/package/monitor_stage.zip"
}
resource "aws_lambda_function" "monitor_stage" {
count = var.namespace == "prod" ? "1" : "0"
filename = "${path.module}/lambda/package/monitor_stage.zip"
function_name = "${var.pipeline_name}-${var.namespace}-monitor-stage"
role = aws_iam_role.monitor_stage[0].arn
handler = "monitor_stage.lambda_handler"
source_code_hash = data.archive_file.monitor_stage_zip.output_base64sha256
runtime = "python3.7"
timeout = "900"
memory_size = "128"
tags = {
Name = "monitor-stage-${local.naming_suffix}"
}
# lifecycle {
# ignore_changes = [
# filename,
# last_modified,
# source_code_hash,
# ]
# }
}
resource "aws_iam_role" "monitor_stage" {
count = var.namespace == "prod" ? "1" : "0"
name = "${var.pipeline_name}-${var.namespace}-monitor-stage"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = {
Name = "monitor-stage-${local.naming_suffix}"
}
}
resource "aws_iam_policy" "monitor_stage" {
count = var.namespace == "prod" ? "1" : "0"
name = "${var.pipeline_name}-monitor-stage"
path = "/"
description = "IAM policy Monitor-stage function"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"rds:Describe*"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "monitor_stage" {
count = var.namespace == "prod" ? "1" : "0"
role = aws_iam_role.monitor_stage[0].name
policy_arn = aws_iam_policy.monitor_stage[0].arn
}
resource "aws_cloudwatch_log_group" "lambda_monitor_stage" {
count = var.namespace == "prod" ? "1" : "0"
name = "/aws/lambda/${aws_lambda_function.monitor_stage[0].function_name}"
retention_in_days = 14
tags = {
Name = "monitor-stage-${local.naming_suffix}"
}
}
resource "aws_iam_policy" "lambda_monitor_stage_logging" {
count = var.namespace == "prod" ? "1" : "0"
name = "${var.pipeline_name}-monitor-stage-logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"cloudwatch:GetMetricStatistics",
"logs:DescribeLogStreams",
"logs:GetLogEvents",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"${aws_cloudwatch_log_group.lambda_monitor_stage[0].arn}",
"${aws_cloudwatch_log_group.lambda_monitor_stage[0].arn}/*"
],
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_monitor_stage_logs" {
count = var.namespace == "prod" ? "1" : "0"
role = aws_iam_role.monitor_stage[0].name
policy_arn = aws_iam_policy.lambda_monitor_stage_logging[0].arn
}
resource "aws_iam_policy" "lambda_monitor_stage_send_slack" {
count = var.namespace == "prod" ? "1" : "0"
name = "${var.pipeline_name}-monitor-stage-slack"
path = "/"
description = "IAM policy to allow sending slack alerts"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ssm:GetParameter",
"ssm:GetParameters"
],
"Effect": "Allow",
"Resource": [
"arn:aws:ssm:${var.region}:${var.account_id[var.namespace]}:parameter/slack_notification_webhook"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_monitor_stage_slack" {
count = var.namespace == "prod" ? "1" : "0"
role = aws_iam_role.monitor_stage[0].name
policy_arn = aws_iam_policy.lambda_monitor_stage_send_slack[0].arn
}