-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
58 lines (53 loc) · 1.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
resource "aws_iam_role" "role" {
name = var.name
description = var.description
assume_role_policy = data.aws_iam_policy_document.account_assume_role.json
}
data "aws_iam_policy_document" "account_assume_role" {
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "AWS"
identifiers = [
for account_id in var.account_ids :
// Specifying the root account is the same as just using the account id but this is what
// we get from using the console so use this format so that it doesn't cause a change.
"arn:aws:iam::${account_id}:root"
]
}
condition {
test = "BoolIfExists"
variable = "aws:MultiFactorAuthPresent"
values = [
"true"
]
}
}
}
resource "aws_iam_policy" "assume_role" {
name = "${var.name}AssumeRole"
description = "Assume the ${var.name} role."
policy = data.aws_iam_policy_document.assume_role.json
}
data "aws_iam_policy_document" "assume_role" {
statement {
actions = [
"sts:AssumeRole"
]
resources = [
aws_iam_role.role.arn
]
}
}
resource "aws_iam_group_policy_attachment" "assume_role" {
for_each = var.groups
group = each.value
policy_arn = aws_iam_policy.assume_role.arn
}
resource "aws_iam_role_policy_attachment" "policy" {
for_each = var.policies
role = aws_iam_role.role.name
policy_arn = each.value
}