-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.tf
128 lines (119 loc) · 3.65 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
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
locals {
scheduler_actions = {
stop = var.stop_cron_schedule
start = var.start_cron_schedule
}
}
#--------------- Lambda---------------------------------------------------------
resource "aws_lambda_function" "ec2" {
for_each = local.scheduler_actions
function_name = "${var.name}-to-${each.key}"
description = "Lambda to ${each.key} EC2 Instances with specific Tag"
role = aws_iam_role.lambda.arn
runtime = "python3.9"
handler = "lambda_function.lambda_handler"
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
tags = var.tags
timeout = 10
environment {
variables = {
EC2TAG_KEY = var.stopstart_tags["TagKEY"]
EC2TAG_VALUE = var.stopstart_tags["TagVALUE"]
EC2_ACTION = upper(each.key)
}
}
}
data "archive_file" "lambda_zip" {
type = "zip"
output_path = "lambda_function.zip"
source {
filename = "lambda_function.py"
content = file("${path.module}/main.py")
}
}
#--------------- Lambda IAM Permissions-----------------------------------------
resource "aws_iam_role" "lambda" {
name = "${var.name}-iam-role"
tags = var.tags
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["lambda.amazonaws.com"]
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_policy" "lambda" {
name = "${var.name}-policy"
tags = var.tags
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid" : "LoggingPermissions",
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:*:*:*"
]
},
{
"Sid" : "WorkPermissions",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:StopInstances",
"ec2:StartInstances"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_policy_attachment" "lambda" {
name = "${var.name}-role-policy-attach"
roles = [aws_iam_role.lambda.name]
policy_arn = aws_iam_policy.lambda.arn
}
#---------------Logging---------------------------------------------------------
resource "aws_cloudwatch_log_group" "lambda" {
for_each = local.scheduler_actions
name = "/aws/lambda/${var.name}-to-${each.key}"
retention_in_days = 7
tags = var.tags
}
#--------------- Lambda Triggers------------------------------------------------
resource "aws_cloudwatch_event_rule" "ec2" {
for_each = local.scheduler_actions
name = "${var.name}-trigger-to-${each.key}-ec2"
description = "Invoke Lambda via AWS EventBridge"
schedule_expression = each.value
tags = var.tags
}
resource "aws_lambda_permission" "ec2" {
for_each = local.scheduler_actions
statement_id = "AllowExecutionFromEventBridge"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ec2[each.key].function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.ec2[each.key].arn
}
resource "aws_cloudwatch_event_target" "ec2" {
for_each = local.scheduler_actions
rule = aws_cloudwatch_event_rule.ec2[each.key].name
arn = aws_lambda_function.ec2[each.key].arn
}