-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.tf
133 lines (116 loc) · 3.93 KB
/
worker.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
/*
* Copyright (c) 2020 Risk Focus Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module "lambda_worker" {
source = "./modules/terraform-aws-lambda"
function_name = "cadmium3-rhodium-worker"
description = "Rhodium Infrastructure Worker"
handler = "rhodium.main.main_cycle"
runtime = "python3.9"
timeout = 300
memory_size = 512
reserved_concurrent_executions = 1
lambda_at_edge = false
vpc_config = var.vpc_config
// Attach a policy.
policy = {
json = data.aws_iam_policy_document.lambda_worker_extra.json
}
s3_bucket_name = "${var.s3_bucket_prefix}-${data.aws_region.current.id}"
s3_bucket_key = var.s3_bucket_path
environment = {
variables = merge(var.environment, var.environment_extra)
}
}
data "aws_iam_policy_document" "lambda_worker_extra" {
statement {
effect = "Allow"
actions = [
"dynamodb:GetItem",
"dynamodb:DeleteItem",
"dynamodb:UpdateItem",
"dynamodb:PutItem",
"dynamodb:BatchWriteItem",
"dynamodb:Scan"
]
resources = [
aws_dynamodb_table.rhodium_environments.arn,
aws_dynamodb_table.rhodium_actions.arn,
aws_dynamodb_table.rhodium_schedules.arn
]
}
statement {
effect = "Allow"
actions = [
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:DescribeInstances",
"ec2:DescribeTags",
"rds:StopDBInstance",
"rds:StopDBCluster",
"rds:StartDBInstance",
"rds:StartDBCluster",
"rds:DescribeDBClusters",
"rds:DescribeDBInstances",
"rds:ListTagsForResource",
"kinesisanalytics:ListApplications",
"kinesisanalytics:ListTagsForResource",
"kinesisanalytics:StartApplication",
"kinesisanalytics:StopApplication",
"autoscaling:DescribeTags",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:UpdateAutoScalingGroup",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"autoscaling:SetDesiredCapacity",
"eks:DescribeCluster",
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
]
resources = [
aws_secretsmanager_secret.rhodium.arn
]
}
}
# This is to optionally manage the CloudWatch Log Group for the Lambda Function.
# If skipping this resource configuration, also add "logs:CreateLogGroup" to the IAM policy below.
resource "aws_cloudwatch_log_group" "lambda_worker" {
name = "/aws/lambda/${module.lambda_worker.function_name}"
retention_in_days = 3
}
resource "aws_cloudwatch_event_rule" "lambda_worker" {
name = "lambda-rhodium-worker"
description = "Trigger Rhodium Worker"
schedule_expression = "rate(1 minute)"
}
resource "aws_cloudwatch_event_target" "lambda_worker" {
rule = aws_cloudwatch_event_rule.lambda_worker.name
target_id = "cadmium3-rhodium-worker"
arn = module.lambda_worker.function_arn
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_rhodium_worker" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = module.lambda_worker.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.lambda_worker.arn
}