-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlambda.tf
151 lines (128 loc) · 3.54 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
resource "aws_iam_role" "lambda" {
name = "${var.prefix}-instanceswitcher"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
path = var.role_path
permissions_boundary = var.role_permissions_boundary
tags = local.tags_base
}
resource "aws_iam_role_policy_attachment" "lambda" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.lambda.name
}
resource "aws_cloudwatch_log_group" "lambda" {
name = "/aws/lambda/${var.prefix}-instanceswitcher"
retention_in_days = 14
tags = local.tags_base
}
# EC2 Actions needed to start/stop instances
resource "aws_iam_policy" "ec2_start_stop" {
name = "${var.prefix}-instanceswitcher-start-stop"
description = "Instance switcher lambda policy to be able to start or stop instances"
path = var.policies_path
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"ec2:StartInstances",
"ec2:StopInstances"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
# EC2 Actions needed to reboot instances
resource "aws_iam_policy" "ec2_reboot" {
name = "${var.prefix}-instanceswitcher-reboot"
description = "Instance switcher lambda policy to reboot instances"
path = var.policies_path
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"ec2:RebootInstances"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
# EC2 Actions needed to terminate instances
resource "aws_iam_policy" "ec2_terminate" {
name = "${var.prefix}-instanceswitcher-terminate"
description = "Instance switcher lambda policy to terminate instances"
path = var.policies_path
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"ec2:TerminateInstances"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "ec2_attach_start_stop" {
count = (length(var.schedules.start) > 0
|| length(var.schedules.stop) > 0
|| length(var.schedules.switch) > 0 ? 1 : 0)
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.ec2_start_stop.arn
}
resource "aws_iam_role_policy_attachment" "ec2_attach_reboot" {
count = (length(var.schedules.reboot) > 0 ? 1 : 0)
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.ec2_reboot.arn
}
resource "aws_iam_role_policy_attachment" "ec2_attach_terminate" {
count = (length(var.schedules.terminate) > 0 ? 1 : 0)
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.ec2_terminate.arn
}
resource "aws_lambda_function" "instanceswitcher" {
filename = data.archive_file.instanceswitcher.output_path
function_name = "${var.prefix}-instanceswitcher"
role = aws_iam_role.lambda.arn
handler = "instanceswitcher.handler"
source_code_hash = filebase64sha256(data.archive_file.instanceswitcher.output_path)
runtime = "python3.8"
tags = local.tags_base
}
data "archive_file" "instanceswitcher" {
type = "zip"
source_dir = "${path.module}/lambda"
output_path = "${path.module}/lambda.zip"
}