-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.tf
More file actions
123 lines (100 loc) · 2.8 KB
/
lambda.tf
File metadata and controls
123 lines (100 loc) · 2.8 KB
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
# lambda function golang
# tfsec:ignore:aws-lambda-enable-tracing
resource "aws_lambda_function" "function" {
filename = "lambda.zip"
function_name = "${var.alert_name}-function"
role = aws_iam_role.lambda_role.arn
handler = "main"
runtime = "go1.x"
timeout = 300
memory_size = 128
source_code_hash = data.archive_file.zip.output_base64sha256
environment {
variables = {
RSS_FEED_URL = var.rss_feed_url
HOURS_SINCE = var.hours_since
RSS_FILTER = var.rss_filter
DYNAMODB_TABLE = aws_dynamodb_table.table.name
ALERT_TOPIC = aws_sns_topic.topic.arn
}
}
tags = var.tags
}
resource "aws_cloudwatch_event_rule" "trigger" {
name = "${var.alert_name}-trigger"
description = "Trigger lambda function for ${var.alert_name} at ${var.cron_expression}"
schedule_expression = "cron(${var.cron_expression})"
}
resource "aws_lambda_permission" "allow_cloudwatch" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.function.arn
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.trigger.arn
}
data "archive_file" "zip" {
depends_on = [null_resource.build]
type = "zip"
source_file = "src/main"
output_path = "lambda.zip"
}
# tfsec:ignore:aws-cloudwatch-log-group-customer-key
resource "aws_cloudwatch_log_group" "log_group" {
name = "/aws/lambda/${var.alert_name}-function"
retention_in_days = var.log_retention_days
tags = var.tags
}
###
# IAM
###
resource "aws_iam_role" "lambda_role" {
name = "${var.alert_name}-role"
assume_role_policy = data.aws_iam_policy_document.trust.json
tags = var.tags
}
data "aws_iam_policy_document" "trust" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
# attach policy to role
resource "aws_iam_role_policy_attachment" "policy" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.policy.arn
}
# IAM policy for lambda
resource "aws_iam_policy" "policy" {
name = "${var.alert_name}-policy"
description = "Policy for lambda"
policy = data.aws_iam_policy_document.policy.json
}
# tfsec:ignore:aws-iam-no-policy-wildcards
data "aws_iam_policy_document" "policy" {
statement {
sid = "AllowDynamoDBGetPut"
actions = [
"dynamodb:PutItem",
"dynamodb:GetItem"
]
resources = [aws_dynamodb_table.table.arn]
}
statement {
sid = "AllowSNSNotification"
actions = [
"sns:Publish"
]
resources = [aws_sns_topic.topic.arn]
}
statement {
sid = "AllowCloudWatchLogs"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
}