generated from plus3it/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
133 lines (115 loc) · 3.31 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
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "aws_partition" "current" {}
locals {
id = data.terraform_remote_state.prereq.outputs.test_id.result
project = "${var.project}-${local.id}"
tags = {
"broker_managed" = true
"contact" = var.contact_email
"project" = local.project
}
}
module "iam_key_enforcer" {
source = "../.."
project_name = local.project
accounts = [
{
account_name = var.account_name
account_number = data.aws_caller_identity.current.account_id
armed = false
debug = true
email_user_enabled = true
email_targets = ["[email protected]"]
exempt_groups = var.exempt_groups
schedule_expression = "rate(10 minutes)"
}
]
assume_role_name = aws_iam_role.assume_role.name
admin_email = "[email protected]"
email_admin_report_enabled = true
email_source = var.email_source
email_banner_message = "IAM Key Enforcement will be armed on 07/31/2023"
email_banner_message_color = "red"
# email_templates = {
# # admin = {
# # subject = "IAM Key Enforcement Report for {{account_number}}"
# # html = "HTML"
# # }
# # user = {
# # subject = "IAM User Key {{armed_state_msg}} for {{user_name}}"
# # }
# }
key_age_delete = var.key_age_delete
key_age_inactive = var.key_age_inactive
key_use_threshold = var.key_use_threshold
key_age_warning = var.key_age_warning
log_level = "DEBUG"
s3_bucket = aws_s3_bucket.this.id
s3_enabled = var.s3_enabled
tags = local.tags
}
resource "aws_s3_bucket" "this" {
bucket = "${local.project}-bucket"
tags = local.tags
force_destroy = true
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
data "aws_iam_policy_document" "iam_key" {
statement {
actions = [
"iam:GenerateCredentialReport",
"iam:GetCredentialReport",
"iam:ListUsers",
"iam:GetAccessKeyLastUsed"
]
resources = [
"*"
]
}
statement {
actions = [
"iam:DeleteAccessKey",
"iam:ListGroupsForUser",
"iam:UpdateAccessKey",
"iam:ListAccessKeys",
"iam:ListUserTags",
]
resources = [
"arn:${data.aws_partition.current.partition}:iam::*:user/*"
]
}
}
resource "aws_iam_policy" "iam_policy" {
name = "${local.project}-policy"
policy = data.aws_iam_policy_document.iam_key.json
}
resource "aws_iam_role" "assume_role" {
name = "${local.project}-role"
managed_policy_arns = [aws_iam_policy.iam_policy.arn]
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Sid" : "AssumeRoleCrossAccount",
"Effect" : "Allow",
"Principal" : {
"AWS" : "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action" : "sts:AssumeRole"
}
]
})
}
data "terraform_remote_state" "prereq" {
backend = "local"
config = {
path = "prereq/terraform.tfstate"
}
}