-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
155 lines (127 loc) · 3.43 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
archive = {
source = "hashicorp/archive"
}
}
}
variable "low_threshold_days" {
default = 90
}
variable "high_threshold_days" {
default = 180
}
variable "dry_run" {
default = true
}
variable "whitelist" {
default = ""
}
variable "schedule" {
default = "rate(1 hour)"
}
variable "lambda_timeout" {
default = 15
}
variable "lambda_memory_size" {
default = 128
}
data "aws_caller_identity" "this" {}
data "aws_region" "this" {}
data "archive_file" "this" {
type = "zip"
source_file = "${path.module}/aws-iam-access-key-disabler"
output_path = "${path.module}/aws-iam-access-key-disabler.zip"
}
resource "aws_lambda_function" "this" {
filename = data.archive_file.this.output_path
function_name = "aws-iam-access-key-disabler"
role = aws_iam_role.this.arn
handler = "aws-iam-access-key-disabler"
source_code_hash = filebase64sha256(data.archive_file.this.output_path)
runtime = "go1.x"
timeout = var.lambda_timeout
memory_size = var.lambda_memory_size
environment {
variables = {
LOWTHRESHOLDDAYS = var.low_threshold_days
HIGHTHRESHOLDDAYS = var.high_threshold_days
DRYRUN = var.dry_run
WHITELIST = var.whitelist
}
}
}
resource "aws_lambda_permission" "this" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.this.arn
}
resource "aws_iam_role" "this" {
name = "aws-iam-access-key-disabler"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "this" {
name = "aws-iam-access-key-disabler"
path = "/"
description = "Policy allowing lambda function aws-iam-access-key-disabler to disable IAM keys."
policy = data.aws_iam_policy_document.this.json
}
resource "aws_iam_role_policy_attachment" "this-attachment" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.this.arn
}
data "aws_iam_policy_document" "this" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
]
resources = ["arn:aws:logs:${data.aws_region.this.name}:${data.aws_caller_identity.this.account_id}:*"]
}
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = ["arn:aws:logs:${data.aws_region.this.name}:${data.aws_caller_identity.this.account_id}:log-group:/aws/lambda/aws-iam-access-key-disabler:*"]
}
statement {
effect = "Allow"
actions = [
"iam:UpdateAccessKey",
"iam:ListUsers",
"iam:ListAccessKeys",
]
resources = ["*"]
}
}
resource "aws_cloudwatch_event_rule" "this" {
name = "aws-iam-access-key-disabler"
description = "Event rule for scheduling aws-iam-access-key-disabler lambda function."
schedule_expression = "${var.schedule}"
}
resource "aws_cloudwatch_event_target" "this" {
rule = aws_cloudwatch_event_rule.this.name
target_id = "aws-iam-access-key-disabler"
arn = aws_lambda_function.this.arn
}