-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
57 lines (46 loc) · 1.51 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
locals {
organization = "observeinc"
repository = "terraform-aws-collection"
}
data "aws_iam_openid_connect_provider" "github_actions" {
url = "https://token.actions.githubusercontent.com"
}
locals {
oidc_claim_prefix = trimprefix(data.aws_iam_openid_connect_provider.github_actions.url, "https://")
}
data "aws_iam_policy_document" "github_actions_assume_role" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [data.aws_iam_openid_connect_provider.github_actions.arn]
}
condition {
test = "StringLike"
variable = "${local.oidc_claim_prefix}:sub"
values = ["repo:${local.organization}/${local.repository}:*"]
}
condition {
test = "StringEquals"
variable = "${local.oidc_claim_prefix}:aud"
values = ["sts.amazonaws.com"]
}
}
}
resource "aws_iam_role" "github_actions_ci" {
name = "${local.repository}-gha-ci"
assume_role_policy = data.aws_iam_policy_document.github_actions_assume_role.json
tags = {
Principal = "GitHub Actions"
Repository = "${local.organization}/${local.repository}"
}
}
resource "aws_iam_role_policy_attachment" "admin_policy_attachment" {
role = aws_iam_role.github_actions_ci.name
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}
resource "github_actions_secret" "aws_ci_role" {
repository = local.repository
secret_name = "AWS_ROLE_ARN"
plaintext_value = aws_iam_role.github_actions_ci.arn
}