-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
135 lines (107 loc) · 3.15 KB
/
main.tf
File metadata and controls
135 lines (107 loc) · 3.15 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
resource "aws_security_group" "lambda_sg" {
name = "${var.name}-sg"
description = "Security group for Lambda to access Aurora RDS"
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
resource "aws_iam_role" "lambda_execution_role" {
name = "${var.name}-role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
tags = var.tags
}
data "aws_iam_policy_document" "lambda_assume_role_policy" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
data "aws_iam_policy_document" "lambda_policy" {
statement {
effect = "Allow"
actions = [
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:DescribeTargetHealth",
"rds:ListTagsForResource",
"rds:DescribeDBInstances",
"rds:DescribeDBClusters",
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
statement {
effect = "Allow"
actions = [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface"
]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "lambda_policy" {
name = "${var.name}-policy"
role = aws_iam_role.lambda_execution_role.id
policy = data.aws_iam_policy_document.lambda_policy.json
}
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "${path.module}/lambda_function.py"
output_path = "${path.module}/lambda_function.zip"
}
resource "aws_lambda_function" "aurora_nlb" {
function_name = "${var.name}-updater"
handler = "lambda_function.lambda_handler"
runtime = "python3.12"
role = aws_iam_role.lambda_execution_role.arn
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
vpc_config {
subnet_ids = var.vpc_subnet_ids
security_group_ids = [aws_security_group.lambda_sg.id]
}
environment {
variables = {
DB_IDENTIFIER = var.identifier
TARGET_GROUP_ARN = var.target_group_arn
TARGET_PORT = var.target_port
TYPE = var.type
}
}
tags = var.tags
}
resource "aws_cloudwatch_event_rule" "every_minute" {
name = "${var.name}-minute"
schedule_expression = "rate(1 minute)"
tags = var.tags
}
resource "aws_cloudwatch_event_target" "lambda_target" {
rule = aws_cloudwatch_event_rule.every_minute.name
target_id = "${var.name}-lambda"
arn = aws_lambda_function.aurora_nlb.arn
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.aurora_nlb.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.every_minute.arn
}