generated from DNXLabs/terraform-aws-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.tf
68 lines (64 loc) · 2.1 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
resource "aws_iam_role" "health_lambda_iam" {
name = var.event_rule_name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = { Service = "lambda.amazonaws.com" }
},
]
})
inline_policy {
name = "sns"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = ["sns:Publish"]
Effect = "Allow"
Resource = var.sns_topic_name != "" ? aws_sns_topic.health_event_topic[0].arn : var.sns_topic_arn
},
{
Action = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
Effect = "Allow"
Resource = "arn:aws:logs:*:*:*"
}
]
})
}
}
data "archive_file" "health_lambda" {
type = "zip"
source_file = "${path.module}/health-lambda.py"
output_path = "${path.module}/health-lambda-function-payload.zip"
}
resource "aws_lambda_function" "health_lambda" {
filename = "${path.module}/health-lambda-function-payload.zip"
function_name = var.event_rule_name
role = aws_iam_role.health_lambda_iam.arn
handler = "health-lambda.lambda_handler"
source_code_hash = data.archive_file.health_lambda.output_base64sha256
runtime = "python3.12"
environment {
variables = {
SNS_TOPIC_ARN = var.sns_topic_name != "" ? aws_sns_topic.health_event_topic[0].arn : var.sns_topic_arn
EVENT_RULE_NAME = var.event_rule_name
ALARM_SUBJECT_PREFIX = var.alarm_subject_prefix
}
}
}
resource "aws_cloudwatch_event_target" "health_lambda" {
rule = aws_cloudwatch_event_rule.console.name
target_id = "health_lambda"
arn = aws_lambda_function.health_lambda.arn
}
resource "aws_lambda_permission" "health_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.health_lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.console.arn
}