generated from terraform-module/terraform-module-blueprint
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
62 lines (49 loc) · 1.73 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
/**
* # AWS Gitlab OIDC Provider Terraform Module
*
* ## Purpose
* This module allows you to create a Gitlab OIDC provider for your AWS account, that will allow Gitlab pipelines to securely authenticate against the AWS API using an IAM role
*
*/
data "tls_certificate" "gitlab" {
url = var.gitlab_tls_url
}
resource "aws_iam_openid_connect_provider" "this" {
count = var.create_oidc_provider ? 1 : 0
client_id_list = var.aud_value
thumbprint_list = [data.tls_certificate.gitlab.certificates[0].sha1_fingerprint]
url = var.url
}
resource "aws_iam_role" "this" {
count = var.create_oidc_provider && 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[*].json)
tags = var.tags
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" {
dynamic "statement" {
for_each = aws_iam_openid_connect_provider.this
content {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
condition {
test = "StringLike"
values = var.project_paths
variable = "${join("", aws_iam_openid_connect_provider.this[*].url)}:${var.match_field}"
}
principals {
identifiers = [statement.value.arn]
type = "Federated"
}
}
}
}