-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.tf
116 lines (103 loc) · 3.6 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
resource "random_pet" "this" {
length = 2
}
/* TODO: WIP
module "cljs_ec2_layer_s3" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 1.44"
create_layer = true
layer_name = "cljs-layer-s3"
description = CLJS lambda layer from S3"
compatible_runtimes = ["nodejs14"]
source_path = ["${path.module}/dist/lambda"] # "../src/lambda-layer"
store_on_s3 = true
s3_bucket = "cljs-with-lambda-builds"
}
*/
module "cljs_ec2_stop" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 1.44"
function_name = "cljs-stop-${random_pet.this.id}"
publish = true
description = "🧪 Stop EC2 Instances - ClojureScript 🥼"
handler = "index.stopInstances"
runtime = "nodejs14.x"
source_path = ["${path.module}/dist/lambda"]
hash_extra = "quickfix-stop"
environment_variables = var.environment
attach_policy_statements = true
#layers = [
# module.cljs_ec2_layer_s3.this_lambda_layer_arn,
#]
policy_statements = {
ec2 = {
effect = "Allow",
actions = ["ec2:Stop*", "ec2:Describe*"],
resources = ["*"]
}
}
allowed_triggers = zipmap(keys(var.stops), [for s in keys(var.stops) : {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.stop_instances[s].arn
}])
}
// TODO: Appears the same hash confuses terraform module
module "cljs_ec2_start" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 1.44"
function_name = "cljs-start-${random_pet.this.id}"
publish = true
description = "🧫 Start EC2 Instances - ClojureScript 🥼"
handler = "index.startInstances"
runtime = "nodejs14.x"
# create_layer = true
source_path = ["${path.module}/dist/lambda"]
hash_extra = "quickfix-start"
environment_variables = var.environment
# attach_cloudwatch_logs_policy = false
# use_existing_cloudwatch_log_group = true
# independent_file_timestamps = true
# store_on_s3 = true
# s3_bucket = module.s3_bucket.this_s3_bucket_id
attach_policy_statements = true
policy_statements = {
ec2 = {
effect = "Allow",
actions = ["ec2:Start*", "ec2:Describe*"],
resources = ["*"]
},
#s3 = {
# effect = "Allow",
# actions = ["s3:LIstAllMyBuckets"],
# resources = ["arn:aws:s3:::*"]
#}
}
allowed_triggers = zipmap(keys(var.starts), [for s in keys(var.starts) : {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.start_instances[s].arn
}])
}
resource "aws_cloudwatch_event_rule" "stop_instances" {
for_each = var.stops
name = "EC2StopInstancesEvent-${each.key}"
description = "EC2 Stop Instances Event"
schedule_expression = each.value.schedule
}
resource "aws_cloudwatch_event_target" "stop_instances" {
for_each = var.stops
rule = aws_cloudwatch_event_rule.stop_instances[each.key].name
arn = module.cljs_ec2_stop.this_lambda_function_arn
input = each.value.event
}
resource "aws_cloudwatch_event_rule" "start_instances" {
for_each = var.starts
name = "EC2StartInstancesEvent-${each.key}"
description = "EC2 Start Instances Event"
schedule_expression = each.value.schedule
}
resource "aws_cloudwatch_event_target" "start_instances" {
for_each = var.starts
rule = aws_cloudwatch_event_rule.start_instances[each.key].name
arn = module.cljs_ec2_start.this_lambda_function_arn
input = each.value.event
}