# workload-identity / aws / t1 "Standard" # # Short-lived AWS access for GitHub Actions, limited to named branches or # deployment environments, and optionally for one Kubernetes service account # through EKS Pod Identity. No stored keys anywhere. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) github_url = "https://token.actions.githubusercontent.com" provider_arn = var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : data.aws_iam_openid_connect_provider.github[0].arn boundary = var.permissions_boundary_arn == "" ? null : var.permissions_boundary_arn } resource "aws_iam_openid_connect_provider" "github" { count = var.create_oidc_provider ? 1 : 0 url = local.github_url client_id_list = ["sts.amazonaws.com"] } data "aws_iam_openid_connect_provider" "github" { count = var.create_oidc_provider ? 0 : 1 url = local.github_url } data "aws_iam_policy_document" "github_trust" { statement { actions = ["sts:AssumeRoleWithWebIdentity"] principals { type = "Federated" identifiers = [local.provider_arn] } condition { test = "StringEquals" variable = "token.actions.githubusercontent.com:aud" values = ["sts.amazonaws.com"] } # StringEquals, and the variable's validation refuses wildcards in the # organisation or repository, so a typo cannot widen this to all of GitHub. condition { test = "StringEquals" variable = "token.actions.githubusercontent.com:sub" values = var.allowed_subjects } } } resource "aws_iam_role" "github" { name = var.name assume_role_policy = data.aws_iam_policy_document.github_trust.json max_session_duration = 3600 permissions_boundary = local.boundary } resource "aws_iam_role_policy_attachment" "github" { for_each = toset(var.policy_arns) role = aws_iam_role.github.name policy_arn = each.value } # EKS Pod Identity. The trust policy trusts the Pod Identity service across the # account, and the association below is what ties the role to one namespace and # service account. Whoever can create associations can therefore hand this role # to any pod, so keep eks:CreatePodIdentityAssociation to a few people. data "aws_iam_policy_document" "pod_trust" { statement { actions = ["sts:AssumeRole", "sts:TagSession"] principals { type = "Service" identifiers = ["pods.eks.amazonaws.com"] } } } resource "aws_iam_role" "pod" { count = var.eks_cluster_name == "" ? 0 : 1 name = "${var.name}-pod-${var.eks_namespace}-${var.eks_service_account}" assume_role_policy = data.aws_iam_policy_document.pod_trust.json permissions_boundary = local.boundary } resource "aws_iam_role_policy_attachment" "pod" { for_each = var.eks_cluster_name == "" ? toset([]) : toset(var.eks_policy_arns) role = aws_iam_role.pod[0].name policy_arn = each.value } resource "aws_eks_pod_identity_association" "pod" { count = var.eks_cluster_name == "" ? 0 : 1 cluster_name = var.eks_cluster_name namespace = var.eks_namespace service_account = var.eks_service_account role_arn = aws_iam_role.pod[0].arn }