-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
55 lines (42 loc) · 1.27 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
locals {
have_repositories = length(var.github_repositories) > 0
plain_oidc_url = trimprefix(var.github_actions_oidc_url, "https://")
thumbprint = data.tls_certificate.github_actions_oidc_endpoint.certificates[0].sha1_fingerprint
}
resource "aws_iam_openid_connect_provider" "github" {
count = local.have_repositories ? 1 : 0
url = var.github_actions_oidc_url
client_id_list = [
"sts.amazonaws.com"
]
thumbprint_list = [
local.thumbprint
]
tags = var.tags
}
data "aws_iam_policy_document" "federated_assume_policy" {
count = local.have_repositories ? 1 : 0
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
principals {
type = "Federated"
identifiers = [
aws_iam_openid_connect_provider.github[0].arn
]
}
condition {
test = "StringLike"
variable = "${local.plain_oidc_url}:sub"
values = [for repo in var.github_repositories : "repo:${repo}:*"]
}
}
}
resource "aws_iam_role" "federated_auth_role" {
for_each = var.role_names
name = each.key
path = var.role_path
description = "Federated identity role for GitHub Actions"
assume_role_policy = data.aws_iam_policy_document.federated_assume_policy[0].json
tags = var.tags
}