-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
74 lines (62 loc) · 2.1 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
locals {
gha_assume_role_sub = var.repository_access_type == "branch" ? "repo:${var.repository_name}:refs/heads/${var.repository_access_branch}" : "repo:${var.repository_name}:*"
}
data "aws_caller_identity" "current" {
}
data "aws_iam_openid_connect_provider" "main" {
arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"
}
# Create the IAM role
data "aws_iam_policy_document" "assume_role_policy" {
statement {
effect = "Allow"
principals {
type = "Federated"
identifiers = [data.aws_iam_openid_connect_provider.main.arn]
}
actions = [
"sts:AssumeRoleWithWebIdentity"
]
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = [
"sts.amazonaws.com"
]
}
dynamic "condition" {
for_each = var.custom_repository_identifiers != null ? [true] : []
content {
test = "ForAnyValue:StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = var.custom_repository_identifiers
}
}
dynamic "condition" {
for_each = var.custom_repository_identifiers == null ? [true] : []
content {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = [
local.gha_assume_role_sub
]
}
}
}
}
resource "aws_iam_role" "main" {
name = var.role_name
path = var.role_path
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
resource "aws_iam_role_policy" "customPolicy" {
for_each = length(var.extra_iam_policies) > 0 ? { for name in var.extra_iam_policies : name.policy_name => name } : {}
name = each.value.policy_name
role = aws_iam_role.main.name
policy = each.value.policy_object
}
resource "aws_iam_role_policy_attachment" "customPolicyAttachment" {
for_each = length(var.extra_iam_policy_attachments) > 0 ? toset(var.extra_iam_policy_attachments) : []
role = aws_iam_role.main.name
policy_arn = each.value
}