forked from ricardoteix/terraform-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.tf
166 lines (154 loc) · 5.23 KB
/
lambda.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
# Faz upload do código da função Lambda para a AWS
resource "aws_lambda_function" "zabbix-add-host" {
function_name = "zabbix-add-host"
handler = "lambda_function.lambda_handler"
publish = true
runtime = "python3.9"
timeout = 15
role = aws_iam_role.zabbix-lambda-role.arn
filename = data.archive_file.lambda_function.output_path
source_code_hash = data.archive_file.lambda_function.output_base64sha256
environment {
variables = {
SECRET_NAME = aws_secretsmanager_secret.zabbix-user-secret-key.name,
}
}
}
# Cria uma política de segurança que permite a função Lambda acessar o Secret Manager "zabbix-user-secret"
resource "aws_iam_policy" "zabbix-user-secret-access-policy" {
name = "zabbix-user-secret-access-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"secretsmanager:GetSecretValue"
]
Resource = aws_secretsmanager_secret.zabbix-user-secret-key.arn
}
]
})
}
# Cria uma política de segurança que permite a função Lambda publicar logs no CloudWatch
resource "aws_iam_policy" "zabbix-log-policy" {
name = "zabbix-log-policy"
policy = jsonencode(
{
Version = "2012-10-17"
Statement = [
{
Effect = "Allow",
Action = "logs:CreateLogGroup",
Resource = "arn:aws:logs:${var.regiao}:${var.conta-aws}:*"
},
{
Effect = "Allow",
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
Resource = [
"arn:aws:logs:${var.regiao}:${var.conta-aws}:log-group:/aws/lambda/${aws_lambda_function.zabbix-add-host.function_name}:*"
]
}
]
})
}
# Cria uma política de segurança que permite a função Lambda acessar recursos do EC2
resource "aws_iam_policy" "zabbix-ec2-policy" {
name = "zabbix-ec2-policy"
policy = jsonencode(
{
Version = "2012-10-17"
Statement = [
{
Sid = "VisualEditor0",
Effect = "Allow",
Action = [
"ec2:DescribeInstances",
"ec2:TerminateInstances",
"ec2:DescribeTags",
"ec2:ModifyInstanceAttribute"
],
Resource = "*"
},
{
Sid = "VisualEditor1",
Effect = "Allow",
Action = "ssm:SendCommand",
Resource = "arn:aws:ec2:${var.regiao}:${var.conta-aws}:instance/*"
}
]
})
}
# Cria uma função IAM que permite a função Lambda acessar o Secret Manager "zabbix-user-secret"
resource "aws_iam_role" "zabbix-lambda-role" {
name = "zabbix-lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
# Anexa a política de segurança à função IAM
resource "aws_iam_role_policy_attachment" "zabbix-user-secret-access-policy-attachment" {
policy_arn = aws_iam_policy.zabbix-user-secret-access-policy.arn
role = aws_iam_role.zabbix-lambda-role.name
}
# Anexa a política de segurança à função IAM
resource "aws_iam_role_policy_attachment" "zabbix-log-policy-attachment" {
policy_arn = aws_iam_policy.zabbix-log-policy.arn
role = aws_iam_role.zabbix-lambda-role.name
}
# Anexa a política de segurança à função IAM
resource "aws_iam_role_policy_attachment" "zabbix-ec2-policy-attachment" {
policy_arn = aws_iam_policy.zabbix-ec2-policy.arn
role = aws_iam_role.zabbix-lambda-role.name
}
# Define o código da função Lambda
data "archive_file" "lambda_function" {
type = "zip"
source_dir = "${path.module}/lambda"
output_path = "${path.module}/zabbix-add-host.zip"
}
# Cria uma regra de evento que é ativada sempre que uma nova instância EC2 é criada
resource "aws_cloudwatch_event_rule" "detect-ec2-instance-rule" {
name = "detect-ec2-instance-rule"
description = "Trigger zabbix-add-host function on new EC2 instance creation"
event_pattern = jsonencode(
{
source = [
"aws.ec2"
],
detail-type = [
"EC2 Instance State-change Notification"
],
detail = {
state = [
"pending"
]
}
}
)
}
# Configura a permissão da função Lambda para ser invocada pela regra de evento
resource "aws_lambda_permission" "event_rule_lambda_permission" {
statement_id = "AllowExecutionFromCloudWatchEvent"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.zabbix-add-host.arn
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.detect-ec2-instance-rule.arn
}
# Associa a regra de evento à função Lambda
resource "aws_cloudwatch_event_target" "zabbix_add_host_lambda_target" {
rule = aws_cloudwatch_event_rule.detect-ec2-instance-rule.name
arn = aws_lambda_function.zabbix-add-host.arn
}