This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
124 lines (109 loc) · 3.06 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
resource "aws_lambda_function" "asa-cleaner" {
filename = "${path.module}/files/lambda/asa-cleaner.zip"
function_name = "${var.env}-asa-cleaner"
role = aws_iam_role.asa-cleaner.arn
handler = "asa_cleaner.lambda_handler"
source_code_hash = "${filebase64sha256("${path.module}/files/lambda/asa-cleaner.zip")}"
runtime = "ruby2.7"
timeout = "600"
environment {
variables = {
ENVIRONMENT = var.env
ASA_API_KEY_PATH = var.asa_api_key_path
ASA_API_SECRET_PATH = var.asa_api_secret_path
ASA_TEAM = var.asa_team
}
}
}
resource "aws_cloudwatch_event_rule" "instance_terminated_events" {
name = "${var.env}-asa-capture-ec2-terminated-events"
description = "Capture all EC2 instance terminated events"
event_pattern = <<PATTERN
{
"source": [
"aws.ec2"
],
"detail-type": [
"EC2 Instance State-change Notification"
],
"detail": {
"state": [
"terminated"
]
}
}
PATTERN
}
resource "aws_cloudwatch_event_target" "asa-cleaner" {
rule = aws_cloudwatch_event_rule.instance_terminated_events.name
arn = aws_lambda_function.asa-cleaner.arn
}
resource "aws_lambda_permission" "events" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.asa-cleaner.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.instance_terminated_events.arn
}
resource "aws_iam_role" "asa-cleaner" {
name = "${var.env}-asa-cleaner"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_policy" "asa-cleaner" {
name = "${var.env}-asa-cleaner"
description = "Allow asa-cleaner lambda to write CloudWatch logs and read SSM secrets."
policy = data.aws_iam_policy_document.asa-cleaner.json
}
data "aws_iam_policy_document" "asa-cleaner" {
statement {
sid = "CreateLogGroup"
effect = "Allow"
actions = ["logs:CreateLogGroup"]
resources = ["arn:aws:logs:*:*:*"]
}
statement {
sid = "CreatePutLogEvents"
effect = "Allow"
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
resources = [
"arn:aws:logs:**:*:log-group:/aws/lambda/${var.env}-asa-cleaner:*",
]
}
statement {
sid = "DescribeAllParameters"
effect = "Allow"
actions = ["ssm:DescribeParameters"]
resources = ["*"]
}
statement {
sid = "GetApiParams"
effect = "Allow"
actions = ["ssm:GetParameter*"]
resources = [
"arn:aws:ssm:*:*:parameter${var.asa_api_key_path}",
"arn:aws:ssm:*:*:parameter${var.asa_api_secret_path}",
]
}
statement {
sid = "DecryptParams"
effect = "Allow"
actions = ["kms:Decrypt"]
resources = ["${var.kms_key_arn}"]
}
}
resource "aws_iam_role_policy_attachment" "attach-asa-cleaner" {
role = aws_iam_role.asa-cleaner.name
policy_arn = aws_iam_policy.asa-cleaner.arn
}