-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
56 lines (47 loc) · 1.66 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
locals {
github_provider_url = "https://token.actions.githubusercontent.com"
}
data "aws_iam_openid_connect_provider" "provider" {
count = var.create_oidc_provider ? 0 : 1
url = local.github_provider_url
}
resource "aws_iam_openid_connect_provider" "provider" {
count = var.create_oidc_provider ? 1 : 0
client_id_list = [
"sts.amazonaws.com",
]
thumbprint_list = var.github_thumbprints
url = local.github_provider_url
}
resource "aws_iam_role" "github_actions_role" {
name = var.role_name
description = "Role assumed by GitHub Actions"
max_session_duration = var.max_session_duration
assume_role_policy = data.aws_iam_policy_document.policy_document.json
depends_on = [aws_iam_openid_connect_provider.provider]
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "attach" {
count = length(var.oidc_role_policies_arns)
policy_arn = var.oidc_role_policies_arns[count.index]
role = aws_iam_role.github_actions_role.name
depends_on = [aws_iam_role.github_actions_role]
}
data "aws_iam_policy_document" "policy_document" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
condition {
test = "StringLike"
values = [
for repo in var.repositories :
"repo:%{if length(regexall(":+", repo)) > 0}${repo}%{else}${repo}:*%{endif}"
]
variable = "token.actions.githubusercontent.com:sub"
}
principals {
identifiers = [var.create_oidc_provider ? aws_iam_openid_connect_provider.provider[0].arn : data.aws_iam_openid_connect_provider.provider[0].arn]
type = "Federated"
}
}
}