-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
95 lines (83 loc) · 2.25 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
provider "archive" {}
data "archive_file" "zip" {
type = "zip"
source_file = "batch.py"
output_path = "batch.zip"
}
resource "aws_cloudwatch_event_rule" "cron" {
name = "cron"
description = "each day cron"
schedule_expression = "rate(1440 minutes)"
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "lambda_logging" {
name = "lambda_logging"
path = "/"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = "${aws_iam_role.iam_for_lambdas.name}"
policy_arn = "${aws_iam_policy.lambda_logging.arn}"
}
resource "aws_lambda_function" "test_lambda" {
function_name = "lambda_function_batch"
role = "${aws_iam_role.iam_for_lambda.arn}"
filename = "${data.archive_file.zip.output_path}"
source_code_hash = "${data.archive_file.zip.output_base64sha256}"
handler = "batch.lambda_handler"
runtime = "python3.7"
environment {
variables = {
REGION = "eu-central-1"
}
}
}
#to add this variable later
resource "aws_cloudwatch_log_group" "log_group_batch" {
name = "/aws/lambda/{var.lambda.function_name}"
retention_in_days = 14
}
resource "aws_cloudwatch_event_target" "cron" {
rule = "${aws_cloudwatch_event_rule.cron.name}"
target_id = "test_lambda"
arn = "${aws_lambda_function.test_lambda.arn}"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_func_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.test_lambda.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.cron.arn}"
}