generated from terraform-module/terraform-module-blueprint
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.tf
59 lines (51 loc) · 1.82 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
/**
* # AWS Github OIDC Provider Terraform Module
*
* ## Purpose
* This module allows you to create a Github OIDC provider for your AWS account, that will help Github Actions to securely authenticate against the AWS API using an IAM role
*
*/
resource "aws_iam_openid_connect_provider" "this" {
count = var.create_oidc_provider ? 1 : 0
client_id_list = [
"sts.amazonaws.com",
]
thumbprint_list = [var.github_thumbprint]
url = "https://token.actions.githubusercontent.com"
}
resource "aws_iam_role" "this" {
count = var.create_oidc_role ? 1 : 0
name = var.role_name
description = var.role_description
max_session_duration = var.max_session_duration
assume_role_policy = join("", data.aws_iam_policy_document.this[0].*.json)
tags = var.tags
# path = var.iam_role_path
# permissions_boundary = var.iam_role_permissions_boundary
depends_on = [aws_iam_openid_connect_provider.this]
}
resource "aws_iam_role_policy_attachment" "attach" {
count = var.create_oidc_role ? length(var.oidc_role_attach_policies) : 0
policy_arn = var.oidc_role_attach_policies[count.index]
role = join("", aws_iam_role.this.*.name)
depends_on = [aws_iam_role.this]
}
data "aws_iam_policy_document" "this" {
count = var.create_oidc_role ? 1 : 0
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 = [try(aws_iam_openid_connect_provider.this[0].arn, var.oidc_provider_arn)]
type = "Federated"
}
}
}