-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
115 lines (96 loc) · 2.62 KB
/
main.tf
File metadata and controls
115 lines (96 loc) · 2.62 KB
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
data "archive_file" "this" {
type = "zip"
source_file = "${path.module}/cloudflareupdater.py"
output_path = "${path.module}/tmp/cloudflareupdater.zip"
}
data "aws_iam_policy_document" "assume" {
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
name_prefix = "${var.name}-"
assume_role_policy = data.aws_iam_policy_document.assume.json
}
resource "aws_iam_role_policy_attachment" "execution" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.this.name
}
data "aws_iam_policy_document" "this" {
statement {
actions = [
"ec2:AuthorizeSecurityGroupIngress",
"ec2:RevokeSecurityGroupIngress"
]
resources = ["*"]
condition {
test = "StringEquals"
variable = "ec2:ResourceTag/${var.tag_key}"
values = [var.tag_value]
}
}
statement {
actions = ["ec2:DescribeSecurityGroups"]
resources = ["*"]
}
}
resource "aws_iam_policy" "this" {
name_prefix = "${var.name}-"
policy = data.aws_iam_policy_document.this.json
}
resource "aws_iam_role_policy_attachment" "this" {
policy_arn = aws_iam_policy.this.arn
role = aws_iam_role.this.name
}
resource "aws_lambda_function" "this" {
filename = data.archive_file.this.output_path
function_name = "${var.name}-cloudflareupdater"
handler = "cloudflareupdater.lambda_handler"
role = aws_iam_role.this.arn
runtime = "python3.13"
source_code_hash = data.archive_file.this.output_base64sha256
tags = var.tags
timeout = 180
environment {
variables = {
PORTS_LIST = join(",", var.allowed_ports)
TAG_KEY = var.tag_key
TAG_VALUE = var.tag_value
}
}
lifecycle {
ignore_changes = [
filename,
last_modified,
]
}
}
resource "aws_cloudwatch_event_rule" "this" {
name_prefix = "${var.name}-scheduled-rule-"
schedule_expression = var.execution_expression
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_event_target" "this" {
arn = aws_lambda_function.this.arn
rule = aws_cloudwatch_event_rule.this.name
lifecycle {
create_before_destroy = true
}
}
resource "aws_lambda_permission" "this" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.this.arn
lifecycle {
create_before_destroy = true
}
}