-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
129 lines (113 loc) · 3.62 KB
/
main.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
resource "aws_iam_role" "lambda_exec_role" {
name = "ro_set_tags_${var.rds_cluster_identifier}"
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 {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "lambda_permissions" {
role = aws_iam_role.lambda_exec_role.name
policy = data.aws_iam_policy_document.lambda_permissions_policy.json
}
data "aws_iam_policy_document" "lambda_permissions_policy" {
statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"rds-data:*",
"rds:*",
]
resources = ["*"]
}
}
# Build the Go binary and create zip file
resource "null_resource" "lambda_builder" {
# Trigger rebuild on code changes
triggers = {
code_hash = sha256(join("", [
for f in fileset("${path.module}/setter", "**/*") : filesha256("${path.module}/setter/${f}") if !contains([
"main.zip",
"bootstrap",
".gitignore",
], f)
]))
}
provisioner "local-exec" {
working_dir = "${path.module}/setter"
command = <<EOT
# Check if go and make are installed
if ! command -v go &> /dev/null; then
echo "go command not found, please install Go"
exit 1
fi
if ! command -v make &> /dev/null; then
echo "make command not found, please install Make"
exit 1
fi
# If both commands are found, run the build and package steps
make clean && make build && make package
EOT
}
}
# Create zip file for Lambda
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "${path.module}/setter/bootstrap"
output_path = "${path.module}/setter/main.zip"
depends_on = [null_resource.lambda_builder]
}
resource "aws_lambda_function" "lambda" {
filename = data.archive_file.lambda_zip.output_path
function_name = "ro_set_tags_${var.rds_cluster_identifier}"
role = aws_iam_role.lambda_exec_role.arn
handler = "HandleRequest"
memory_size = 128
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
runtime = "provided.al2"
tags = merge(var.tags, {
Name = "ro_set_tags_${var.rds_cluster_identifier}"
})
environment {
variables = {
TAGS = jsonencode(var.push_tags),
RDS_CLUSTER_IDENTIFIER = var.rds_cluster_identifier,
}
}
lifecycle {
ignore_changes = [
last_modified,
]
}
}
resource "aws_cloudwatch_event_rule" "read_replica_created" {
name = "ro_set_tags_${var.rds_cluster_identifier}"
description = "Trigger Lambda when instance is created in ${var.rds_cluster_identifier}"
event_pattern = jsonencode({
"source" : ["aws.rds"],
"detail-type" : ["RDS DB Instance Event"],
"detail" : {
"EventID" : ["RDS-EVENT-0005"] # Event: "DB instance created"
}
})
}
resource "aws_lambda_permission" "allow_eventbridge" {
statement_id = "ro_set_tags_${var.rds_cluster_identifier}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.read_replica_created.arn
}
resource "aws_cloudwatch_event_target" "read_replica_target" {
rule = aws_cloudwatch_event_rule.read_replica_created.name
target_id = "ro_set_tags_${var.rds_cluster_identifier}"
arn = aws_lambda_function.lambda.arn
}